【问题标题】:Creating Spaces for matplotlib graphs为 matplotlib 图创建空间
【发布时间】:2020-05-13 16:39:20
【问题描述】:

我的任务是根据每月的总降雨量制作图表,我编写了代码和图表,但我在 X 轴上有一个问题,它没有足够的空间让它可读。 我认为给 x 轴一个更大的值会有所帮助,但我想我错了

代码:

import matplotlib.pyplot as plt
Rainfall=[0]*12
i=0
print("Enter the Rainfall each month")
while i<len(Rainfall):
    print('Month #',i + 1, ': ',end=' ')
    Rainfall[i]=int(input())
    i+=1

total = sum(Rainfall)
ave=total/len(Rainfall)

High = max (Rainfall)
Low = min(Rainfall)


print("total Amount= ",total)
print("Average Average Amount {:0.2f}".format(ave))


print ("The months with the highest value are : ")
print ([i+1 for i, j in enumerate(Rainfall) if j == High])
print ("The months with the Lowest value are : ")
print ([i+1 for i, j in enumerate(Rainfall) if j == Low])

plt.plot([100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200],Rainfall,marker='.')
plt.xticks([100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200],['January ','Februrary','March','April','May','June','July','August','September','October',"November", "December"])
plt.xlabel('Month')
plt.ylabel('Rainfall')
plt.show()

输出:

Enter the Rainfall each month
Month # 1 :  1
Month # 2 :  2
Month # 3 :  3
Month # 4 :  4
Month # 5 :  5
Month # 6 :  6
Month # 7 :  7
Month # 8 :  8
Month # 9 :  10
Month # 10 :  12
Month # 11 :  13
Month # 12 :  14
total Amount=  85
Average Average Amount 7.08
The months with the highest value are : 
[12]
The months with the Lowest value are : 
[1]

Process finished with exit code 0

图表:

【问题讨论】:

    标签: python matplotlib graph


    【解决方案1】:

    尝试使用 rotation=90 将刻度标签旋转 90 度

    plt.xticks([100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200],
               ['January ','Februrary','March','April','May','June','July','August','September',
                'October',"November", "December"], rotation=90)
    

    【讨论】:

    • 对您表示最大的感谢,我什至没有想到这一点。非常感谢您的帮助
    • @TenebrisMortem :如果我的回答对您有所帮助,您应该阅读What should I do when someone answers my question?
    • 抱歉,我之前尝试过对我之前的问题进行投票,但网站说我需要特定级别,感谢您提供的信息^^
    • 嗯快速问题我如何才能对作为评论的答案进行投票?我目前正在投票并接受过去对我有帮助的答案
    • @TenebrisMortem :要给评论点赞,我认为你需要有更多的声望点。
    猜你喜欢
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 2019-01-04
    • 2023-02-09
    • 2011-02-08
    相关资源
    最近更新 更多