【发布时间】:2020-04-29 06:08:22
【问题描述】:
玩具问题:
import matplotlib.pyplot as plt
x = [10, 11, 12, 15]
y = [5, 7, 4, 3]
z = ["a", "b", "c", "d"]
fig, ax = plt.subplots(figsize = (6, 6))
ax.bar(x, y)
for i, j in enumerate(zip(y, z)):
plt.text(i + 10, j[0] - 0.3, s = str(j[0]), color = 'white')
plt.text(i + 10, j[0] + 0.1, s = str(j[1]), color = 'black')
plt.show()
这将创建一个图,将实际值作为文本标签,不错的东西。由于 x 值之间存在间隙,文本标签并未在每个条形上方对齐。
不用担心,可以插入一些数据:
import matplotlib.pyplot as plt
x = [10, 11, 12, 15]
y = [5, 7, 4, 3]
z = ["a", "b", "c", "d"]
x.insert(3, 13)
x.insert(4, 14)
y.insert(3, 0)
y.insert(4, 0)
z.insert(3, ' ')
z.insert(4, ' ')
fig, ax = plt.subplots(figsize = (6, 6))
ax.bar(x, y)
# ax.set_yscale('log')
for i, j in enumerate(zip(y, z)):
plt.text(i + 10, j[0] - 0.3, s = str(j[0]), color = 'white')
plt.text(i + 10, j[0] + 0.1, s = str(j[1]), color = 'black')
plt.show()
甜蜜,正是我们想要的,华丽的情节。
不过……
# ax.set_yscale('log')
如果 set_yscale('log') 未注释:
ValueError: Image size of 384x806494 pixels is too large. It must be less than 2^16 in each direction.
所以你不能在 y 值中有一个 0,因为 log 0 是未定义的。有道理。
除了现在,我无法解决原来的问题,因为 0 不能用于表示空条。
log 1 是 = 0,但是如果插入 1,仍然会显示一个 bar,因为我猜是什么原因?
matplotlib 版本“2.2.3”是否有任何解决方法? 0不能用,什么能用?
谢谢
【问题讨论】:
-
这段代码在现代 matplotlib 上使用对数刻度可以正常工作。
-
@JodyKlymak 这实际上很令人沮丧 x) 我无法更新 VM 上需要执行类似代码的任何内容..
-
你看使用
bar的bottom参数了吗?
标签: python matplotlib plot bar-chart