91aaa在线国内观看,亚洲AV午夜福利精品一区二区,久久偷拍人视频,久久播这里有免费视播

<strong id="fvuar"></strong>

  • <sub id="fvuar"><dl id="fvuar"><em id="fvuar"></em></dl></sub>

    1. 千鋒教育-做有情懷、有良心、有品質(zhì)的職業(yè)教育機構(gòu)

      手機站
      千鋒教育

      千鋒學習站 | 隨時隨地免費學

      千鋒教育

      掃一掃進入千鋒手機站

      領(lǐng)取全套視頻
      千鋒教育

      關(guān)注千鋒學習站小程序
      隨時隨地免費學習課程

      當前位置:首頁  >  技術(shù)干貨  > 常見圖形繪制

      常見圖形繪制

      來源:千鋒教育
      發(fā)布人:qyf
      時間: 2022-08-12 17:42:30 1660297350

        為了能學會正確使用matplotlib進行繪制各種圖形,并對數(shù)據(jù)可視化有一個更深的了解,本篇文章給大家?guī)淼木褪菙?shù)據(jù)分析常見圖形繪制部分.

      截屏2021-09-14 下午3.39.22

        我們?nèi)匀粡腵函數(shù)功能`、`實例代碼`、`參數(shù)講解`、`效果演示`四個層面來介紹每一種統(tǒng)計圖,希望能給每個學習數(shù)據(jù)分析的小伙伴帶來幫助.

        #### bar()函數(shù)

        ##### 1.函數(shù)功能

        繪制柱狀圖,主要用來比較不同類別之間的數(shù)據(jù)情況。

        ##### 2. 參數(shù)詳解

        plt.bar(x,height,width=0.8,bottom=None,*, *align='center'*, *data=None*, **kwargs)

        - x:在什么位置顯示柱形圖;

        - height:每根柱子的高度;

        - width:每根柱子的寬度,可以一樣,也可以各不相同;

        - bottom:每根柱子底部位置,可以一樣,也可以各不相同;

        - align:柱子的位置與x值的關(guān)系,有center、edge可選;

        - color: 柱形的顏色

        - edgecolor:柱子邊緣顏色的設(shè)置

        - linewidth: 柱形邊緣線的線條寬度

        - tick_label:柱形的刻度標簽

        - hatch:表示刻度陰影類型主要有這些類型:`/`、`*`、`.`、`|`、`-`、`+`、`x`、`o`、`O`

        ##### 3.演示代碼

        import matplotlib as mpl

        import matplotlib.pyplot as plt

        plt.rcParams['font.sans-serif'] = ['Simhei']

        # 中文情況下 負號顯示會有異常 所以還需要設(shè)置負號的操作

        plt.rcParams['axes.unicode_minus'] = False

        x = ['a','b','c','d','e','f','g','h']

        y = np.random.randint(1,10,8)

        plt.bar(x,y,align='center',color='c',edgecolor='r',hatch='/')

        plt.xlabel('編號')

        plt.ylabel('滿意度')

        plt.show()

        ##### 4.效果

      截屏2021-09-14 下午3.53.03

        **錯位柱狀圖**

        import numpy as np

        import matplotlib.pyplot as plt

        # 準備數(shù)據(jù)

        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)

        # 計算數(shù)據(jù)個數(shù)

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

        # 添加一些文本標簽,標題,定制x軸的刻度等

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

      截屏2021-09-14 下午4.08.34

        **堆疊柱狀圖**

        import numpy as np

        import matplotlib.pyplot as plt

        # 準備數(shù)據(jù)

        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)

        # 計算數(shù)據(jù)個數(shù)

        ind = np.arange(len(men_means))

        # 定義柱形的寬度

        width = 0.35

        p1 = plt.bar(ind, menMeans, width, yerr=menStd)

        p2 = plt.bar(ind, womenMeans, width,bottom=menMeans, yerr=womenStd)

        plt.ylabel('Scores')

        plt.title('Scores by group and gender')

        plt.xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5'))

        plt.yticks(np.arange(0, 81, 10))

        plt.legend((p1[0], p2[0]), ('Men', 'Women'))

        plt.show()

      截屏2021-09-14 下午4.08.50

        現(xiàn)有部分租房數(shù)據(jù),給大家過濾出兩列,分別是price 和 date,分析每年的租房均值

        import pandas as pd

        listdf=pd.read_excel('listings.xlsx')

        data = listdf[['price','date']].dropna()

        data['year'] = data['date'].dt.year

        r = data.groupby(by='year').mean()

        plt.bar(r.index,r.values.reshape(len(r)),alpha=0.6,width = 0.8, facecolor = 'deeppink', edgecolor = 'green', lw=1, label='租房分析')

        plt.legend(loc=2)

        plt.show()

        效果:

      截屏2021-09-14 下午4.48.48

        **刻度傾斜**

        ...

        plt.bar(r.index,r.values.reshape(len(r)),alpha=0.6,width = 0.8, facecolor = 'deeppink', edgecolor = 'green', lw=1, label='租房分析')

        plt.legend(loc=2)

        plt.xticks(rotation=45)

        plt.show()

        ```

        效果:

      截屏2021-09-14 下午4.56.54

        **添加數(shù)據(jù)標簽**

        y = r.values.reshape(len(r))

        b = plt.bar(r.index,y,alpha=0.6,width = 0.8, facecolor = 'deeppink', edgecolor = 'green', lw=1, label='租房分析')

        # 添加數(shù)據(jù)標簽 就是矩形上面的數(shù)值

        def add_labels(rects):

        for rect in rects:

        height = rect.get_height()

        plt.text(rect.get_x() + rect.get_width()/2, height, height, ha='center', va='bottom')

        rect.set_edgecolor('white')

        add_labels(b)

        plt.legend(loc=2)

        plt.xticks(rotation=45)

        plt.show()

      截屏2021-09-14 下午5.13.07

        ### barh()函數(shù)

        ##### 1.函數(shù)功能

        繪制條形圖

        ##### 2. 參數(shù)詳解

        繪制條形圖`plt.barh(x,y)`

        - x:在y軸上顯示的類別

        - y:各個類別的數(shù)量值

        ##### . 實例代碼

        import matplotlib as mpl

        import matplotlib.pyplot as plt

        mpl.rcParams['font.sans-serif'] = ['SimHei']

        x = [1,2,3,4,5,6,7,8]

        y = [2,3,4,9,1,2,6,4]

        plt.barh(x,y,tick_label=['a','b','c','d','e','f','g','h'],color='m')

        plt.xlabel('評分')

        plt.ylabel('編號')

        plt.show()

        ##### 4. 效果演示

      截屏2021-09-14 下午5.25.32

        ### hist()函數(shù)

        ##### 1.函數(shù)功能

        繪制直方圖

        **2.參數(shù)詳解**

        繪制直方圖`plt.hist(x,bins,color,alpha)`

        - x:數(shù)據(jù)集,直方圖會對該數(shù)據(jù)集的大小按區(qū)間進行歸類

        - bins:數(shù)據(jù)集的分隔區(qū)間

        - color:直方圖的顏色

        - alpha:直方圖顏色的透明度

        - rwidth: 柱子之間的距離

        ##### 3. 實例代碼

        import matplotlib as mpl

        import matplotlib.pyplot as plt

        import numpy as np

        mpl.rcParams['font.sans-serif'] = ['SimHei']

        x = np.random.randint(0,100,100) # 生成范圍在【0~100】之間100個數(shù)據(jù)

        bins = np.arange(0,101,10) # 生成數(shù)組[0 10 20 ... 100],里面是間隔為10的十個數(shù)

        plt.hist(x,bins,color='m',alpha=0.5,rwidth=0.8)

        plt.xlabel('分數(shù)段')

        plt.ylabel('人數(shù)')

        plt.title("各分數(shù)段人數(shù)分布")

        plt.show()

        > 直方圖與柱形圖相似但不同,直方圖表示的是離散型數(shù)值的區(qū)間分布情況;更多關(guān)于直方圖hist的教程請參考官方文檔。

        > range與arange的區(qū)別:

        > arange函數(shù)返回的是numpy里定義的數(shù)組,數(shù)組每一個元素的數(shù)據(jù)類型一致。range在Python2與Python3里有著不同的功能。Python2里的range返回的是列表,而Python3里的range返回的是可迭代的對象,通常使用for循環(huán)將其輸出。

        ##### 4. 效果演示

      截屏2021-09-14 下午5.34.18

        ### pie()函數(shù)

        ##### 1.函數(shù)功能

        繪制餅圖,顯示不同類別所占百分比。

        **2.參數(shù)說明:**

        繪制餅圖`plt.pie(x,explode,labels,autopct,startangle)`

        - x:每一塊的比例,如果sum(x)>1,會對sum(x)進行歸一化操作。

        - explode:每一塊離開中心的距離

        - labels:每一塊外側(cè)顯示的標簽文字

        - autopct:控制餅圖百分比設(shè)置,可以使用format字符串表示,`%1.1f%%`小數(shù)點前后各一位(沒有用空格補齊)

        - startangle:起始繪制角度,默認從x軸正方向逆時針畫起,若設(shè)定90度則從y軸正方向畫起。

        ##### 3. 實例代碼

        import matplotlib as mpl

        import matplotlib.pyplot as plt

        import numpy as np

        mpl.rcParams['font.sans-serif'] = ['SimHei']

        labels = ['房貸','育兒','飲食','交通','娛樂','其它']

        sizes = [5,1,2,0.5,0.8,1.5]

        explode = (0,0.1,0,0,0,0)

        plt.pie(x=sizes,explode=explode,labels=labels,autopct='%1.1f%%',startangle=150)

        plt.title("餅圖-家庭支出情況")

        plt.axis('equal')

        # 添加圖例

        plt.legend(loc="upper right",fontsize=10,bbox_to_anchor=(1.1,1.05),borderaxespad=0.3)

        plt.show()

        ##### 4. 效果演示

      截屏2021-09-14 下午5.51.05

        ### scatter()函數(shù)

        ##### 1.函數(shù)功能

        用于繪制氣泡圖,二維數(shù)據(jù)借助氣泡大小展示三維數(shù)據(jù)。

        ##### **2.參數(shù)說明:**

        繪制氣泡圖:`plt.scatter(a,b,c,s,cmap)`

        - a:x軸上的離散數(shù)值,固定長度的數(shù)組。

        - b:y軸上的離散數(shù)值,固定長度的數(shù)組。

        - c:氣泡的顏色,可以是固定顏色也可以是一個數(shù)組。

        - s:氣泡的大小,用于記錄第三維度的函數(shù)關(guān)系。

        - cmap:顏色映射表,可以簡單理解成配色方案。

        ##### 3. 實例代碼

        import matplotlib as mpl

        import matplotlib.pyplot as plt

        import numpy as np

        mpl.rcParams['font.sans-serif'] = ['SimHei'] # 顯示中文

        mpl.rcParams['axes.unicode_minus'] = False # 為了坐標軸負號正常顯示

        a = np.random.randn(100)

        b = np.random.randn(100)

        plt.style.use('ggplot') # 設(shè)置繪圖風格

        plt.scatter(a,b,c=np.random.rand(100),cmap='jet',s=100*(a**2+b**2),alpha=0.7)

        plt.colorbar()

        plt.title('氣泡圖')

        plt.show()

        > matplotlib默認不支持中文,設(shè)置中文字體后,負號會顯示異常。需要手動將坐標軸負號設(shè)為False才能正常顯示負號。

        ##### 4. 效果演示

      截屏2021-09-14 下午5.48.04

        繪制不同顏色的散點圖

        colrs = ['red', 'blue','yellow']

        labels = ['red', 'blue','yellow']

        d1 = np.random.randn(10,3) # 10行,2列的ndarray,一行代表一個點

        d2 = np.array([0,1,0,1,1,1,0,1,0,0])

        # 循環(huán)的目的是區(qū)分,循環(huán)一次就畫一種類的所有點

        # d2==0時,點的顏色是red,圖例是red

        # d2==1時,點的顏色是blue,圖例是blue

        for i in range(d1.shape[1]): # shape[1]的值是2,即便利兩次

        plt.scatter(d1[d2==i,0]

        ,d1[d2==i,1]

        ,s=50

        ,c=colrs[i]

        ,label = labels[i],alpha=0.4)

        plt.legend('散點圖')

        plt.show()

      截屏2021-09-14 下午5.58.31

        ### polar()函數(shù)

        ##### 1.函數(shù)功能

        繪制雷達圖(極線圖)

        ##### **2.參數(shù)說明:**

        繪制雷達圖`plt.polar(theta,r,marker)`

        - theta:在極坐標系下坐標點的角度

        - r:在極坐標系下坐標點與極點的距離

        - marker:定義各個點的樣式

        ##### 3. 實例代碼

        import matplotlib as mpl

        import matplotlib.pyplot as plt

        import numpy as np

        mpl.rcParams['font.sans-serif'] = ['SimHei']

        plt.style.use('ggplot') # 設(shè)置繪圖風格

        theta = np.array([0.25,0.75,1,1.5,0.25]) # 定義各個點的極角,注意最后要閉合

        r = [20,60,40,60,20] # 定義各個點極徑的長度

        plt.polar(theta*np.pi,r,'r-',lw=1,marker='o') # 設(shè)置雷達圖路徑,r-表示紅色實線

        plt.fill(theta*np.pi,r,c='c',alpha=0.4) # 填充雷達圖,課設(shè)置顏色與透明度

        plt.ylim(0,100) # 設(shè)置極坐標軸的范圍

        plt.title('雷達圖',fontsize=12)

        plt.show()

        ##### 4. 效果演示

      截屏2021-09-14 下午6.05.49

        參考示例 1:

        labels = np.array(['a','b','c','d','e','f']) # 標簽

        dataLenth = 6 # 數(shù)據(jù)長度

        data1 = np.random.randint(0,10,6)

        data2 = np.random.randint(0,10,6) # 數(shù)據(jù)

        angles = np.linspace(0, 2*np.pi, dataLenth, endpoint=False) # 分割圓周長

        data1 = np.concatenate((data1, [data1[0]])) # 閉合

        data2 = np.concatenate((data2, [data2[0]])) # 閉合

        angles = np.concatenate((angles, [angles[0]])) # 閉合

        plt.polar(angles, data1, 'o-', linewidth=1) #做極坐標系

        plt.fill(angles, data1, alpha=0.25)# 填充

        plt.polar(angles, data2, 'o-', linewidth=1) #做極坐標系

        plt.fill(angles, data2, alpha=0.25)# 填充

        plt.thetagrids(angles * 180/np.pi, labels) # 設(shè)置網(wǎng)格、標簽

        plt.ylim(0,10) # polar的極值設(shè)置為ylim

      截屏2021-09-14 下午6.15.06

        參考示例 2

        import numpy as np

        import matplotlib.pyplot as plt

        # 開始設(shè)定一些數(shù)據(jù)

        #標簽

        labels = np.array(['藝術(shù)A','調(diào)研I','實際R','常規(guī)C','企業(yè)E','社會S'])

        #數(shù)據(jù)個數(shù)

        dataLenth = 6

        #數(shù)據(jù)

        data = np.array([1,4,3,6,4,8])

        angles = np.linspace(0, 2*np.pi, dataLenth, endpoint=False)

        data = np.concatenate((data, [data[0]])) # 閉合

        angles = np.concatenate((angles, [angles[0]])) # 閉合

        fig = plt.figure()

        ax = fig.add_subplot(111, polar=True)# polar參數(shù)!!

        ax.plot(angles, data, 'bo-', linewidth=2)# 畫線

        ax.fill(angles, data, facecolor='r', alpha=0.25)# 填充

        ax.set_thetagrids(angles * 180/np.pi, labels, fontproperties="SimHei")

        ax.set_title("matplotlib雷達圖", va='bottom', fontproperties="SimHei")

        ax.set_rlim(0,10)

        ax.grid(True)

        plt.show()

      截屏2021-09-14 下午6.13.05

        ### stem()函數(shù)

        ##### 1.函數(shù)功能

        用于繪制棉棒圖

        ##### 2.參數(shù)說明

        繪制棉棒圖`plt.stem(x,y,linefmt,markerfmt,basefmt)`

        - x:指定x軸的位置

        - y:設(shè)置棉棒的長度

        - linefmt:棉棒的樣式

        - markerfmt:棉棒末端的樣式

        - basefmt:棉棒基線的樣式

        ##### 3.實例代碼

        import matplotlib as mpl

        import matplotlib.pyplot as plt

        import numpy as np

        mpl.rcParams['font.sans-serif'] = ['SimHei']

        mpl.rcParams['axes.unicode_minus'] = False

        x = np.linspace(0.5,2*np.pi,20)

        y = np.random.randn(20)

        plt.stem(x,y,linefmt='-.',markerfmt='o',basefmt='-')

        plt.title('棉棒圖')

        plt.show()

        ##### 4.效果演示

      截屏2021-09-14 下午6.18.22

        ### boxplot()函數(shù)

        ##### 1.函數(shù)功能

        用于繪制箱線圖

        ##### 2.參數(shù)說明:

        繪制箱線圖`plt.boxplot(x,labels)`

        - x:輸入的數(shù)據(jù)

        - label:圖例

        ##### 3.實例代碼

        import matplotlib as mpl

        import matplotlib.pyplot as plt

        import numpy as np

        mpl.rcParams['font.sans-serif'] = ['SimHei']

        mpl.rcParams['axes.unicode_minus'] = False

        x1 = np.random.randn(100)

        x2 = np.random.randn(100)

        x3 = np.random.randn(100)

        labels = ['第一','第二','第三']

        plt.boxplot([x1,x2,x3],labels=labels)

        plt.grid(axis='y',ls=':',lw=1,c='g',alpha=0.4)

        plt.title('箱線圖')

        plt.show()

        ##### 4.效果演示

      截屏2021-09-14 下午6.19.32

        ### errotbar()函數(shù)

        ##### 1.函數(shù)功能

        用于繪制誤差棒圖

        ##### 2.參數(shù)說明

        繪制誤差棒圖`plt.errorbar(x,y,fmt,yerr,xerr,ecolor,mfc,mec,capthick,capsize)`

        - x:數(shù)據(jù)點的水平位置

        - y:數(shù)據(jù)點的垂直位置

        - fmt:數(shù)據(jù)點的標記樣式和數(shù)據(jù)點標記的連接線樣式

        - xerr:x軸方向數(shù)據(jù)點的誤差計算方法

        - yerr:y軸方向數(shù)據(jù)誤差點的計算方法

        - ecolor:誤差棒的顏色

        - mfc:數(shù)據(jù)點的標記顏色

        - mec:數(shù)據(jù)點標記邊緣顏色

        - capthick:誤差棒邊界橫杠的厚度

        - capsize:誤差棒邊界橫杠的大小

        ##### 3.實例代碼

        import matplotlib as mpl

        import matplotlib.pyplot as plt

        import numpy as np

        mpl.rcParams['font.sans-serif'] = ['SimHei']

        mpl.rcParams['axes.unicode_minus'] = False

        x = np.linspace(0.1,0.6,6)

        y = np.exp(x)

        plt.errorbar(x,y,fmt='o:',yerr=0.2,xerr=0.02,ecolor='g',mfc='c',mec='r',capthick=2,capsize=3)

        plt.xlim(0,0.7)

        plt.title('誤差棒圖')

        plt.show()

        ##### 4.效果演示

      截屏2021-09-14 下午6.20.23

        更多關(guān)于“Python培訓”的問題,歡迎咨詢千鋒教育在線名師。千鋒教育多年辦學,課程大綱緊跟企業(yè)需求,更科學更嚴謹,每年培養(yǎng)泛IT人才近2萬人。不論你是零基礎(chǔ)還是想提升,都可以找到適合的班型,千鋒教育隨時歡迎你來試聽。

      tags:
      聲明:本站稿件版權(quán)均屬千鋒教育所有,未經(jīng)許可不得擅自轉(zhuǎn)載。
      10年以上業(yè)內(nèi)強師集結(jié),手把手帶你蛻變精英
      請您保持通訊暢通,專屬學習老師24小時內(nèi)將與您1V1溝通
      免費領(lǐng)取
      今日已有369人領(lǐng)取成功
      劉同學 138****2860 剛剛成功領(lǐng)取
      王同學 131****2015 剛剛成功領(lǐng)取
      張同學 133****4652 剛剛成功領(lǐng)取
      李同學 135****8607 剛剛成功領(lǐng)取
      楊同學 132****5667 剛剛成功領(lǐng)取
      岳同學 134****6652 剛剛成功領(lǐng)取
      梁同學 157****2950 剛剛成功領(lǐng)取
      劉同學 189****1015 剛剛成功領(lǐng)取
      張同學 155****4678 剛剛成功領(lǐng)取
      鄒同學 139****2907 剛剛成功領(lǐng)取
      董同學 138****2867 剛剛成功領(lǐng)取
      周同學 136****3602 剛剛成功領(lǐng)取
      相關(guān)推薦HOT
      反欺詐中所用到的機器學習模型有哪些?

      一、邏輯回歸模型邏輯回歸是一種常用的分類模型,特別適合處理二分類問題。在反欺詐中,邏輯回歸可以用來預(yù)測一筆交易是否是欺詐。二、決策樹模...詳情>>

      2023-10-14 14:09:29
      軟件開發(fā)管理流程中會出現(xiàn)哪些問題?

      一、需求不清需求不明確是導致項目失敗的主要原因之一。如果需求沒有清晰定義,開發(fā)人員可能會開發(fā)出不符合用戶期望的產(chǎn)品。二、通信不足溝通問...詳情>>

      2023-10-14 13:43:21
      軟件定制開發(fā)中的敏捷開發(fā)是什么?

      軟件定制開發(fā)中的敏捷開發(fā)是什么軟件定制開發(fā)中的敏捷開發(fā),從宏觀上看,是一個高度關(guān)注人員交互,持續(xù)開發(fā)與交付,接受需求變更并適應(yīng)環(huán)境變化...詳情>>

      2023-10-14 13:24:57
      什么是PlatformIo?

      PlatformIO是什么PlatformIO是一個全面的物聯(lián)網(wǎng)開發(fā)平臺,它為眾多硬件平臺和開發(fā)環(huán)境提供了統(tǒng)一的工作流程,有效簡化了開發(fā)過程,并能兼容各種...詳情>>

      2023-10-14 12:55:06
      云快照與自動備份有什么區(qū)別?

      1、定義和目標不同云快照的主要目標是提供一種快速恢復數(shù)據(jù)的方法,它只記錄在快照時間點后的數(shù)據(jù)變化,而不是所有的數(shù)據(jù)。自動備份的主要目標...詳情>>

      2023-10-14 12:48:59