【问题标题】:X-axis labels not showing on bar plotX 轴标签未显示在条形图上
【发布时间】:2021-12-03 05:17:48
【问题描述】:

我的绘图仅在 x 轴上显示所有其他标签时遇到问题。

代码:

m_count=  [12, 12, 13, 16, 12, 12, 13, 16, 9, 10]
f_count =[13, 13, 12, 9, 13, 13, 11, 9, 15, 15]
    
labels = ["Capomulin", "Ceftamin", "Infubinol", "Ketapril", "Naftisol", "Placebo", "Propriva", "Ramicane", "Stelasyn", "Zoniferol"]
N=10
ind = np.arange(N)
width = .35
fig= plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.bar(ind, m_count, width, color ="b")
ax.bar(ind, f_count, width, color ="r", bottom=m_count)
ax.set_ylabel('Count')
ax.set_title("Count of Mice by Drug Regimen")
ax.set_xticks(ind, labels)
ax.set_xticklabels(labels, rotation=90 )
ax.set_yticks(np.arange(0, 30, 2))
ax.legend(labels=['Male', 'Female'])
plt.show()

这会导致:

【问题讨论】:

  • ax.set_xticks(ind, labels) 应该得到TypeError: set_ticks() takes 2 positional arguments but 3 were given,因为它不需要labels。更新到当前版本的 matplotlib (3.4.3),并使用 ax.set_xticks(ind),这应该可以解决问题。 Code and Plot。我投票关闭由错字引起的 as。

标签: python pandas matplotlib bar-chart


【解决方案1】:

我最快的解决方法是使用

plt.xticks(ind, labels)

但感谢特伦顿的 cmets,我想出了一个更新的解决方案:

ax.set_xticks(ind)
ax.set_xticklabels(labels)

【讨论】:

  • 成功了!确信我已经尝试过 plt 变体。
  • @TrentonMcKinney 谢谢,很高兴知道,请查看更新版本!
  • 这是一个更合适的答案。就像仅供参考一样,这类问题通常被认为是一个错字,并且有一个特定的接近投票。由拼写错误引起的问题通常对社区没有好处,因此这些问题通常会被关闭和删除。
猜你喜欢
  • 2017-09-26
  • 2019-07-15
  • 2019-01-28
  • 2021-07-24
  • 2020-03-31
  • 2018-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多