2020-12-08 matplotlib 訓(xùn)練

# matplotlib的官方簡(jiǎn)單條形圖的例子

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
#我畫(huà)的
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開(kāi)始的
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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 08 | 現(xiàn)金水平:手里的錢(qián)是越多越好嗎? 賈寧·財(cái)務(wù)思維課 你好,我是賈寧,歡迎來(lái)到我的《財(cái)務(wù)思維課》。 之前兩...
    六安姐閱讀 890評(píng)論 0 0
  • 今天,隨著電商行業(yè)的不斷發(fā)展,很多人都看到了電商的優(yōu)勢(shì),紛紛的加入到電商行列,現(xiàn)在做電商真的是越來(lái)越難,尤其是電商...
    可愛(ài)到爆炸_05d8閱讀 253評(píng)論 0 0
  • 中原焦點(diǎn)團(tuán)隊(duì)鄭海燕堅(jiān)持分享902天2020.12.8 369,讀書(shū)約練 與時(shí)間賽跑 感覺(jué)現(xiàn)在的學(xué)習(xí)越來(lái)越有緊迫感了...
    晴嵐85閱讀 241評(píng)論 0 0
  • 久違的晴天,家長(zhǎng)會(huì)。 家長(zhǎng)大會(huì)開(kāi)好到教室時(shí),離放學(xué)已經(jīng)沒(méi)多少時(shí)間了。班主任說(shuō)已經(jīng)安排了三個(gè)家長(zhǎng)分享經(jīng)驗(yàn)。 放學(xué)鈴聲...
    飄雪兒5閱讀 7,826評(píng)論 16 22
  • 今天感恩節(jié)哎,感謝一直在我身邊的親朋好友。感恩相遇!感恩不離不棄。 中午開(kāi)了第一次的黨會(huì),身份的轉(zhuǎn)變要...
    余生動(dòng)聽(tīng)閱讀 10,876評(píng)論 0 11

友情鏈接更多精彩內(nèi)容