最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How to fix alignment of projection from (x,y,z) coordinates onto xy-plane in matplotlib 3d plot? - Stack Overflow

programmeradmin7浏览0评论

I was trying to make a 3D visualization of the joint probability mass function with the following code:

import math
import numpy as np
import matplotlib.pyplot as plt

def f(x, y):
    if(1 <= x + y and x + y <= 4):
        return (mathb(3, x) * mathb(2, y) * mathb(3, 4 - x - y)) / mathb(8, 4)
    else:
        return 0.0

x_domain = np.array([0, 1, 2, 3])
y_domain = np.array([0, 1, 2])

X, Y = np.meshgrid(x_domain, y_domain)
X = np.ravel(X)
Y = np.ravel(Y)

Z = np.zeros_like(X, dtype=float)
for i in range(len(X)):
    Z[i] = f(X[i], Y[i])

fig = plt.figure(figsize=(8, 4))
ax = fig.add_subplot(1, 1, 1, projection="3d")

ax.scatter(X, Y, Z) # plots the induvidual points
for i in range(len(X)):  # draws lines from xy plane up (x,y,z)
    ax.plot([X[i], X[i]], [Y[i], Y[i]], [0, Z[i]], color="r")

ax.set_xticks(x_domain)  
ax.set_yticks(y_domain)  
plt.show()

Which gave the following result:

As you can see, the stems of lines from the xy-plane do not align with the integer coordinates. I have searched through the matplotlib documentation and cant find the cause for this (unless I have overlooked something). Does anyone know how to fix the alignment issue?

EDIT It seems stack overflow could not render the mathjax equation, so I removed it.

I was trying to make a 3D visualization of the joint probability mass function with the following code:

import math
import numpy as np
import matplotlib.pyplot as plt

def f(x, y):
    if(1 <= x + y and x + y <= 4):
        return (mathb(3, x) * mathb(2, y) * mathb(3, 4 - x - y)) / mathb(8, 4)
    else:
        return 0.0

x_domain = np.array([0, 1, 2, 3])
y_domain = np.array([0, 1, 2])

X, Y = np.meshgrid(x_domain, y_domain)
X = np.ravel(X)
Y = np.ravel(Y)

Z = np.zeros_like(X, dtype=float)
for i in range(len(X)):
    Z[i] = f(X[i], Y[i])

fig = plt.figure(figsize=(8, 4))
ax = fig.add_subplot(1, 1, 1, projection="3d")

ax.scatter(X, Y, Z) # plots the induvidual points
for i in range(len(X)):  # draws lines from xy plane up (x,y,z)
    ax.plot([X[i], X[i]], [Y[i], Y[i]], [0, Z[i]], color="r")

ax.set_xticks(x_domain)  
ax.set_yticks(y_domain)  
plt.show()

Which gave the following result:

As you can see, the stems of lines from the xy-plane do not align with the integer coordinates. I have searched through the matplotlib documentation and cant find the cause for this (unless I have overlooked something). Does anyone know how to fix the alignment issue?

EDIT It seems stack overflow could not render the mathjax equation, so I removed it.

Share Improve this question asked Jan 31 at 21:53 a_floating_pointa_floating_point 3891 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Actually, they DO align with the integer grid, as you will see by rotating your plot a bit.

It's just that the z axis doesn't naturally start from 0, which is where the end of your whatever-they-are are.

Add the line

ax.set_zlim( 0.0, 0.25 )

just before plt.show() and you will be good to go.

发布评论

评论列表(0)

  1. 暂无评论