国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Matplotlib數(shù)據(jù)可視化:柱狀圖與直方圖

柱狀圖和直方圖是兩種非常類似的統(tǒng)計圖,區(qū)別在于:

  • 直方圖展示數(shù)據(jù)的分布,柱狀圖比較數(shù)據(jù)的大小。

  • 直方圖X軸為定量數(shù)據(jù),柱狀圖X軸為分類數(shù)據(jù)。因此,直方圖上的每個條形都是不可移動的,X軸上的區(qū)間是連續(xù)的、固定的。而柱狀圖上的每個條形是可以隨意排序的,有的情況下需要按照分類數(shù)據(jù)的名稱排列,有的則需要按照數(shù)值的大小排列。

  • 直方圖柱子無間隔,柱狀圖條形有間隔

  • 直方圖條形寬度可不一,柱狀圖條形寬度須一致。柱狀圖條形的寬度因為沒有數(shù)值含義,所以寬度必須一致。但是在直方圖中,條形的寬度代表了區(qū)間的長度,根據(jù)區(qū)間的不同,條形的寬度可以不同,但理論上應(yīng)為單位長度的倍數(shù)。

本文將介紹matplotlib中柱狀圖和直方圖的作圖方法。

from matplotlib import pyplot as plt
import numpy as np
import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['SimHei']  # 中文字體支持

1 bar()與barh()

matplotlib中提供了bar()和barh()兩種方法畫柱狀圖,bar()用來畫垂直柱狀圖,barh()畫水平柱狀圖,兩者參數(shù)大同小異,如下所示:

2 垂直柱狀圖與水平柱狀圖

value= np.arange(6) ** 2
category = range(len(value))

fig = plt.figure(figsize=(84))

# 垂直柱狀圖
ax1 = fig.add_subplot(121)
ax1.set_title('圖1 垂直柱狀圖')
ax1.bar(x=category, height=value)

# 垂直柱狀圖
ax2 = fig.add_subplot(122)
ax2.set_title('圖2 水平柱狀圖')
ax2.barh(y=category, width=value)  # 注意這里參數(shù)名和值的傳遞與bar()不同

plt.show()

3 顏色、透明度與邊框

value= np.arange(6) ** 2
category = range(len(value))

fig = plt.figure(figsize=(84))

# 垂直柱狀圖
ax1 = fig.add_subplot(121)
ax1.set_title('圖1 垂直柱狀圖')
ax1.bar(x=category, height=value, 
        alpha=0.5,  # 透明度
        width=0.5,   # 每個條形的寬度
        color='yellow',  # 填充前景色
        edgecolor='red',  # 邊框顏色
        linewidth=3  # 邊框?qū)挾?/span>
       )

# 垂直柱狀圖
ax2 = fig.add_subplot(122)
ax2.set_title('圖2 水平柱狀圖')
ax2.barh(y=category, width=value,
         alpha=1,  # 透明度
         height=0.8,   # 每個條形的寬度
        color=['green''red''yellow''blue''grey''magenta'],  # 填充前景色
         linewidth=3  # 不顯示邊框
       )

plt.show()

4 刻度標(biāo)簽

value= np.arange(6) ** 2
category = range(len(value))

fig = plt.figure(figsize=(84))

# 垂直柱狀圖
ax1 = fig.add_subplot(121)
ax1.set_title('圖1 垂直柱狀圖')
ax1.bar(x=category, height=value, 
        tick_label='類別'
       )

# 垂直柱狀圖
ax2 = fig.add_subplot(122)
ax2.set_title('圖2 水平柱狀圖')
ax2.barh(y=category, width=value,
         tick_label=['類1''類2''類3''類4''類5''類6']
       )

plt.show()

5 添加誤差線

means = (2035303527)  # 各組平均分
std = (23412)  # 組各標(biāo)準(zhǔn)差
label = ('第一組''第二組''第三種''第四組''第五組')
bar_width = 0.4
bar_x = np.arange(len(label)) 


fig = plt.figure(figsize=(84))


ax1 = fig.add_subplot(121)
bar1 = ax1.bar(x=bar_x, height=means, width=bar_width, color='green',
              yerr=std,  # 添加誤差線
              ecolor='red',  # 誤差線顏色
              capsize=5,  # 兩端線段長短
               tick_label=label
             )

ax2 = fig.add_subplot(122)
bar2 = ax2.barh(y=bar_x, width=means, height=bar_width, color='green',
              xerr=std,  # 添加誤差線
              ecolor='red',  # 誤差線顏色
              capsize=5,  # 兩端線段長短
              tick_label=label
             )

plt.show()

6 添加數(shù)據(jù)標(biāo)注

means = (2035303527)  # 各組平均分
std = (23412)  # 組各標(biāo)準(zhǔn)差
label = ('第一組''第二組''第三種''第四組''第五組')
bar_width = 0.5
bar_x = np.arange(len(label)) 


fig = plt.figure(figsize=(104),tight_layout=True)


ax1 = fig.add_subplot(121)
bar1 = ax1.bar(x=bar_x, height=means, width=bar_width, color='green', tick_label=label
             )
