How to plot a 3D histogram with matplotlib/mplot3d?
I have three arrays and I am trying to make a 3D histogram.
x = [1, 2, 3, 2, 5, 2, 6, 8, 6, 7]
y = [10, 10, 20, 50, 20, 20, 30, 10, 40, 50, 60]
z = [105, 25, 26, 74, 39, 85, 74, 153, 52, 98]
Here's my attempt so far:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = plt.axes(projection='3d')
binsOne = sorted(set(x))
binsTwo = sorted(set(y))
hist, xedges, yedges = np.histogram2d(x, y, bins=[binsOne, binsTwo])
xpos, ypos = np.meshgrid(xedges[:-1] + 0.25 , yedges[:-1] + 0.25)
xpos = xpos.flatten('F')
ypos = ypos.flatten('F')
zpos = np.zeros_like(xpos)
dx = dx.flatten()
dy = dy.flatten()
dz = hist.flatten()
ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='b', zsort='average')
How do I incorporate the z array into my 3D histogram?
python matplotlib mplot3d
add a comment |
I have three arrays and I am trying to make a 3D histogram.
x = [1, 2, 3, 2, 5, 2, 6, 8, 6, 7]
y = [10, 10, 20, 50, 20, 20, 30, 10, 40, 50, 60]
z = [105, 25, 26, 74, 39, 85, 74, 153, 52, 98]
Here's my attempt so far:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = plt.axes(projection='3d')
binsOne = sorted(set(x))
binsTwo = sorted(set(y))
hist, xedges, yedges = np.histogram2d(x, y, bins=[binsOne, binsTwo])
xpos, ypos = np.meshgrid(xedges[:-1] + 0.25 , yedges[:-1] + 0.25)
xpos = xpos.flatten('F')
ypos = ypos.flatten('F')
zpos = np.zeros_like(xpos)
dx = dx.flatten()
dy = dy.flatten()
dz = hist.flatten()
ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='b', zsort='average')
How do I incorporate the z array into my 3D histogram?
python matplotlib mplot3d
I advice you this post: stackoverflow.com/questions/53611716/… Matplotlib have some issues with the 3D plotting, so pay attention!
– Alessandro Peca
Jan 3 at 8:25
add a comment |
I have three arrays and I am trying to make a 3D histogram.
x = [1, 2, 3, 2, 5, 2, 6, 8, 6, 7]
y = [10, 10, 20, 50, 20, 20, 30, 10, 40, 50, 60]
z = [105, 25, 26, 74, 39, 85, 74, 153, 52, 98]
Here's my attempt so far:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = plt.axes(projection='3d')
binsOne = sorted(set(x))
binsTwo = sorted(set(y))
hist, xedges, yedges = np.histogram2d(x, y, bins=[binsOne, binsTwo])
xpos, ypos = np.meshgrid(xedges[:-1] + 0.25 , yedges[:-1] + 0.25)
xpos = xpos.flatten('F')
ypos = ypos.flatten('F')
zpos = np.zeros_like(xpos)
dx = dx.flatten()
dy = dy.flatten()
dz = hist.flatten()
ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='b', zsort='average')
How do I incorporate the z array into my 3D histogram?
python matplotlib mplot3d
I have three arrays and I am trying to make a 3D histogram.
x = [1, 2, 3, 2, 5, 2, 6, 8, 6, 7]
y = [10, 10, 20, 50, 20, 20, 30, 10, 40, 50, 60]
z = [105, 25, 26, 74, 39, 85, 74, 153, 52, 98]
Here's my attempt so far:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = plt.axes(projection='3d')
binsOne = sorted(set(x))
binsTwo = sorted(set(y))
hist, xedges, yedges = np.histogram2d(x, y, bins=[binsOne, binsTwo])
xpos, ypos = np.meshgrid(xedges[:-1] + 0.25 , yedges[:-1] + 0.25)
xpos = xpos.flatten('F')
ypos = ypos.flatten('F')
zpos = np.zeros_like(xpos)
dx = dx.flatten()
dy = dy.flatten()
dz = hist.flatten()
ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='b', zsort='average')
How do I incorporate the z array into my 3D histogram?
python matplotlib mplot3d
python matplotlib mplot3d
edited Jan 3 at 0:07
Matt-pow
asked Jan 2 at 23:45
Matt-powMatt-pow
155416
155416
I advice you this post: stackoverflow.com/questions/53611716/… Matplotlib have some issues with the 3D plotting, so pay attention!
– Alessandro Peca
Jan 3 at 8:25
add a comment |
I advice you this post: stackoverflow.com/questions/53611716/… Matplotlib have some issues with the 3D plotting, so pay attention!
– Alessandro Peca
Jan 3 at 8:25
I advice you this post: stackoverflow.com/questions/53611716/… Matplotlib have some issues with the 3D plotting, so pay attention!
– Alessandro Peca
Jan 3 at 8:25
I advice you this post: stackoverflow.com/questions/53611716/… Matplotlib have some issues with the 3D plotting, so pay attention!
– Alessandro Peca
Jan 3 at 8:25
add a comment |
1 Answer
1
active
oldest
votes
The z
array must have the same shape not of x
and y
but of xpos
and ypos
(which are of themselves the same shape). You may find this example more useful than the one you appear to be drawing from. The following code is to demonstrate the example in the first link applied to your question,
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
_x = [1, 2, 3, 2, 5, 2, 6, 8, 6, 7]
_y = [10, 10, 20, 50, 20, 20, 30, 10, 40, 50]
_xx, _yy = np.meshgrid(_x, _y)
x, y = _xx.ravel(), _yy.ravel()
_z = np.array([105, 25, 26, 74, 39, 85, 74, 153, 52, 98])
# There may be an easier way to do this, but I am not aware of it
z = np.zeros(len(x))
for i in range(1, len(x)):
z[i] = _z[(i*len(_z)) / len(x)]
bottom = np.zeros_like(z)
width = depth = 1
ax.bar3d(x, y, bottom, width, depth, z, shade=True)
plt.show()
Thanks for your answer, why is the z not following x and y in ax.bar3d? Also can you explain to me what does the for loop does for _z?
– Matt-pow
Jan 3 at 6:04
@Matt-pow I don't understand your question, thez
values in the array are determining the height of the the bar, thex
values determine thex
position and they
values determine they
position. Forx
andy
pairs that correspond to a commonz
value you see multiple bars. The values on they
andx
axis are a little confusing since the bar1
inx
actually goes from1
to2
and8
goes from8
to9
.
– William Miller
Jan 3 at 6:25
@Matt-pow Thefor
loop for_z
converts_z
, a shape(10L, )
array intoz
a shape(100L, )
array populated by 10 instances of each value
– William Miller
Jan 3 at 6:37
1
Thanks for the explaination
– Matt-pow
Jan 3 at 21:45
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%2f54014645%2fhow-to-plot-a-3d-histogram-with-matplotlib-mplot3d%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
The z
array must have the same shape not of x
and y
but of xpos
and ypos
(which are of themselves the same shape). You may find this example more useful than the one you appear to be drawing from. The following code is to demonstrate the example in the first link applied to your question,
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
_x = [1, 2, 3, 2, 5, 2, 6, 8, 6, 7]
_y = [10, 10, 20, 50, 20, 20, 30, 10, 40, 50]
_xx, _yy = np.meshgrid(_x, _y)
x, y = _xx.ravel(), _yy.ravel()
_z = np.array([105, 25, 26, 74, 39, 85, 74, 153, 52, 98])
# There may be an easier way to do this, but I am not aware of it
z = np.zeros(len(x))
for i in range(1, len(x)):
z[i] = _z[(i*len(_z)) / len(x)]
bottom = np.zeros_like(z)
width = depth = 1
ax.bar3d(x, y, bottom, width, depth, z, shade=True)
plt.show()
Thanks for your answer, why is the z not following x and y in ax.bar3d? Also can you explain to me what does the for loop does for _z?
– Matt-pow
Jan 3 at 6:04
@Matt-pow I don't understand your question, thez
values in the array are determining the height of the the bar, thex
values determine thex
position and they
values determine they
position. Forx
andy
pairs that correspond to a commonz
value you see multiple bars. The values on they
andx
axis are a little confusing since the bar1
inx
actually goes from1
to2
and8
goes from8
to9
.
– William Miller
Jan 3 at 6:25
@Matt-pow Thefor
loop for_z
converts_z
, a shape(10L, )
array intoz
a shape(100L, )
array populated by 10 instances of each value
– William Miller
Jan 3 at 6:37
1
Thanks for the explaination
– Matt-pow
Jan 3 at 21:45
add a comment |
The z
array must have the same shape not of x
and y
but of xpos
and ypos
(which are of themselves the same shape). You may find this example more useful than the one you appear to be drawing from. The following code is to demonstrate the example in the first link applied to your question,
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
_x = [1, 2, 3, 2, 5, 2, 6, 8, 6, 7]
_y = [10, 10, 20, 50, 20, 20, 30, 10, 40, 50]
_xx, _yy = np.meshgrid(_x, _y)
x, y = _xx.ravel(), _yy.ravel()
_z = np.array([105, 25, 26, 74, 39, 85, 74, 153, 52, 98])
# There may be an easier way to do this, but I am not aware of it
z = np.zeros(len(x))
for i in range(1, len(x)):
z[i] = _z[(i*len(_z)) / len(x)]
bottom = np.zeros_like(z)
width = depth = 1
ax.bar3d(x, y, bottom, width, depth, z, shade=True)
plt.show()
Thanks for your answer, why is the z not following x and y in ax.bar3d? Also can you explain to me what does the for loop does for _z?
– Matt-pow
Jan 3 at 6:04
@Matt-pow I don't understand your question, thez
values in the array are determining the height of the the bar, thex
values determine thex
position and they
values determine they
position. Forx
andy
pairs that correspond to a commonz
value you see multiple bars. The values on they
andx
axis are a little confusing since the bar1
inx
actually goes from1
to2
and8
goes from8
to9
.
– William Miller
Jan 3 at 6:25
@Matt-pow Thefor
loop for_z
converts_z
, a shape(10L, )
array intoz
a shape(100L, )
array populated by 10 instances of each value
– William Miller
Jan 3 at 6:37
1
Thanks for the explaination
– Matt-pow
Jan 3 at 21:45
add a comment |
The z
array must have the same shape not of x
and y
but of xpos
and ypos
(which are of themselves the same shape). You may find this example more useful than the one you appear to be drawing from. The following code is to demonstrate the example in the first link applied to your question,
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
_x = [1, 2, 3, 2, 5, 2, 6, 8, 6, 7]
_y = [10, 10, 20, 50, 20, 20, 30, 10, 40, 50]
_xx, _yy = np.meshgrid(_x, _y)
x, y = _xx.ravel(), _yy.ravel()
_z = np.array([105, 25, 26, 74, 39, 85, 74, 153, 52, 98])
# There may be an easier way to do this, but I am not aware of it
z = np.zeros(len(x))
for i in range(1, len(x)):
z[i] = _z[(i*len(_z)) / len(x)]
bottom = np.zeros_like(z)
width = depth = 1
ax.bar3d(x, y, bottom, width, depth, z, shade=True)
plt.show()
The z
array must have the same shape not of x
and y
but of xpos
and ypos
(which are of themselves the same shape). You may find this example more useful than the one you appear to be drawing from. The following code is to demonstrate the example in the first link applied to your question,
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
_x = [1, 2, 3, 2, 5, 2, 6, 8, 6, 7]
_y = [10, 10, 20, 50, 20, 20, 30, 10, 40, 50]
_xx, _yy = np.meshgrid(_x, _y)
x, y = _xx.ravel(), _yy.ravel()
_z = np.array([105, 25, 26, 74, 39, 85, 74, 153, 52, 98])
# There may be an easier way to do this, but I am not aware of it
z = np.zeros(len(x))
for i in range(1, len(x)):
z[i] = _z[(i*len(_z)) / len(x)]
bottom = np.zeros_like(z)
width = depth = 1
ax.bar3d(x, y, bottom, width, depth, z, shade=True)
plt.show()
answered Jan 3 at 0:31


