bar3d图中重叠错误 [英] Wrong overlap in bar3d plot

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

问题描述

我已经制作了这个3d条形图,但是在某些条形中发现了一个错误的重叠,如下图所示的绿色圆圈所示:

I've made this 3d bar plot, but I've found a wrong overlap in some bars, as I show in the image below with green circles:

该情节是由以下人员制作的:

The plot is made by:

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

fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(111, projection='3d')    
matrix = np.array([
[84 80 68 56 60 44 55 39 27 29]
[82 67 63 44 47 33 22 19  9  2]
[53 61 48 34  0 16  0  0  0  0]
[48 25  0  0  0  0  0  0  0  0]])

len_x, len_y = matrix.shape
_x = np.arange(len_x)
_y = np.arange(len_y)

xpos, ypos = np.meshgrid(_x, _y)
xpos = xpos.flatten('F')
ypos = ypos.flatten('F')
zpos = np.zeros_like(xpos)

dx = np.ones_like(zpos)
dy = dx.copy()
dz = matrix.flatten()

cmap=plt.cm.magma(plt.Normalize(0,100)(dz))

ax.bar3d(xpos+0.32, ypos-0.3, zpos, dx-0.6, dy-0.1, dz, zsort='max', color=cmap)

ax.set_xlabel('x')
ax.set_xticks(np.arange(len_x+1))
ax.set_xticklabels(['1000','500','100','50','0'])
ax.set_xlim(0,4)
ax.set_ylabel('y')
ax.set_yticks(np.arange(len_y+1))
ax.set_yticklabels(['0.5','1.','1.5','2.','2.5','3.','3.5','4.','4.5','5.'])
ax.set_ylim(-0.5,10)
ax.set_zlabel('z')
ax.set_zlim(0,100)
ax.view_init(ax.elev, ax.azim+100)

这是一个错误吗?为什么有些酒吧严重重叠?我正在使用matplotlib版本2.1.0和anaconda python 3.6.3

Is it a bug? Why some bars are badly overlapping? I'm using matplotlib version 2.1.0 and anaconda python 3.6.3

推荐答案

@DavidG在评论中指出,这是一个没有理想解决方案的问题:

As pointed out by @DavidG in the comments, this is an issue without an ideal solution:

我的3D图在某些视角下看起来不正确
这可能是mplot3d最常报告的问题.问题是从某些角度来看,一个3D对象将出现在另一个对象,即使它实际上在后面.这个可以导致绘制的图形看起来物理上不正确".

My 3D plot doesn’t look right at certain viewing angles
This is probably the most commonly reported issue with mplot3d. The problem is that – from some viewing angles – a 3D object would appear in front of another object, even though it is physically behind it. This can result in plots that do not look "physically correct."

不幸的是,虽然正在做一些工作以减少发生该工件的问题,目前是一个棘手的问题,无法解决完全解决,直到matplotlib在其支持3D图形渲染核心.
[来源]

Unfortunately, while some work is being done to reduce the occurrence of this artifact, it is currently an intractable problem, and can not be fully solved until matplotlib supports 3D graphics rendering at its core.
[Source]

但是,我可以通过处理图表的可视角度并减少条形之间的接触面积来大大减少此问题.

However, I was able to greatly reduce this issue by playing with the viewing angle of the plots and reducing the contact areas between the bars.

例如,为了更改视角(相机位置"),我使用了:

For example, to change the viewing angle (the "camera position"), I used:

ax.view_init(elev=30, azim=-60) # Changes the elevation and azimuth

如何使用python/matplotlib为3d图设置相机位置"的更多详细信息?

根据接触面积,这取决于您的情节.就我而言,所有条形图都沿y轴接触,因此我只是稍微减小了 dy 参数,以在条形图之间留一些间隙.

As per the contact areas, it depends on your plot. In my case, all the bars had contact alongside the y axis, so I just reduced the dy param a little to left some gap between the bars.

这篇关于bar3d图中重叠错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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