Matplotlib: Increase ax.matshow width/height, and set color map (cmap) to transparent
I have a matrix built of random values and I plotted it with ax.matplot
, where the value of each matrix cell is plotted in the center of the matrix cell, and I need to increase the size of the plot (specially the width) and also set the color map (cmap) to "transparent", where the cells of the matrix plot will be white and the matrix value in the center of the cell with be black.
The code:
import numpy as np
import matplotlib.pyplot as plt
low_dim = 0
high_dim = 20
matrix = np.random.randint(low_dim, high_dim, (high_dim,high_dim))
print (matrix)
fig, ax = plt.subplots()
ax.matshow(matriz, cmap=None,interpolation='nearest')
for i in xrange(20):
for j in xrange(20):
number = matrix[j,i]
ax.text(i, j, str(number), va='center', ha='center')
plt.show()
EDIT
That's matrix
in the code (https://i.stack.imgur.com/e3mGO.png (matrix), a matrix generated with the command matrix = np.random.randint(low_dim, high_dim, (high_dim,high_dim))
, the picture is from the console.
I've tried changing the figsize in fig,ax = plt.subplot()
,but it doesn't change anything. And I've also set cmap = none
but nothing happened.
Another problem is that, in the first picture, the numbers in the index of x and y axes are varying from 0 to 15 and stepping 5, like 0, 5, 10, 15 (I've marked it in red). But in the second picture (also marked in red), the values are from 0 to 19,with no step. If possible, I need to arrive to the second picture's result, the indexes varying from 0 to 19 with no step.
Picture 1: What I'm getting: https://i.stack.imgur.com/mlExQ.png
Picture 2: What I need to get: https://i.stack.imgur.com/T3CC4.png
python-2.7 matplotlib axes colormap
add a comment |
I have a matrix built of random values and I plotted it with ax.matplot
, where the value of each matrix cell is plotted in the center of the matrix cell, and I need to increase the size of the plot (specially the width) and also set the color map (cmap) to "transparent", where the cells of the matrix plot will be white and the matrix value in the center of the cell with be black.
The code:
import numpy as np
import matplotlib.pyplot as plt
low_dim = 0
high_dim = 20
matrix = np.random.randint(low_dim, high_dim, (high_dim,high_dim))
print (matrix)
fig, ax = plt.subplots()
ax.matshow(matriz, cmap=None,interpolation='nearest')
for i in xrange(20):
for j in xrange(20):
number = matrix[j,i]
ax.text(i, j, str(number), va='center', ha='center')
plt.show()
EDIT
That's matrix
in the code (https://i.stack.imgur.com/e3mGO.png (matrix), a matrix generated with the command matrix = np.random.randint(low_dim, high_dim, (high_dim,high_dim))
, the picture is from the console.
I've tried changing the figsize in fig,ax = plt.subplot()
,but it doesn't change anything. And I've also set cmap = none
but nothing happened.
Another problem is that, in the first picture, the numbers in the index of x and y axes are varying from 0 to 15 and stepping 5, like 0, 5, 10, 15 (I've marked it in red). But in the second picture (also marked in red), the values are from 0 to 19,with no step. If possible, I need to arrive to the second picture's result, the indexes varying from 0 to 19 with no step.
Picture 1: What I'm getting: https://i.stack.imgur.com/mlExQ.png
Picture 2: What I need to get: https://i.stack.imgur.com/T3CC4.png
python-2.7 matplotlib axes colormap
What ismatrix
in your code? And if you use Python 2.7, you should tag the question accordingly.
– Mr. T
Nov 25 '18 at 18:26
I've edited it.
– L'utilisatrice
Nov 25 '18 at 18:35
I would have hoped, this example to be of help.
– ImportanceOfBeingErnest
Nov 25 '18 at 18:41
add a comment |
I have a matrix built of random values and I plotted it with ax.matplot
, where the value of each matrix cell is plotted in the center of the matrix cell, and I need to increase the size of the plot (specially the width) and also set the color map (cmap) to "transparent", where the cells of the matrix plot will be white and the matrix value in the center of the cell with be black.
The code:
import numpy as np
import matplotlib.pyplot as plt
low_dim = 0
high_dim = 20
matrix = np.random.randint(low_dim, high_dim, (high_dim,high_dim))
print (matrix)
fig, ax = plt.subplots()
ax.matshow(matriz, cmap=None,interpolation='nearest')
for i in xrange(20):
for j in xrange(20):
number = matrix[j,i]
ax.text(i, j, str(number), va='center', ha='center')
plt.show()
EDIT
That's matrix
in the code (https://i.stack.imgur.com/e3mGO.png (matrix), a matrix generated with the command matrix = np.random.randint(low_dim, high_dim, (high_dim,high_dim))
, the picture is from the console.
I've tried changing the figsize in fig,ax = plt.subplot()
,but it doesn't change anything. And I've also set cmap = none
but nothing happened.
Another problem is that, in the first picture, the numbers in the index of x and y axes are varying from 0 to 15 and stepping 5, like 0, 5, 10, 15 (I've marked it in red). But in the second picture (also marked in red), the values are from 0 to 19,with no step. If possible, I need to arrive to the second picture's result, the indexes varying from 0 to 19 with no step.
Picture 1: What I'm getting: https://i.stack.imgur.com/mlExQ.png
Picture 2: What I need to get: https://i.stack.imgur.com/T3CC4.png
python-2.7 matplotlib axes colormap
I have a matrix built of random values and I plotted it with ax.matplot
, where the value of each matrix cell is plotted in the center of the matrix cell, and I need to increase the size of the plot (specially the width) and also set the color map (cmap) to "transparent", where the cells of the matrix plot will be white and the matrix value in the center of the cell with be black.
The code:
import numpy as np
import matplotlib.pyplot as plt
low_dim = 0
high_dim = 20
matrix = np.random.randint(low_dim, high_dim, (high_dim,high_dim))
print (matrix)
fig, ax = plt.subplots()
ax.matshow(matriz, cmap=None,interpolation='nearest')
for i in xrange(20):
for j in xrange(20):
number = matrix[j,i]
ax.text(i, j, str(number), va='center', ha='center')
plt.show()
EDIT
That's matrix
in the code (https://i.stack.imgur.com/e3mGO.png (matrix), a matrix generated with the command matrix = np.random.randint(low_dim, high_dim, (high_dim,high_dim))
, the picture is from the console.
I've tried changing the figsize in fig,ax = plt.subplot()
,but it doesn't change anything. And I've also set cmap = none
but nothing happened.
Another problem is that, in the first picture, the numbers in the index of x and y axes are varying from 0 to 15 and stepping 5, like 0, 5, 10, 15 (I've marked it in red). But in the second picture (also marked in red), the values are from 0 to 19,with no step. If possible, I need to arrive to the second picture's result, the indexes varying from 0 to 19 with no step.
Picture 1: What I'm getting: https://i.stack.imgur.com/mlExQ.png
Picture 2: What I need to get: https://i.stack.imgur.com/T3CC4.png
python-2.7 matplotlib axes colormap
python-2.7 matplotlib axes colormap
edited Nov 25 '18 at 18:34
L'utilisatrice
asked Nov 25 '18 at 18:12
L'utilisatriceL'utilisatrice
981110
981110
What ismatrix
in your code? And if you use Python 2.7, you should tag the question accordingly.
– Mr. T
Nov 25 '18 at 18:26
I've edited it.
– L'utilisatrice
Nov 25 '18 at 18:35
I would have hoped, this example to be of help.
– ImportanceOfBeingErnest
Nov 25 '18 at 18:41
add a comment |
What ismatrix
in your code? And if you use Python 2.7, you should tag the question accordingly.
– Mr. T
Nov 25 '18 at 18:26
I've edited it.
– L'utilisatrice
Nov 25 '18 at 18:35
I would have hoped, this example to be of help.
– ImportanceOfBeingErnest
Nov 25 '18 at 18:41
What is
matrix
in your code? And if you use Python 2.7, you should tag the question accordingly.– Mr. T
Nov 25 '18 at 18:26
What is
matrix
in your code? And if you use Python 2.7, you should tag the question accordingly.– Mr. T
Nov 25 '18 at 18:26
I've edited it.
– L'utilisatrice
Nov 25 '18 at 18:35
I've edited it.
– L'utilisatrice
Nov 25 '18 at 18:35
I would have hoped, this example to be of help.
– ImportanceOfBeingErnest
Nov 25 '18 at 18:41
I would have hoped, this example to be of help.
– ImportanceOfBeingErnest
Nov 25 '18 at 18:41
add a comment |
1 Answer
1
active
oldest
votes
matshow takes arguments for origin and alpha. The rest is just settings ticks and labels.
ax.matshow(matrix, origin='lower', alpha=0, cmap=None, interpolation='nearest')
tick_labels = range(high_dim)
ax.set_xticks(, minor=False)
ax.set_xticks(np.arange(-.5, 20, 1), minor=True)
ax.set_yticks(, minor=False)
ax.set_yticks(np.arange(-.5, 20, 1), minor=True)
ax.set_xticklabels(, minor=False)
ax.set_xticklabels(tick_labels, minor=True)
ax.set_yticklabels(, minor=False)
ax.set_yticklabels(tick_labels, minor=True)
ax.xaxis.set_ticks_position('bottom')
ax.grid(which='minor', color='grey', linestyle='-', linewidth=1)
I ran the code and the result was a little problem I faced before and wasn't capable of solving: That's the resulted matrix in the console (I print it first in the console to check if the plot code is fine) ---> i.imgur.com/JpS5ZJF.jpg, and that's the plot generated ---> i.imgur.com/4RRiXU3.jpg. Notice that the lines are "upside down", and so are the index of y axis (19 at top, 0 at bottom). How could I soulve that?
– L'utilisatrice
Nov 26 '18 at 1:18
If you want to have your origin in the upper left corner, you can setorigin='upper'
– pxe
Nov 26 '18 at 1:28
Thank you so much! It's working now.
– L'utilisatrice
Nov 26 '18 at 11:58
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470427%2fmatplotlib-increase-ax-matshow-width-height-and-set-color-map-cmap-to-transp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
matshow takes arguments for origin and alpha. The rest is just settings ticks and labels.
ax.matshow(matrix, origin='lower', alpha=0, cmap=None, interpolation='nearest')
tick_labels = range(high_dim)
ax.set_xticks(, minor=False)
ax.set_xticks(np.arange(-.5, 20, 1), minor=True)
ax.set_yticks(, minor=False)
ax.set_yticks(np.arange(-.5, 20, 1), minor=True)
ax.set_xticklabels(, minor=False)
ax.set_xticklabels(tick_labels, minor=True)
ax.set_yticklabels(, minor=False)
ax.set_yticklabels(tick_labels, minor=True)
ax.xaxis.set_ticks_position('bottom')
ax.grid(which='minor', color='grey', linestyle='-', linewidth=1)
I ran the code and the result was a little problem I faced before and wasn't capable of solving: That's the resulted matrix in the console (I print it first in the console to check if the plot code is fine) ---> i.imgur.com/JpS5ZJF.jpg, and that's the plot generated ---> i.imgur.com/4RRiXU3.jpg. Notice that the lines are "upside down", and so are the index of y axis (19 at top, 0 at bottom). How could I soulve that?
– L'utilisatrice
Nov 26 '18 at 1:18
If you want to have your origin in the upper left corner, you can setorigin='upper'
– pxe
Nov 26 '18 at 1:28
Thank you so much! It's working now.
– L'utilisatrice
Nov 26 '18 at 11:58
add a comment |
matshow takes arguments for origin and alpha. The rest is just settings ticks and labels.
ax.matshow(matrix, origin='lower', alpha=0, cmap=None, interpolation='nearest')
tick_labels = range(high_dim)
ax.set_xticks(, minor=False)
ax.set_xticks(np.arange(-.5, 20, 1), minor=True)
ax.set_yticks(, minor=False)
ax.set_yticks(np.arange(-.5, 20, 1), minor=True)
ax.set_xticklabels(, minor=False)
ax.set_xticklabels(tick_labels, minor=True)
ax.set_yticklabels(, minor=False)
ax.set_yticklabels(tick_labels, minor=True)
ax.xaxis.set_ticks_position('bottom')
ax.grid(which='minor', color='grey', linestyle='-', linewidth=1)
I ran the code and the result was a little problem I faced before and wasn't capable of solving: That's the resulted matrix in the console (I print it first in the console to check if the plot code is fine) ---> i.imgur.com/JpS5ZJF.jpg, and that's the plot generated ---> i.imgur.com/4RRiXU3.jpg. Notice that the lines are "upside down", and so are the index of y axis (19 at top, 0 at bottom). How could I soulve that?
– L'utilisatrice
Nov 26 '18 at 1:18
If you want to have your origin in the upper left corner, you can setorigin='upper'
– pxe
Nov 26 '18 at 1:28
Thank you so much! It's working now.
– L'utilisatrice
Nov 26 '18 at 11:58
add a comment |
matshow takes arguments for origin and alpha. The rest is just settings ticks and labels.
ax.matshow(matrix, origin='lower', alpha=0, cmap=None, interpolation='nearest')
tick_labels = range(high_dim)
ax.set_xticks(, minor=False)
ax.set_xticks(np.arange(-.5, 20, 1), minor=True)
ax.set_yticks(, minor=False)
ax.set_yticks(np.arange(-.5, 20, 1), minor=True)
ax.set_xticklabels(, minor=False)
ax.set_xticklabels(tick_labels, minor=True)
ax.set_yticklabels(, minor=False)
ax.set_yticklabels(tick_labels, minor=True)
ax.xaxis.set_ticks_position('bottom')
ax.grid(which='minor', color='grey', linestyle='-', linewidth=1)
matshow takes arguments for origin and alpha. The rest is just settings ticks and labels.
ax.matshow(matrix, origin='lower', alpha=0, cmap=None, interpolation='nearest')
tick_labels = range(high_dim)
ax.set_xticks(, minor=False)
ax.set_xticks(np.arange(-.5, 20, 1), minor=True)
ax.set_yticks(, minor=False)
ax.set_yticks(np.arange(-.5, 20, 1), minor=True)
ax.set_xticklabels(, minor=False)
ax.set_xticklabels(tick_labels, minor=True)
ax.set_yticklabels(, minor=False)
ax.set_yticklabels(tick_labels, minor=True)
ax.xaxis.set_ticks_position('bottom')
ax.grid(which='minor', color='grey', linestyle='-', linewidth=1)
answered Nov 26 '18 at 0:56
pxepxe
1207
1207
I ran the code and the result was a little problem I faced before and wasn't capable of solving: That's the resulted matrix in the console (I print it first in the console to check if the plot code is fine) ---> i.imgur.com/JpS5ZJF.jpg, and that's the plot generated ---> i.imgur.com/4RRiXU3.jpg. Notice that the lines are "upside down", and so are the index of y axis (19 at top, 0 at bottom). How could I soulve that?
– L'utilisatrice
Nov 26 '18 at 1:18
If you want to have your origin in the upper left corner, you can setorigin='upper'
– pxe
Nov 26 '18 at 1:28
Thank you so much! It's working now.
– L'utilisatrice
Nov 26 '18 at 11:58
add a comment |
I ran the code and the result was a little problem I faced before and wasn't capable of solving: That's the resulted matrix in the console (I print it first in the console to check if the plot code is fine) ---> i.imgur.com/JpS5ZJF.jpg, and that's the plot generated ---> i.imgur.com/4RRiXU3.jpg. Notice that the lines are "upside down", and so are the index of y axis (19 at top, 0 at bottom). How could I soulve that?
– L'utilisatrice
Nov 26 '18 at 1:18
If you want to have your origin in the upper left corner, you can setorigin='upper'
– pxe
Nov 26 '18 at 1:28
Thank you so much! It's working now.
– L'utilisatrice
Nov 26 '18 at 11:58
I ran the code and the result was a little problem I faced before and wasn't capable of solving: That's the resulted matrix in the console (I print it first in the console to check if the plot code is fine) ---> i.imgur.com/JpS5ZJF.jpg, and that's the plot generated ---> i.imgur.com/4RRiXU3.jpg. Notice that the lines are "upside down", and so are the index of y axis (19 at top, 0 at bottom). How could I soulve that?
– L'utilisatrice
Nov 26 '18 at 1:18
I ran the code and the result was a little problem I faced before and wasn't capable of solving: That's the resulted matrix in the console (I print it first in the console to check if the plot code is fine) ---> i.imgur.com/JpS5ZJF.jpg, and that's the plot generated ---> i.imgur.com/4RRiXU3.jpg. Notice that the lines are "upside down", and so are the index of y axis (19 at top, 0 at bottom). How could I soulve that?
– L'utilisatrice
Nov 26 '18 at 1:18
If you want to have your origin in the upper left corner, you can set
origin='upper'
– pxe
Nov 26 '18 at 1:28
If you want to have your origin in the upper left corner, you can set
origin='upper'
– pxe
Nov 26 '18 at 1:28
Thank you so much! It's working now.
– L'utilisatrice
Nov 26 '18 at 11:58
Thank you so much! It's working now.
– L'utilisatrice
Nov 26 '18 at 11:58
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470427%2fmatplotlib-increase-ax-matshow-width-height-and-set-color-map-cmap-to-transp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is
matrix
in your code? And if you use Python 2.7, you should tag the question accordingly.– Mr. T
Nov 25 '18 at 18:26
I've edited it.
– L'utilisatrice
Nov 25 '18 at 18:35
I would have hoped, this example to be of help.
– ImportanceOfBeingErnest
Nov 25 '18 at 18:41