【问题标题】:Aligning x axis with bars matplotlib将 x 轴与条形 matplotlib 对齐
【发布时间】:2019-06-23 13:15:02
【问题描述】:

我是 matplotlib 的新手。我试图制作一个简单的条形图,但是我似乎无法让轴与条形对齐。我尝试了多种宽度组合,但我确定我遗漏了一些东西。

labels, values = zip(*charfarm.items())
indexes = np.arange(len(labels))
width = 0.8
plt.bar(indexes, values, width)
plt.xticks(indexes + width, labels)
plt.show()

Graph

【问题讨论】:

  • 为什么要将宽度添加到刻度位置?删除该部分,它应该看起来不错。

标签: python matplotlib


【解决方案1】:

由于您在 plt.xticks 函数中的 indexes + width 操作,您的刻度被抵消了。

请参考以下代码:

import matplotlib.pyplot as plt
import numpy as np

plt.style.use('ggplot')

labels = ['A', 'B', 'C', 'D', 'E', 'F']
values = [10, 15, 12, 9, 7, 13]

indexes = np.arange(len(labels))
width = 0.8
plt.bar(indexes, values, width=width, color='green')
plt.title('Bar Chart')
plt.xticks(indexes, labels)
plt.show() 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多