这是我用来创建对数条形图的代码
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize = (12,6))
ax = fig.add_subplot(111)
x = ['Blue Whale', 'Killer Whale', 'Bluefin tuna', \
'Bottlenose dolphin', "Maui's dolphin", 'Flounder',\
'Starfish', 'Spongebob Squarepants']
y = [190000, 5987, 684, 650, 40, 6.8, 5, 0.02]
ax.bar(np.arange(len(x)),y, log=1)
ax.set_xticklabels(x, rotation = 45)
fig.savefig(filename = "f:/plot.png")
现在这是创建条形图,其中不显示第一个标签,即 Blue Whale
。这是我得到的情节 那么如何纠正呢? Matplotlib 版本为 2.0.0
,Numpy 版本为 1.12.1
谢谢
最佳答案
在 matplotlib 2.0 中,轴的边缘可能有未显示的刻度线。为了安全起见,除了刻度标签之外,您还可以设置刻度位置,
ax.set_xticks(np.arange(len(x)))
ax.set_xticklabels(x, rotation = 45)
如果旋转,您可能还想将标签设置为与其右边缘对齐:
ax.set_xticklabels(x, rotation = 45, ha="right")
关于python - matplotlib 未在条形图的 x 轴上显示第一个标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43673659/