下面是我正在编写的创建对数条形图的代码
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
谢谢
发布于 2017-04-28 15:19:50
在matplotlib 2.0中,轴的边缘可能有未显示的刻度线。为了安全起见,除了刻度标签之外,您还可以设置刻度位置,
ax.set_xticks(np.arange(len(x)))
ax.set_xticklabels(x, rotation = 45)如果标签被旋转,您可能还希望将标签设置为与其右边缘对齐:
ax.set_xticklabels(x, rotation = 45, ha="right")发布于 2017-04-28 15:19:25
是的,我也同意这有点奇怪。无论如何,这里有一个解决方法(只需在前面定义xtick)。
import matplotlib.pyplot as plt
import numpy as np
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]
fig = plt.figure(figsize = (12,6))
ax = fig.add_subplot(111)
ax.bar(np.arange(len(x)),y, log=1)
ax.set_xticks(np.arange(len(x)))
ax.set_xticklabels(x, rotation = 45, zorder=100)
fig.show()

发布于 2017-04-28 15:19:22
set_xticklabels()将设置显示的文本refer to this。所以像这样修改应该是可行的:
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]
pos = np.arange(len(x))
ax.bar(pos,y, log=1)
ax.set_xticks(pos)
ax.set_xticklabels(x, rotation = 45)https://stackoverflow.com/questions/43673659
复制相似问题