weird 3d bar plot for given data using matplotlib











up vote
0
down vote

favorite












I am trying to 3d plot below data, with height being the respecitive joint probability from probability mass function. The idea is to visualize covariance. I had to go 3D because, the probabilities varies for different combinations of sample. The bars or boxes overlap each other in weird ways that I am unable to infer a proper 3d perspective in different angles. If you look at below gif you will know (box suddenly grows over each other at few angles out of nowhere). Kindly help how to resolve this issue. Also alpha is not working.



Issues:

1. Weird 3d boxes rendering

2. Alpha also not working



Problematic output:
img



MWE (jupyter):



%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from itertools import product
from mpl_toolkits.mplot3d import Axes3D


X , Y = [100,250], [0,100,200]
xb, yb = 175, 125

import pandas as pd
matrix = np.array([
[0.20, 0.10, 0.20],
[0.05, 0.15, 0.30]
])
df = pd.DataFrame(matrix, columns=Y)
df.index = [100, 250]

top = 1
fig = plt.figure(figsize=(15,5))

ax1 = fig.add_subplot(121)

for xy in product(X,Y):
x,y = xy[0], xy[1]
z = df.loc[x,y]
d1, d2 = xb - x, yb - y
color = 'green' if d1*d2 > 0 else 'red'
ax1.add_patch(patches.Rectangle((x, y), d1, d2, alpha=z, facecolor=color))
ax1.scatter(x,y,color='black')


ax1.axvline(x=Xb, ls=':', color='blue')
ax1.axhline(y=Yb, ls=':', color='blue')
ax1.set_xticks(X)
ax1.set_yticks(Y)
ax1.set_xlim([min(X)-50,max(X)+50])
ax1.set_ylim([min(Y)-50,max(Y)+50])



ax2 = fig.add_subplot(122, projection='3d')
ax2.view_init(elev=30., azim=-50)
for xy in product(X,Y):
x ,y = xy[0], xy[1]
z = df.loc[x,y]
# print(x, y, z)
width = x - 175
depth = y - 125
pro = width*depth
top = z
bottom = np.zeros_like(top)
if pro > 0: #positive
color='#B9F6CA'
else:
color='#EF9A9A'
ax2.bar3d(x, y, bottom, -width, -depth, top, color=color)
ax2.scatter(x, y, z, color='blue')

def rotate(angle):
ax2.view_init(azim=angle)

from matplotlib import animation
ani = animation.FuncAnimation(fig, rotate, frames=np.arange(0,362,2),interval=100)
from IPython.display import HTML
plt.close()
HTML(ani.to_jshtml())


Related math problem:
enter image description here










share|improve this question






















  • The matplotlib FAQ summarizes this as My 3D plot doesn’t look right at certain viewing angles.
    – ImportanceOfBeingErnest
    Nov 4 at 11:05










  • despite a steep learning curve, i was inclind to use mayavi, but they are too problematic installing and using in a win 10, anaconda based jupyter notebook. and i did not think i would counter such artifacts even for simple 3d graphs. no workaround possible?
    – Paari Vendhan
    Nov 4 at 11:15















up vote
0
down vote

favorite












I am trying to 3d plot below data, with height being the respecitive joint probability from probability mass function. The idea is to visualize covariance. I had to go 3D because, the probabilities varies for different combinations of sample. The bars or boxes overlap each other in weird ways that I am unable to infer a proper 3d perspective in different angles. If you look at below gif you will know (box suddenly grows over each other at few angles out of nowhere). Kindly help how to resolve this issue. Also alpha is not working.



Issues:

1. Weird 3d boxes rendering

2. Alpha also not working



Problematic output:
img



MWE (jupyter):



%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from itertools import product
from mpl_toolkits.mplot3d import Axes3D


X , Y = [100,250], [0,100,200]
xb, yb = 175, 125

import pandas as pd
matrix = np.array([
[0.20, 0.10, 0.20],
[0.05, 0.15, 0.30]
])
df = pd.DataFrame(matrix, columns=Y)
df.index = [100, 250]

top = 1
fig = plt.figure(figsize=(15,5))

ax1 = fig.add_subplot(121)

for xy in product(X,Y):
x,y = xy[0], xy[1]
z = df.loc[x,y]
d1, d2 = xb - x, yb - y
color = 'green' if d1*d2 > 0 else 'red'
ax1.add_patch(patches.Rectangle((x, y), d1, d2, alpha=z, facecolor=color))
ax1.scatter(x,y,color='black')


