【问题标题】:Change x-axis scale size in a bar graph更改条形图中的 x 轴刻度大小
【发布时间】:2021-10-29 17:47:16
【问题描述】:

对于一些数据集,这是我的代码

import matplotlib.pyplot as plt
year = []
relative_recurrence = []
f = open('relative recurrence plot.txt', 'r')
for row in f:
    row = row.split(' ')
    year.append(row[0])
    relative_recurrence.append(float(row[1]))
plt.bar(year, relative_recurrence, color = 'g', label = 'HILDCAAS')

生成这样的条形图:

x 轴上的值范围是 1975 年到 2017 年。y 轴上的值是一些十进制值。在绘图的 x 轴上,这些值是重叠的。我想将比例更改为 1975、1980、1985 等,以保持它们清晰可见。

需要的命令是什么?

我尝试了 xlim() 命令。但它没有用。

【问题讨论】:

    标签: python pandas numpy matplotlib


    【解决方案1】:

    试试这个:

    import matplotlib.pyplot as plt
    year = []
    relative_recurrence = []
    f = open('relative recurrence plot.txt','r')
    for row in f:
        row = row.split(' ')
        year.append(float(row[0]))
        relative_recurrence.append(float(row[1]))
    plt.bar(year, relative_recurrence,color = 'g', label = 'HILDCAAS')
    plt.xticks(np.arange(min(year), max(year) + 1, 5.0))
    

    【讨论】:

    • @AyushiNema 有帮助吗?
    • 我添加了上述命令plt.xticks(np.arange(min(year), max(year) + 1, 1)) 并得到错误:只能将str(不是“int”)连接到str
    • @AyushiNema 用完整代码编辑了我的答案,现在应该可以使用了
    • @AyushiNema 在哪一行?
    • @AyushiNema 显示完整的追溯
    【解决方案2】:

    自行更改 x 轴刻度

    plt.xticks([item for item in range (enter desired range number)])
    

    【讨论】:

    • 你说的“by own”是什么意思(好像不太明白)?请通过editing (changing) your answer 回复,而不是在 cmets 中(without "Edit:"、"Update:" 或类似的 - 答案应该看起来像是今天写的)。
    猜你喜欢
    • 2020-05-29
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多