for b in bar1:
        height = b.get_height()
        ax1.annotate('{}'.format(height),
                    xy=(b.get_x() + b.get_width() / 2, height),
                    xytext=(03),  # 3 points vertical offset
                    textcoords="offset points",color='red',
                    ha='center', va='bottom')

ax2 = fig.add_subplot(122)
bar2 = ax2.barh(y=bar_x, width=means, height=bar_width, color='green', tick_label=label
             )

for b in bar2:
        width = b.get_width()
        ax2.annotate('{}'.format(width),
                    xy=(width, b.get_y() + b.get_height() / 2),
                    xytext=(03),  # 3 points vertical offset
                    textcoords="offset points",color='red',
                    ha='left', va='center')

plt.show()

7 分組柱狀圖

menMeans = (2035303527)  # 男生各組平均分
womenMeans = (2532342025)# 女生各組平均分
menStd = (23412)  # 男生組各標(biāo)準(zhǔn)差
womenStd = (35233# 女生組各標(biāo)準(zhǔn)差
label = ('第一組''第二組''第三種''第四組''第五組')
bar_width = 0.4
bar_x = np.arange(len(label)) 


fig = plt.figure(figsize=(84))


ax = fig.add_subplot(111)
ax.set_title('圖1 垂直柱狀圖')
bar1 = ax.bar(x=bar_x - bar_width/2,   # 設(shè)置不同的x起始位置
              height=menMeans, width=bar_width)
bar2 = ax.bar(x=bar_x + bar_width/2,   # 設(shè)置不同的x起始位置
              height=womenMeans, width=bar_width,
        )

ax.set_xlabel('組別')
ax.set_ylabel('分數(shù)')
ax.set_title('各組不同性別分數(shù)')
ax.set_xticks(range(5))
ax.set_xticklabels(label)
ax.set_yticklabels(np.arange(08110))
ax.legend((bar1, bar2), ('男生''女生'))

plt.show()

8 堆疊柱狀圖

menMeans = (2035303527)  # 男生各組平均分
womenMeans = (2532342025)# 女生各組平均分
menStd = (23412)  # 男生組各標(biāo)準(zhǔn)差
womenStd = (35233# 女生組各標(biāo)準(zhǔn)差
label = ('第一組''第二組''第三種''第四組''第五組')
bar_width = 0.4
bar_x = np.arange(len(label)) 

fig = plt.figure(figsize=(84))


ax = fig.add_subplot(111)
ax.set_title('圖1 垂直柱狀圖')
bar1 = ax.bar(x=bar_x, height=menMeans, width=bar_width)
bar2 = ax.bar(x=bar_x, height=womenMeans, width=bar_width,
        bottom=menMeans # 通過bottom參數(shù)設(shè)置起始位置, 起始位置就是下半部分(bar1)條形的高度
        )

ax.set_xlabel('組別')
ax.set_ylabel('分數(shù)')
ax.set_title('各組不同性別分數(shù)')
ax.set_xticks(range(5))
ax.set_xticklabels(label)
ax.set_yticklabels(np.arange(08110))
ax.legend((bar1, bar2), ('男生''女生'))

plt.show()

9 直方圖

直方圖的繪制是通過hist()方法完成。hist()方法參數(shù)很多,來看看主要的參數(shù):

data_x = [13198125131124139131117128108135138131102107114119128121142127130124,
   10111011611711012812811599136126134951381171117813212411315011011786,
   951441051261301261301261161231061121381238610199136123117119105137,
   123128125104109134125127105120107129116108132103136118102120114105115,
   13214511912111213912513810913213415610611712714413913911914083110102,
   123107143115136118139123112118125109119133112114122109106123116131127,
   11511811213511514613711610314483123111110111100154136100118119133134,
   106129126110111109141120117106149122122110118127121114125126114140103,
   1301411171061141211141331379212111214697137105981171128197139113134,
   1061441101371371111041171001111011101051291371121201131331128394146,
   13310113111611184137115122106144109123116111111133150]

fig = plt.figure(figsize=(84))

ax1 = fig.add_subplot(121)
hists1 = ax1.hist(x=data_x, bins=5)  # 等距劃分

ax2 = fig.add_subplot(122)
hists2 = ax2.hist(x=data_x,bins=[78,90,100,120,140,145,150])

plt.show()

hist()方法將會返回一個包含三個元素的數(shù)組,第一個元素為每個條形區(qū)間中元素的數(shù)量,第二個元素為區(qū)間的邊界,第三個元素為Patch實例化對象。

hists1
(array([ 9., 49., 97., 77., 18.]),
array([ 78. , 93.6, 109.2, 124.8, 140.4, 156. ]),
<a list of 5 Patch objects>)

作者:奧辰
Github:https://github.com/ChenHuabin321
https://www.cnblogs.com/chenhuabin


加入機器學(xué)習(xí)微信群
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Matplotlib柱狀圖(代碼+注釋詳解)
phython+opencv圖像處理
matplotlib.pyplot中繪畫操作
matplotlib命令與格式:圖像(figure)與子區(qū)域(axes)布局與規(guī)劃
matplotlib繪圖入門詳解
干貨|教你一文掌握:Matplotlib+Seaborn可視化
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服