ax1.axvline(x=Xb, ls=':', color='blue')
ax1.axhline(y=Yb, ls=':', color='blue')
ax1.set_xticks(X)
ax1.set_yticks(Y)
ax1.set_xlim([min(X)-50,max(X)+50])
ax1.set_ylim([min(Y)-50,max(Y)+50])



ax2 = fig.add_subplot(122, projection='3d')
ax2.view_init(elev=30., azim=-50)
for xy in product(X,Y):
x ,y = xy[0], xy[1]
z = df.loc[x,y]
# print(x, y, z)
width = x - 175
depth = y - 125
pro = width*depth
top = z
bottom = np.zeros_like(top)
if pro > 0: #positive
color='#B9F6CA'
else:
color='#EF9A9A'
ax2.bar3d(x, y, bottom, -width, -depth, top, color=color)
ax2.scatter(x, y, z, color='blue')

def rotate(angle):
ax2.view_init(azim=angle)

from matplotlib import animation
ani = animation.FuncAnimation(fig, rotate, frames=np.arange(0,362,2),interval=100)
from IPython.display import HTML
plt.close()
HTML(ani.to_jshtml())


Related math problem:
enter image description here










share|improve this question






















  • The matplotlib FAQ summarizes this as My 3D plot doesn’t look right at certain viewing angles.
    – ImportanceOfBeingErnest
    Nov 4 at 11:05










  • despite a steep learning curve, i was inclind to use mayavi, but they are too problematic installing and using in a win 10, anaconda based jupyter notebook. and i did not think i would counter such artifacts even for simple 3d graphs. no workaround possible?
    – Paari Vendhan
    Nov 4 at 11:15













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to 3d plot below data, with height being the respecitive joint probability from probability mass function. The idea is to visualize covariance. I had to go 3D because, the probabilities varies for different combinations of sample. The bars or boxes overlap each other in weird ways that I am unable to infer a proper 3d perspective in different angles. If you look at below gif you will know (box suddenly grows over each other at few angles out of nowhere). Kindly help how to resolve this issue. Also alpha is not working.



Issues:

1. Weird 3d boxes rendering

2. Alpha also not working



Problematic output:
img



MWE (jupyter):



%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from itertools import product
from mpl_toolkits.mplot3d import Axes3D


X , Y = [100,250], [0,100,200]
xb, yb = 175, 125

import pandas as pd
matrix = np.array([
[0.20, 0.10, 0.20],
[0.05, 0.15, 0.30]
])
df = pd.DataFrame(matrix, columns=Y)
df.index = [100, 250]

top = 1
fig = plt.figure(figsize=(15,5))

ax1 = fig.add_subplot(121)

for xy in product(X,Y):
x,y = xy[0], xy[1]
z = df.loc[x,y]
d1, d2 = xb - x, yb - y
color = 'green' if d1*d2 > 0 else 'red'
ax1.add_patch(patches.Rectangle((x, y), d1, d2, alpha=z, facecolor=color))
ax1.scatter(x,y,color='black')


ax1.axvline(x=Xb, ls=':', color='blue')
ax1.axhline(y=Yb, ls=':', color='blue')
ax1.set_xticks(X)
ax1.set_yticks(Y)
ax1.set_xlim([min(X)-50,max(X)+50])
ax1.set_ylim([min(Y)-50,max(Y)+50])



ax2 = fig.add_subplot(122, projection='3d')
ax2.view_init(elev=30., azim=-50)
for xy in product(X,Y):
x ,y = xy[0], xy[1]
z = df.loc[x,y]
# print(x, y, z)
width = x - 175
depth = y - 125
pro = width*depth
top = z
bottom = np.zeros_like(top)
if pro > 0: #positive
color='#B9F6CA'
else:
color='#EF9A9A'
ax2.bar3d(x, y, bottom, -width, -depth, top, color=color)
ax2.scatter(x, y, z, color='blue')

def rotate(angle):
ax2.view_init(azim=angle)

from matplotlib import animation
ani = animation.FuncAnimation(fig, rotate, frames=np.arange(0,362,2),interval=100)
from IPython.display import HTML
plt.close()
HTML(ani.to_jshtml())


Related math problem:
enter image description here










share|improve this question













I am trying to 3d plot below data, with height being the respecitive joint probability from probability mass function. The idea is to visualize covariance. I had to go 3D because, the probabilities varies for different combinations of sample. The bars or boxes overlap each other in weird ways that I am unable to infer a proper 3d perspective in different angles. If you look at below gif you will know (box suddenly grows over each other at few angles out of nowhere). Kindly help how to resolve this issue. Also alpha is not working.