William MillerWilliam Miller
1,478317
1,478317
Thanks for your answer, why is the z not following x and y in ax.bar3d? Also can you explain to me what does the for loop does for _z?
– Matt-pow
Jan 3 at 6:04
@Matt-pow I don't understand your question, thez
values in the array are determining the height of the the bar, thex
values determine thex
position and they
values determine they
position. Forx
andy
pairs that correspond to a commonz
value you see multiple bars. The values on they
andx
axis are a little confusing since the bar1
inx
actually goes from1
to2
and8
goes from8
to9
.
– William Miller
Jan 3 at 6:25
@Matt-pow Thefor
loop for_z
converts_z
, a shape(10L, )
array intoz
a shape(100L, )
array populated by 10 instances of each value
– William Miller
Jan 3 at 6:37
1
Thanks for the explaination
– Matt-pow
Jan 3 at 21:45
add a comment |
Thanks for your answer, why is the z not following x and y in ax.bar3d? Also can you explain to me what does the for loop does for _z?
– Matt-pow
Jan 3 at 6:04
@Matt-pow I don't understand your question, thez
values in the array are determining the height of the the bar, thex
values determine thex
position and they
values determine they
position. Forx
andy
pairs that correspond to a commonz
value you see multiple bars. The values on they
andx
axis are a little confusing since the bar1
inx
actually goes from1
to2
and8
goes from8
to9
.
– William Miller
Jan 3 at 6:25
@Matt-pow Thefor
loop for_z
converts_z
, a shape(10L, )
array intoz
a shape(100L, )
array populated by 10 instances of each value
– William Miller
Jan 3 at 6:37
1
Thanks for the explaination
– Matt-pow
Jan 3 at 21:45
Thanks for your answer, why is the z not following x and y in ax.bar3d? Also can you explain to me what does the for loop does for _z?
– Matt-pow
Jan 3 at 6:04
Thanks for your answer, why is the z not following x and y in ax.bar3d? Also can you explain to me what does the for loop does for _z?
– Matt-pow
Jan 3 at 6:04
@Matt-pow I don't understand your question, the
z
values in the array are determining the height of the the bar, the x
values determine the x
position and the y
values determine the y
position. For x
and y
pairs that correspond to a common z
value you see multiple bars. The values on the y
and x
axis are a little confusing since the bar 1
in x
actually goes from 1
to 2
and 8
goes from 8
to 9
.– William Miller
Jan 3 at 6:25
@Matt-pow I don't understand your question, the
z
values in the array are determining the height of the the bar, the x
values determine the x
position and the y
values determine the y
position. For x
and y
pairs that correspond to a common z
value you see multiple bars. The values on the y
and x
axis are a little confusing since the bar 1
in x
actually goes from 1
to 2
and 8
goes from 8
to 9
.– William Miller
Jan 3 at 6:25
@Matt-pow The
for
loop for _z
converts _z
, a shape (10L, )
array into z
a shape (100L, )
array populated by 10 instances of each value– William Miller
Jan 3 at 6:37
@Matt-pow The
for
loop for _z
converts _z
, a shape (10L, )
array into z
a shape (100L, )
array populated by 10 instances of each value– William Miller
Jan 3 at 6:37
1
1
Thanks for the explaination
– Matt-pow
Jan 3 at 21:45
Thanks for the explaination
– Matt-pow
Jan 3 at 21:45
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%2f54014645%2fhow-to-plot-a-3d-histogram-with-matplotlib-mplot3d%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
I advice you this post: stackoverflow.com/questions/53611716/… Matplotlib have some issues with the 3D plotting, so pay attention!
– Alessandro Peca
Jan 3 at 8:25