【问题标题】:How to plot pseudo-3d bar chart in matplotlib?如何在 matplotlib 中绘制伪 3d 条形图?
【发布时间】:2013-08-06 02:49:22
【问题描述】:

我想为我的老板准备一些统计数据。 matplotlib 条形图的扁平风格会让那些习惯于 Excel 图表的人看起来很便宜,但为了清楚起见,可能应该避免使用这样的风格。

我不是那么远,但我不知道如何给出合适的钢筋厚度:

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

row = [0, 0, 0, 22, 0, 0, 4, 16, 2, 0, 4, 4, 12, 26]
length = len(row)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.arange(length)
y = np.zeros(14)
z = np.array(row)
width = 0.8
ax.bar3d(x, y, [0]*length, 0.5, 0.001, z)
ax.set_xticks(x + width/2)
ax.set_xticklabels(titles[2:], rotation=90)
ax.set_yticks(y)
ax.set_zlabel('count')
plt.show()

结果:

【问题讨论】:

  • 你试过什么?最近有一个关于酒吧气缸的问题。与 Excel 的好奇比较,我有完全相反的反应。
  • 也许您可以使用this website 或this updated/additional information 中列出的步骤
  • Schorsch,谢谢,我去过那里,但代码显示非常不完整。 :(
  • 网络说服我使用这毕竟不是一个好主意:例如 github.com/propublica/guides/blob/master/news-apps.md “不惜一切代价避免 3-D 图表。”

标签: python 3d matplotlib bar-chart


【解决方案1】:

条形的粗细由ax.bar3d 中的dx, dy 参数设置,您拥有0.5, 0.001 的值。我确信您注意到的问题是,更改 dy 会更改条形的长度(在您的情况下为无标题轴),但 matplotlib 有助于重新调整 y 轴以便数据填充它。这让它看起来很奇怪(我假设这是问题所在,如果不是,抱歉)。

要解决这个问题,您可以使用ax.set_ylim(0, 0.002) 设置 y 限制(基本上您的 y 值从 0->0.001 变化)。如果您更改 dy 或为 bar3d 指定的 y 值(当前为 0),那么您将需要相应地更新限制。

例子:

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

row = [0, 0, 0, 22, 0, 0, 4, 16, 2, 0, 4, 4, 12, 26]
length = len(row)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.bar3d(range(length), [0]*length, [0]*length, 0.5, 0.001, row)

ax.set_ylim(-0.005, 0.005)
plt.show()

【讨论】:

  • 非常感谢!我不得不对此进行大量猜测。使 y 刻度实际上不可见也对我没有帮助。 ;)
猜你喜欢
  • 1970-01-01
  • 2016-08-17
  • 1970-01-01
  • 1970-01-01
  • 2020-02-28
  • 2015-08-08
  • 1970-01-01
  • 2017-06-30
  • 1970-01-01
相关资源
最近更新 更多