matplotlib bar3d 裁剪问题 [英] matplotlib bar3d clipping problems

查看:44
本文介绍了matplotlib bar3d 裁剪问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Matplotlib 1.2.0 和 Python 2.7.3 创建 3D 条形图.我遵循了 http://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg19740.html 并逐个绘制了条形图,但是我仍然遇到渲染问题(即,条形图相互重叠).

I am trying to create a 3D bar graph with Matplotlib 1.2.0 and Python 2.7.3. I followed the advice in http://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg19740.html and plotted the bar one by one, but I am still getting rendering problems (i.e., bars on top of each other).

此外,当我调用我的代码时,我得到以下信息:

Moreover, I get the following when I invoke my code:

/usr/apps/python/lib/python2.7/site-packages/mpl_toolkits/mplot3d/axes3d.py:1476:RuntimeWarning:除法数为n时除以零]]

/usr/apps/python/lib/python2.7/site-packages/mpl_toolkits/mplot3d/axes3d.py:1476: RuntimeWarning: divide by zero encountered in divide for n in normals])

/usr/apps/python/lib/python2.7/site-packages/mpl_toolkits/mplot3d/axes3d.py:1476:RuntimeWarning:在正常情况下除以n时遇到无效值))

/usr/apps/python/lib/python2.7/site-packages/mpl_toolkits/mplot3d/axes3d.py:1476: RuntimeWarning: invalid value encountered in divide for n in normals])

我的问题:

  1. 这些严重警告吗?我是否需要调查他们并尝试消灭他们?如何消除它们?
  2. zsort='max' 和 zsort='average' 有什么区别?
  3. 我还能做些什么来消除渲染问题?

提前致谢!

这是我的代码:

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

# my data
dat = [2.31778665482167e-310, 0.006232785101850947, 0.0285075971030949, 0.0010248181570355695, 0.0048776795767614825, 0.02877090365176044, 0.002459331469834533, 0.0008594610645495889, 0.002919824084878003, 0.000968081117692596, 0.0, 0.0, 0.0319623949119874, 0.00568752311279771, 0.009994801469036968, 0.03248018520506219, 0.006686905726805326, 0.005987863156039365, 0.0072955095915350045, 0.005568911905473998, 0.0, 0.0, 0.0, 0.028483143996551524, 0.031030793902192794, 0.06125216053962635, 0.02935971973938871, 0.028507530280092265, 0.030112963748812088, 0.028293406731749605, 0.0, 0.0, 0.0, 0.0, 0.004510645022825792, 0.028998119822468988, 0.0013993630391143715, 0.0010726572949244424, 0.002288215944285159, 0.0006513973584945584, 0.0, 1.1625e-320, 1.15348834e-316, 2.3177866547513e-310, 0.0, 0.03148966953869102, 0.005215047563268979, 0.004491716298086729, 0.006010166308872446, 0.005186976949223524, 0.0, 0.0, 0.0, 0.0, 0.0, 1.107e-320, 0.02983657915729719, 0.028893006725328373, 0.030526067389954753, 0.028629390713739978, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0015217840289869456, 0.002751587509779179, 0.001413669523724954, 1.15348834e-316, 2.3177866547513e-310, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0024680339073824705, 0.0008254364860386303, 0.0, 0.0, 0.0, 9.965e-321, 1.15348834e-316, 2.3177866547513e-310, 0.0, 0.0, 0.0, 0.002621588539481613, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 9.41e-321, 1.15348834e-316, 2.3177866547513e-310]
dat = np.reshape(dat,[10,10],order='F')

lx = len(dat[0])
ly = len(dat[:,0])
n = lx*ly

# generate colors
cm = plt.get_cmap('jet')
vv = range(len(dat))
cNorm = colors.Normalize(vmin=0, vmax=vv[-1])
scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=cm)
colorVals = [scalarMap.to_rgba(i) for i in range(ly)]

# generate plot data
xpos = np.arange(0,lx,1)  
ypos = np.arange(0,ly,1)
xpos, ypos = np.meshgrid(xpos+0.25, ypos+0.25)
xpos = xpos.flatten()
ypos = ypos.flatten()
zpos = np.zeros(n)
dx = 0.5*np.ones_like(zpos)
dy = dx.copy()
dz = dat.flatten()
cc = np.tile(range(lx), (ly,1))
cc = cc.T.flatten()

# generate plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
opacity = 1

for i in range(n):
    ax.bar3d(xpos[i], ypos[i], zpos[i], dx[i], dy[i], dz[i],
             color=colorVals[cc[i]], alpha=opacity, zsort='max')

plt.autoscale(enable=True, axis='both', tight=True)
plt.grid()
plt.show(block=False)

推荐答案

这不是您要寻找的答案,但是我认为这可能是matplotlib中的错误.我认为同样的问题是在这里遇到的.根据

This isn't the answer that you are looking for, but I think that this might be a bug in matplotlib. I think that the same problem was encountered here. The problem was described as "intractable" according to the mplot3d FAQ.

但是在我看来,这似乎并不棘手.您只需确定哪个对象更靠近查看者并相应地设置 z 顺序.因此,我认为问题可能只是一个错误.

But to me it doesn't seem intractable. You simple need to figure out which object is closer to the viewer and set the z-order accordingly. So, I think that the problem might just be a bug.

如果我使用matplotlib 3D 直方图示例并仅更改"bins = 4"到bins=6"或更高的数字,然后我得到相同的axes3d.py:1476:RuntimeWarning:在divide/for n in normals中遇到无效值])".此外,我可以重现条形的错误 z 顺序(看看前面附近的高个子跳到他矮个子朋友面前的人):

If I take the matplotlib 3D histogram example and just change "bins=4" to "bins=6" or a higher number, then I get the same "axes3d.py:1476: RuntimeWarning: invalid value encountered in divide / for n in normals])". Also, I can reproduce the wrong z-order of the bars (check out the tall guy near the front who jumps in front of his short friend):

条形图的错误排序似乎与除以零误差有关,因为当我使用较少数量的箱时,这些图看起来很好.

The incorrect ordering of the bars seems linked to the divide by zero error, since the plots look just fine when I use a smaller number of bins.

axes.py中的1476行是:

Line 1476 in axes.py is:

shade = np.array([np.dot(n / proj3d.mod(n), [-1, -1, 0.5]) for n in normals])

基本上,我认为它正在尝试使用每个面的法线向量来找出阴影.但是,一个或多个法向向量为零,事实并非如此.所以,我认为这只是 matplotlib 中的一些错误,可能可以由比我拥有更多编程技能的人修复.

Basically, I think it is trying to figure out the shading using the normal vectors to each face. But, one or more of the normal vectors is zero, which should not be the case. So, I think that this is just some bug in matplotlib that can probably be fixed by someone with more programming skills than myself.

mplot3d FAQ 是正确的,如果您想要更好的 3D 引擎,可以使用 MayaVI.我用过

The mplot3d FAQ is correct that MayaVI can be used if you want a better 3D engine. I used

from mayavi import mlab
mlab.barchart(xpos,ypos,dz*100)

生成数据图:

我希望这很快就可以解决.我想在不久的将来制作一些类似的3D条形图.

I hope that this gets figured out soon. I would like to make some similar 3D barcharts in the near future.

这篇关于matplotlib bar3d 裁剪问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