Issues:

1. Weird 3d boxes rendering

2. Alpha also not working



Problematic output:
img



MWE (jupyter):



%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from itertools import product
from mpl_toolkits.mplot3d import Axes3D


X , Y = [100,250], [0,100,200]
xb, yb = 175, 125

import pandas as pd
matrix = np.array([
[0.20, 0.10, 0.20],
[0.05, 0.15, 0.30]
])
df = pd.DataFrame(matrix, columns=Y)
df.index = [100, 250]

top = 1
fig = plt.figure(figsize=(15,5))

ax1 = fig.add_subplot(121)

for xy in product(X,Y):
x,y = xy[0], xy[1]
z = df.loc[x,y]
d1, d2 = xb - x, yb - y
color = 'green' if d1*d2 > 0 else 'red'
ax1.add_patch(patches.Rectangle((x, y), d1, d2, alpha=z, facecolor=color))
ax1.scatter(x,y,color='black')


ax1.axvline(x=Xb, ls=':', color='blue')
ax1.axhline(y=Yb, ls=':', color='blue')
ax1.set_xticks(X)
ax1.set_yticks(Y)
ax1.set_xlim([min(X)-50,max(X)+50])
ax1.set_ylim([min(Y)-50,max(Y)+50])



ax2 = fig.add_subplot(122, projection='3d')
ax2.view_init(elev=30., azim=-50)
for xy in product(X,Y):
x ,y = xy[0], xy[1]
z = df.loc[x,y]
# print(x, y, z)
width = x - 175
depth = y - 125
pro = width*depth
top = z
bottom = np.zeros_like(top)
if pro > 0: #positive
color='#B9F6CA'
else:
color='#EF9A9A'
ax2.bar3d(x, y, bottom, -width, -depth, top, color=color)
ax2.scatter(x, y, z, color='blue')

def rotate(angle):
ax2.view_init(azim=angle)

from matplotlib import animation
ani = animation.FuncAnimation(fig, rotate, frames=np.arange(0,362,2),interval=100)
from IPython.display import HTML
plt.close()
HTML(ani.to_jshtml())


Related math problem:
enter image description here







python matplotlib 3d covariance probability-density






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 4 at 9:50









Paari Vendhan

5419




5419












  • The matplotlib FAQ summarizes this as My 3D plot doesn’t look right at certain viewing angles.
    – ImportanceOfBeingErnest
    Nov 4 at 11:05










  • despite a steep learning curve, i was inclind to use mayavi, but they are too problematic installing and using in a win 10, anaconda based jupyter notebook. and i did not think i would counter such artifacts even for simple 3d graphs. no workaround possible?
    – Paari Vendhan
    Nov 4 at 11:15


















  • The matplotlib FAQ summarizes this as My 3D plot doesn’t look right at certain viewing angles.
    – ImportanceOfBeingErnest
    Nov 4 at 11:05










  • despite a steep learning curve, i was inclind to use mayavi, but they are too problematic installing and using in a win 10, anaconda based jupyter notebook. and i did not think i would counter such artifacts even for simple 3d graphs. no workaround possible?
    – Paari Vendhan
    Nov 4 at 11:15
















The matplotlib FAQ summarizes this as My 3D plot doesn’t look right at certain viewing angles.
– ImportanceOfBeingErnest
Nov 4 at 11:05




The matplotlib FAQ summarizes this as My 3D plot doesn’t look right at certain viewing angles.
– ImportanceOfBeingErnest
Nov 4 at 11:05












despite a steep learning curve, i was inclind to use mayavi, but they are too problematic installing and using in a win 10, anaconda based jupyter notebook. and i did not think i would counter such artifacts even for simple 3d graphs. no workaround possible?
– Paari Vendhan
Nov 4 at 11:15




despite a steep learning curve, i was inclind to use mayavi, but they are too problematic installing and using in a win 10, anaconda based jupyter notebook. and i did not think i would counter such artifacts even for simple 3d graphs. no workaround possible?
– Paari Vendhan
Nov 4 at 11:15

















active

oldest

votes











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',
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
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53139534%2fweird-3d-bar-plot-for-given-data-using-matplotlib%23new-answer', 'question_page');
}
);

Post as a guest





































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53139534%2fweird-3d-bar-plot-for-given-data-using-matplotlib%23new-answer', 'question_page');
}
);

Post as a guest




















































































這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini