如何使用 matplot lib 调整 3D 条形图的条宽
How to adjust bar's widht of 3D bar graph using matplot lib
如何调整条形图条形的宽度。此 post 附上了一张图片,显示了实际情况。我们可以看到标签,显示年份,从 2011 年到 2015 年。
我正在寻找一种解决方案来减少沿 yaxis 的各个条的宽度。我尝试了许多随机解决方案,这些解决方案可以在网上找到,但没有任何效果。
给定的图像实际上是 3D 图形,但由于视图清晰,仅显示了 2D 图片。
请帮忙:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
data = np.array([[0,6,3,0,0,0,0,0,0,0,0,2,2,3,0,2,2,3,0,0,0,0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,0,2,2,0,0,0,0,0,0,0,2],
[3,3,5,3,3,0,0,0,0,0,0,0,0,0,3,4,0,0,4,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,2,0,2,0,0],
[0,0,7,5,0,10,5,0,0,0,2,0,0,0,0,0,0,0,2,0,0,0,2,4,3,3,2,0,0,0,0,0,3,2,3,2,3,0,0,0,0,0,0,0,0,3,0],
[0,0,0,4,0,2,3,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,0,0],
[5,0,2,2,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,5,2,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0], ])
#data = data.transpose()
row_names = ['Ch','Bi','Ba','Ho','Ja','Pu','N','Ga','Ke','Ma','Me','Ti','Ka','Th','Di','Wa','Ha','My','Tu','Ma','Bi','Ut Ka','Ra','Da','Ch','Ko','B','Bid','Gu','D&N','Ra','Pu','San','Dhu','Jal','Gon','Gand','Alw','Jal','SAS','Gur','Pat','Hos','Lu','Pan','Kur','Jan']
column_names = ["2011","2012","2013","2014","2015"]
fig = plt.figure()
ax = Axes3D(fig)
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()
ypos = ypos.flatten()
zpos = np.ones(lx*ly)*1e-10
dx = 1. * np.ones_like(zpos)
dy = dx.copy()
dz = data.flatten()
print dz
cs = ['r', 'g', 'b', 'y', 'c'] * ly
values = np.linspace(0.2, 1., xpos.ravel().shape[0])
colors = cm.rainbow(values)
ticksx = np.arange(1, len(row_names)+1,1)
plt.xticks(ticksx, row_names)
z_data = np.random.rand(0,10)
ticksy = np.arange(0.5, len(column_names),1)
plt.yticks(ticksy, column_names)
#ax.tick_params(width=2, colors='r')
ax.w_xaxis.set_ticklabels(row_names,rotation=90,size=12)
ax.w_yaxis.set_ticklabels(column_names,rotation=40,size=12)
ax.bar3d(xpos,ypos,zpos, dx, dy, dz, alpha=0.7, zsort='max',color=colors)
plt.ion()
plt.show()
raw_input(" " )
更新:
将 "dx" 值减少 1 到 0.5,减少了条形宽度,但现在它显示了我想完全删除的条形之间的巨大填充。
最后更新:
我又附上了一张图片。我期待这种 3D 条形图,条宽减小(已完成),并且条之间的间隙最小。
呼叫签名或bar3d
是
ax.bar3d(xpos,ypos,zpos, dx, dy, dz,...)
其中 dx
和 dy
是条形的宽度和深度。因此,通过向 dx
提供不同的数组或数字来更改宽度。例如。要有 0.5 宽的条,请使用
ax.bar3d(..., dx=0.5, ...)
如果您想更改轴的宽度,您可以查看 。在此处实施此解决方案,
ax.bar3d(xpos,ypos,zpos, 1, 1, dz, alpha=0.7, zsort='max',color=colors)
ax.get_proj = lambda: np.dot(Axes3D.get_proj(ax), np.diag([1,0.4, 1, 1]))
会给出:
我正在寻找一种解决方案来减少沿 yaxis 的各个条的宽度。我尝试了许多随机解决方案,这些解决方案可以在网上找到,但没有任何效果。
给定的图像实际上是 3D 图形,但由于视图清晰,仅显示了 2D 图片。
请帮忙:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
data = np.array([[0,6,3,0,0,0,0,0,0,0,0,2,2,3,0,2,2,3,0,0,0,0,0,0,0,0,0,0,0,0,2,2,0,0,0,0,0,2,2,0,0,0,0,0,0,0,2],
[3,3,5,3,3,0,0,0,0,0,0,0,0,0,3,4,0,0,4,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,2,0,2,0,0],
[0,0,7,5,0,10,5,0,0,0,2,0,0,0,0,0,0,0,2,0,0,0,2,4,3,3,2,0,0,0,0,0,3,2,3,2,3,0,0,0,0,0,0,0,0,3,0],
[0,0,0,4,0,2,3,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,0,0],
[5,0,2,2,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,5,2,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0], ])
#data = data.transpose()
row_names = ['Ch','Bi','Ba','Ho','Ja','Pu','N','Ga','Ke','Ma','Me','Ti','Ka','Th','Di','Wa','Ha','My','Tu','Ma','Bi','Ut Ka','Ra','Da','Ch','Ko','B','Bid','Gu','D&N','Ra','Pu','San','Dhu','Jal','Gon','Gand','Alw','Jal','SAS','Gur','Pat','Hos','Lu','Pan','Kur','Jan']
column_names = ["2011","2012","2013","2014","2015"]
fig = plt.figure()
ax = Axes3D(fig)
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()
ypos = ypos.flatten()
zpos = np.ones(lx*ly)*1e-10
dx = 1. * np.ones_like(zpos)
dy = dx.copy()
dz = data.flatten()
print dz
cs = ['r', 'g', 'b', 'y', 'c'] * ly
values = np.linspace(0.2, 1., xpos.ravel().shape[0])
colors = cm.rainbow(values)
ticksx = np.arange(1, len(row_names)+1,1)
plt.xticks(ticksx, row_names)
z_data = np.random.rand(0,10)
ticksy = np.arange(0.5, len(column_names),1)
plt.yticks(ticksy, column_names)
#ax.tick_params(width=2, colors='r')
ax.w_xaxis.set_ticklabels(row_names,rotation=90,size=12)
ax.w_yaxis.set_ticklabels(column_names,rotation=40,size=12)
ax.bar3d(xpos,ypos,zpos, dx, dy, dz, alpha=0.7, zsort='max',color=colors)
plt.ion()
plt.show()
raw_input(" " )
更新: 将 "dx" 值减少 1 到 0.5,减少了条形宽度,但现在它显示了我想完全删除的条形之间的巨大填充。
最后更新: 我又附上了一张图片。我期待这种 3D 条形图,条宽减小(已完成),并且条之间的间隙最小。
呼叫签名或bar3d
是
ax.bar3d(xpos,ypos,zpos, dx, dy, dz,...)
其中 dx
和 dy
是条形的宽度和深度。因此,通过向 dx
提供不同的数组或数字来更改宽度。例如。要有 0.5 宽的条,请使用
ax.bar3d(..., dx=0.5, ...)
如果您想更改轴的宽度,您可以查看
ax.bar3d(xpos,ypos,zpos, 1, 1, dz, alpha=0.7, zsort='max',color=colors)
ax.get_proj = lambda: np.dot(Axes3D.get_proj(ax), np.diag([1,0.4, 1, 1]))
会给出: