簡體   English   中英

用python 3.7用matplotlib繪制條形圖

[英]plotting bar chart with matplotlib with python 3.7

我繪制了條形圖,我希望每個條形都有一個學生的名字。 這是我的代碼:

    import matplotlib.pyplot
    import numpy as np
    names=["avi","jose","bob","nick","zelda","mark"]
    pos=np.arange(6)+0.5
    matplotlib.pyplot.bar(pos,(4,8,12,3,17,6),align="center",color="red")
    matplotlib.pyplot.title("hight of students in unches",color="blue")
    matplotlib.pyplot.xlabel("hight in inches",color="red")
    matplotlib.pyplot.ylabel("students",color="red")
    matplotlib.pyplot.tick_params(axis="x",color="white")
    matplotlib.pyplot.tick_params(axis="y",color="white")
    matplotlib.pyplot.yticks(pos,(names))
    matplotlib.pyplot.show()

但是我一直把名字放在一邊而不是酒吧上方 在此處輸入圖片說明

感謝您的幫助:)|

docs中獲取示例代碼:

names=["avi","jose","bob","nick","zelda","mark"]
pos=np.arange(6)+0.5
bars = matplotlib.pyplot.bar(pos,(4,8,12,3,17,6),align="center",color="red")
matplotlib.pyplot.title("hight of students in unches",color="blue")
matplotlib.pyplot.xlabel("hight in inches",color="red")
matplotlib.pyplot.ylabel("students",color="red")

for i,bar in enumerate(bars):
    matplotlib.pyplot.text(bar.get_x() + bar.get_width()/2., 1.05*bar.get_height(),
                names[i],ha='center', va='bottom')

matplotlib.pyplot.tick_params(axis="x",color="white")
matplotlib.pyplot.tick_params(axis="y",color="white")
matplotlib.pyplot.show()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM