【问题标题】:plotting bar chart with matplotlib with python 3.7使用matplotlib和python 3.7绘制条形图
【发布时间】:2019-01-31 01:54:19
【问题描述】:

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

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

但我一直在旁边而不是栏上方看到名字

感谢您的帮助:)|

【问题讨论】:

  • 因为您使用的是matplotlib.pyplot.yticks(pos,(names)),所以您会在y轴上看到名称

标签: python matplotlib bar-chart


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-03-10
    • 1970-01-01
    • 2018-05-18
    • 2019-06-24
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    相关资源
    最近更新 更多