2020-12-08 matplotlib 训练

# matplotlib的官方简单条形图的例子

import numpy as np
import matplotlib.pyplot as plt

men_means, men_std = (20, 35, 30, 35, 27), (2, 3, 4, 1, 2)
women_means, women_std = (25, 32, 34, 20, 25), (3, 5, 2, 3, 3)

ind = np.arange(len(men_means))  # the x locations for the groups
width = 0.35  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(ind - width/2, men_means, width, yerr=men_std,
                color='SkyBlue', label='Men')
rects2 = ax.bar(ind + width/2, women_means, width, yerr=women_std,
                color='IndianRed', label='Women')

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind)
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))
ax.legend()


def autolabel(rects, xpos='center'):
    """
    Attach a text label above each bar in *rects*, displaying its height.

    *xpos* indicates which side to place the text w.r.t. the center of
    the bar. It can be one of the following {'center', 'right', 'left'}.
    """

    xpos = xpos.lower()  # normalize the case of the parameter
    ha = {'center': 'center', 'right': 'left', 'left': 'right'}
    offset = {'center': 0.5, 'right': 0.57, 'left': 0.43}  # x_txt = x + w*off

    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x() + rect.get_width()*offset[xpos], 1.01*height,
                '{}'.format(height), ha=ha[xpos], va='bottom')


autolabel(rects1, "left")
autolabel(rects2, "right")

plt.show()
Figure_1.png
#我画的
import numpy as np
import matplotlib.pyplot as plt

men_means = (20, 35, 30, 35, 27)
women_means = (40, 40, 40, 40, 40)

x_ticks = np.arange(5)
x_ticklabels = (('G1', 'G2', 'G3', 'G4','G5'))

width = 0.35

fig, ax = plt.subplots()

pic1 = ax.bar(x_ticks-width/2, men_means, width=width, label="Men")
pic2 = ax.bar(x_ticks+width/2, women_means, width=width, label="Women" )

ax.set_title('Score by group and gender')
ax.set_ylabel('Score')
ax.set_xlabel('Group')
ax.set_ylim((15,60))
ax.set_xticks(x_ticks)
ax.set_xticklabels(x_ticklabels)

for a, b, c in zip(x_ticks, men_means, women_means):
    ax.text(a-width/2, b+1, b, ha='center', va='bottom')
    ax.text(a+width/2, c+1, c, ha='center', va='bottom')
Figure_1.png
import numpy as np
import matplotlib.pyplot as plt

men = (183, 188, 177, 180, 185)
women = (165, 160, 167, 158, 162)
ind = np.arange(5)
m_label = ('m1', 'm2', 'm3', 'm4', 'm5')
w_label = ('w1', 'w2', 'w3', 'w4', 'w5')
g_label = ('g1', 'g2', 'g3', 'g4', 'g5')

fig, axes = plt.subplots(2,2)

# 这些值都是从0开始的
axes[0,0].bar(ind, men, color='blue')
axes[0,0].set_xticks(ind)
axes[0,0].set_xticklabels(m_label)
axes[0,0].set_ylim(0,200)

axes[0,1].bar(ind, women, color='red')
axes[0,1].set_xticks(ind)
axes[0,1].set_xticklabels(w_label)
axes[0,1].set_ylim(0,200)

wid = 0.35

axes[1,0].bar(ind-wid/2, men, width = wid, label="Men")
axes[1,0].bar(ind+wid/2, women, width = wid, label="Women")
axes[1,0].set_xticks(ind)
axes[1,0].set_xticklabels(g_label)
axes[1,0].set_xlabel("Gender")
axes[1,0].set_ylabel("Height")
axes[1,0].set_ylim(150,200)
axes[1,0].set_yticks((160,170,180,190,200))
axes[1,0].legend()


axes[1,1].barh(ind-wid/2, men, height = wid, label="Men")
axes[1,1].barh(ind+wid/2, women, height = wid, label="Women")
axes[1,1].set_yticks(ind)
axes[1,1].set_yticklabels(g_label)
axes[1,1].set_xlim(100,200)


for a, b, c in zip(ind, men, women):
    axes[1,1].text(b+2, a-wid/2, b)
    axes[1,1].text(c+2, a+wid/2, c)
Figure_1.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 08 | 现金水平:手里的钱是越多越好吗? 贾宁·财务思维课 你好,我是贾宁,欢迎来到我的《财务思维课》。 之前两...
    六安姐阅读 769评论 0 0
  • 今天,随着电商行业的不断发展,很多人都看到了电商的优势,纷纷的加入到电商行列,现在做电商真的是越来越难,尤其是电商...
    可爱到爆炸_05d8阅读 235评论 0 0
  • 中原焦点团队郑海燕坚持分享902天2020.12.8 369,读书约练 与时间赛跑 感觉现在的学习越来越有紧迫感了...
    晴岚85阅读 226评论 0 0
  • 久违的晴天,家长会。 家长大会开好到教室时,离放学已经没多少时间了。班主任说已经安排了三个家长分享经验。 放学铃声...
    飘雪儿5阅读 7,592评论 16 22
  • 今天感恩节哎,感谢一直在我身边的亲朋好友。感恩相遇!感恩不离不弃。 中午开了第一次的党会,身份的转变要...
    迷月闪星情阅读 10,620评论 0 11