本文介绍了Matplotlib - 3D条形图上的错误法线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题






使用matplotlib并在图表上绘制3d条形图时






$ b

b $ b


当我绘制一个240密耳的高密度图表时,我得到这个结果:



看到一些酒吧的某些面孔是错误的?






我使用的是Matplotlib和Numpy的最新稳定版本。我的Python版本是2.7.3






LOGS






这是我从控制台获得的唯一警告:






任何帮助都非常感谢。






EDIT b $ b

使用@Saullo Castro答案,这是生成的新图:



或者,使用回答中提供的示例(参见标有红点的区域):





唯一的问题左边是顶部的酒吧脸,但已经相当不错。

解决方案

使用参数 zsort =当调用 ax.bar3d()时,'max'会解决您的问题(请参阅):

  ax.bar3d(xpos,ypos,zpos,dx,dy,dz,color ='b',alpha = 1,zsort ='max')
pre>

我使用了来自

 <$ c $> 

c> from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

data = np.array([[0,1,0,2, 0],
[0,3,0,2,0],
[6,1,1,7,0],
[0,5,0,2,9] ,
[0,1,0,4,0],
[9,1,3,4,2],
[0,0,2,1,3],] )

column_names = ['a','b','c','d','e']
row_names = ['Mon','Tue','Wed' ,'Thu','Fri','Sat','Sun']

fig = plt.figure()
ax = Axes3D(fig)

lx = len(data [0])#制定矩阵维数
ly = len(data [:,0])
xpos = np.arange(0,lx,1)的位置
ypos = np.arange(0,ly,1)
xpos,ypos = np.meshgrid(xpos + 0.5,ypos + 0.5)

xpos = xpos。 flatten()#将位置转换为1D数组
ypos = ypos.flatten()
zpos = np.ones(lx * ly)* 1e-10

dx = 1。 * np.ones_like(zpos)
dy = dx.copy()
dz = data.flatten()

ax.bar3d(xpos,ypos,zpos,dx,dy ,dz,color ='b',alpha = 1,zsort ='max')
plt.ion()
plt.show()
/ pre>

PROBLEM


When using matplotlib and plotting 3d bars on a chart I got wrong normals values on some bar faces.


EXAMPLE


When I plot a high density bins graph, with 240 bars, I get this result:

See that some faces of some bars are wrong? The bars Z order gets wrong too.


ABOUT


I'm using the latest stable version of Matplotlib and Numpy. My Python version is 2.7.3


LOGS


This is the only warning the I get from console:


Any help is much appreciated.


EDIT

With @Saullo Castro answer, this is the new graph produced:

Or, using the sample presented in the answer (see the region marked with red dots):

The only problem left is the bar face on the top, but is already pretty good. If anyone has any comments on this, feel free to help me.

解决方案

Using the parameter zsort='max' when you call ax.bar3d()solves your problem (see here):

ax.bar3d(xpos,ypos,zpos, dx, dy, dz,  color='b', alpha=1., zsort='max')

I used a modified version of the code from this other question to play with your problem:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

data = np.array([[0,1,0,2,0],
                 [0,3,0,2,0],
                 [6,1,1,7,0],
                 [0,5,0,2,9],
                 [0,1,0,4,0],
                 [9,1,3,4,2],
                 [0,0,2,1,3], ])

column_names = ['a','b','c','d','e']
row_names = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']

fig = plt.figure()
ax = Axes3D(fig)

lx= len(data[0])            # Work out matrix dimensions
ly= len(data[:,0])
xpos = np.arange(0,lx,1)    # Set up a mesh of positions
ypos = np.arange(0,ly,1)
xpos, ypos = np.meshgrid(xpos+0.5, ypos+0.5)

xpos = xpos.flatten()   # Convert positions to 1D array
ypos = ypos.flatten()
zpos = np.ones(lx*ly)*1e-10

dx = 1. * np.ones_like(zpos)
dy = dx.copy()
dz = data.flatten()

ax.bar3d(xpos,ypos,zpos, dx, dy, dz,  color='b', alpha=1., zsort='max')
plt.ion()
plt.show()

这篇关于Matplotlib - 3D条形图上的错误法线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:48