用python 3.7用matplotlib绘制条形图

时间:2018-08-24 14:45:48

标签: python matplotlib bar-chart

我画了一个条形图,我希望每个条形都有一个学生名。 这是我的代码:

    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()

但是我一直在侧面而不是在栏上方enter image description here

感谢您的帮助:)|

1 个答案:

答案 0 :(得分:0)

the 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()

enter image description here