Plot matrix of weighted cells in grid with Matplotlib
I have a square matrix built from an array of random integers, defined below:
import numpy as np
dim_low, dim_high = 0, 20 #array of random integers' dimensions
matrix = np.random.random_integers(low = dim_low,high = dim_high, size=(dim_high,dim_high))
print(matrix) #the matrix of defined with repetitions of the array.
Resulted matrix in the picture:
https://i.stack.imgur.com/eEcCh.png
What could I do to plot the matrix generated in a grid with Matplotlib, in a way that the values of each cell (the weights) are printed in the center of each cell, and there's a scale from 0 to 20 in x an y axis, as in the picture below (notice that 'x' and 'o' are text in the example, what I need is the weights, in integer form, not text form):
https://i.stack.imgur.com/9mBuG.png (here)
python matplotlib matrix grid
add a comment |
I have a square matrix built from an array of random integers, defined below:
import numpy as np
dim_low, dim_high = 0, 20 #array of random integers' dimensions
matrix = np.random.random_integers(low = dim_low,high = dim_high, size=(dim_high,dim_high))
print(matrix) #the matrix of defined with repetitions of the array.
Resulted matrix in the picture:
https://i.stack.imgur.com/eEcCh.png
What could I do to plot the matrix generated in a grid with Matplotlib, in a way that the values of each cell (the weights) are printed in the center of each cell, and there's a scale from 0 to 20 in x an y axis, as in the picture below (notice that 'x' and 'o' are text in the example, what I need is the weights, in integer form, not text form):
https://i.stack.imgur.com/9mBuG.png (here)
python matplotlib matrix grid
If you are capable of producing the image shown, what's the difference to putting your matrix elements in the text instead of some letters"o"
? Which exact problem are you trying to solve?
– ImportanceOfBeingErnest
Nov 25 '18 at 15:18
add a comment |
I have a square matrix built from an array of random integers, defined below:
import numpy as np
dim_low, dim_high = 0, 20 #array of random integers' dimensions
matrix = np.random.random_integers(low = dim_low,high = dim_high, size=(dim_high,dim_high))
print(matrix) #the matrix of defined with repetitions of the array.
Resulted matrix in the picture:
https://i.stack.imgur.com/eEcCh.png
What could I do to plot the matrix generated in a grid with Matplotlib, in a way that the values of each cell (the weights) are printed in the center of each cell, and there's a scale from 0 to 20 in x an y axis, as in the picture below (notice that 'x' and 'o' are text in the example, what I need is the weights, in integer form, not text form):
https://i.stack.imgur.com/9mBuG.png (here)
python matplotlib matrix grid
I have a square matrix built from an array of random integers, defined below:
import numpy as np
dim_low, dim_high = 0, 20 #array of random integers' dimensions
matrix = np.random.random_integers(low = dim_low,high = dim_high, size=(dim_high,dim_high))
print(matrix) #the matrix of defined with repetitions of the array.
Resulted matrix in the picture:
https://i.stack.imgur.com/eEcCh.png
What could I do to plot the matrix generated in a grid with Matplotlib, in a way that the values of each cell (the weights) are printed in the center of each cell, and there's a scale from 0 to 20 in x an y axis, as in the picture below (notice that 'x' and 'o' are text in the example, what I need is the weights, in integer form, not text form):
https://i.stack.imgur.com/9mBuG.png (here)
python matplotlib matrix grid
python matplotlib matrix grid
asked Nov 25 '18 at 15:00
L'utilisatriceL'utilisatrice
83119
83119
If you are capable of producing the image shown, what's the difference to putting your matrix elements in the text instead of some letters"o"
? Which exact problem are you trying to solve?
– ImportanceOfBeingErnest
Nov 25 '18 at 15:18
add a comment |
If you are capable of producing the image shown, what's the difference to putting your matrix elements in the text instead of some letters"o"
? Which exact problem are you trying to solve?
– ImportanceOfBeingErnest
Nov 25 '18 at 15:18
If you are capable of producing the image shown, what's the difference to putting your matrix elements in the text instead of some letters
"o"
? Which exact problem are you trying to solve?– ImportanceOfBeingErnest
Nov 25 '18 at 15:18
If you are capable of producing the image shown, what's the difference to putting your matrix elements in the text instead of some letters
"o"
? Which exact problem are you trying to solve?– ImportanceOfBeingErnest
Nov 25 '18 at 15:18
add a comment |
2 Answers
2
active
oldest
votes
I pulled most of this from this post.
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))
fig, ax = plt.subplots()
for i in range(0, high_dim):
for j in range(0, high_dim):
val = matrix[i,j]
ax.text(i+0.5, j+0.5, str(val), va='center', ha='center')
ax.set_xlim(low_dim, high_dim)
ax.set_ylim(low_dim, high_dim)
ax.set_xticks(np.arange(high_dim))
ax.set_yticks(np.arange(high_dim))
ax.grid()
plt.show()
add a comment |
The right module for this would be seaborn. It has all the functionality you ask for and more...
Try using https://seaborn.pydata.org/generated/seaborn.heatmap.html. I won't take you through the different options because they're really well documented.
Goodluck!
BTW, you'll want to use a panda pivot table for comfortable compatibility.
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%2f53468770%2fplot-matrix-of-weighted-cells-in-grid-with-matplotlib%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I pulled most of this from this post.
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))
fig, ax = plt.subplots()
for i in range(0, high_dim):
for j in range(0, high_dim):
val = matrix[i,j]
ax.text(i+0.5, j+0.5, str(val), va='center', ha='center')
ax.set_xlim(low_dim, high_dim)
ax.set_ylim(low_dim, high_dim)
ax.set_xticks(np.arange(high_dim))
ax.set_yticks(np.arange(high_dim))
ax.grid()
plt.show()
add a comment |
I pulled most of this from this post.
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))
fig, ax = plt.subplots()
for i in range(0, high_dim):
for j in range(0, high_dim):
val = matrix[i,j]
ax.text(i+0.5, j+0.5, str(val), va='center', ha='center')
ax.set_xlim(low_dim, high_dim)
ax.set_ylim(low_dim, high_dim)
ax.set_xticks(np.arange(high_dim))
ax.set_yticks(np.arange(high_dim))
ax.grid()
plt.show()
add a comment |
I pulled most of this from this post.
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))
fig, ax = plt.subplots()
for i in range(0, high_dim):
for j in range(0, high_dim):
val = matrix[i,j]
ax.text(i+0.5, j+0.5, str(val), va='center', ha='center')
ax.set_xlim(low_dim, high_dim)
ax.set_ylim(low_dim, high_dim)
ax.set_xticks(np.arange(high_dim))
ax.set_yticks(np.arange(high_dim))
ax.grid()
plt.show()
I pulled most of this from this post.
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))
fig, ax = plt.subplots()
for i in range(0, high_dim):
for j in range(0, high_dim):
val = matrix[i,j]
ax.text(i+0.5, j+0.5, str(val), va='center', ha='center')
ax.set_xlim(low_dim, high_dim)
ax.set_ylim(low_dim, high_dim)
ax.set_xticks(np.arange(high_dim))
ax.set_yticks(np.arange(high_dim))
ax.grid()
plt.show()
answered Nov 25 '18 at 15:27
drbabaghanoushdrbabaghanoush
563
563
add a comment |
add a comment |
The right module for this would be seaborn. It has all the functionality you ask for and more...
Try using https://seaborn.pydata.org/generated/seaborn.heatmap.html. I won't take you through the different options because they're really well documented.
Goodluck!
BTW, you'll want to use a panda pivot table for comfortable compatibility.
add a comment |
The right module for this would be seaborn. It has all the functionality you ask for and more...
Try using https://seaborn.pydata.org/generated/seaborn.heatmap.html. I won't take you through the different options because they're really well documented.
Goodluck!
BTW, you'll want to use a panda pivot table for comfortable compatibility.
add a comment |
The right module for this would be seaborn. It has all the functionality you ask for and more...
Try using https://seaborn.pydata.org/generated/seaborn.heatmap.html. I won't take you through the different options because they're really well documented.
Goodluck!
BTW, you'll want to use a panda pivot table for comfortable compatibility.
The right module for this would be seaborn. It has all the functionality you ask for and more...
Try using https://seaborn.pydata.org/generated/seaborn.heatmap.html. I won't take you through the different options because they're really well documented.
Goodluck!
BTW, you'll want to use a panda pivot table for comfortable compatibility.
answered Nov 25 '18 at 15:22
ShlomiFShlomiF
839410
839410
add a comment |
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%2f53468770%2fplot-matrix-of-weighted-cells-in-grid-with-matplotlib%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
If you are capable of producing the image shown, what's the difference to putting your matrix elements in the text instead of some letters
"o"
? Which exact problem are you trying to solve?– ImportanceOfBeingErnest
Nov 25 '18 at 15:18