日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python Matplotlib 可視化總結(jié)歸納(一) 折線圖、散點(diǎn)圖及其

系統(tǒng) 1980 0

一、數(shù)據(jù)準(zhǔn)備

1、python 內(nèi)置函數(shù) range

創(chuàng)建一個(gè)整數(shù)列表,只限于整數(shù)

            
              range(start, stop, step)
            
          

計(jì)數(shù)從start開始,默認(rèn)從0開始,stop結(jié)束,但不包含stop,step為步長(zhǎng),默認(rèn)為1

2. numpy中的arange

語法與range類似,由開始、結(jié)束、步長(zhǎng)組成,步長(zhǎng)默認(rèn)為1

            
              import numpy as np
print (np.arange(0,1,0.1))
            
          

輸出:

            
              [ 0.   0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9]
            
          

?3. numpy中的linspace模塊,

            
              np.linspace(start, end, num, endpoint=True)
            
          

計(jì)數(shù)從start開始,默認(rèn)從0開始,stop結(jié)束, num表示總個(gè)數(shù),endpoint默認(rèn)為false,設(shè)置不包含終值

            
              import numpy as np
print(np.linspace(0, 1, 10,endpoint=False))
print(np.linspace(0, 1, 10,endpoint=True))
            
          

輸出:

            
              [ 0.   0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9]  
[ 0.          0.11111111  0.22222222  0.33333333  0.44444444  0.55555556
  0.66666667  0.77777778  0.88888889  1.        ]
            
          

二、圖的類型 (繪制折線圖、散點(diǎn)圖,細(xì)節(jié)會(huì)在第三部分講解)

1.折線圖

            
              #繪制折線圖
import matplotlib.pyplot as plt
month = [1,2,3,4,5,6]
sales = [10,12,12.4,13,16,18]

plt.plot(month,sales)

plt.title("Sales for the first 6 months") #圖名
plt.xlabel("month")#x軸標(biāo)簽
plt.ylabel("sales")#y軸標(biāo)簽
plt.tick_params(axis='both')#x,y軸都有刻度

plt.savefig('.//result//3.1.png')#保存圖片,一定要在show之前保存圖片,否則保存的圖片就為空白
plt.show(
            
          

python Matplotlib 可視化總結(jié)歸納(一) 折線圖、散點(diǎn)圖及其坐標(biāo)軸屬性設(shè)置_第1張圖片

2. 散點(diǎn)圖

            
              plt.scatter(month,sales)
            
          
            
              import matplotlib.pyplot as plt
month = [1,2,3,4,5,6]
sales = [10,12,12.4,13,16,18]

plt.scatter(month,sales)

plt.title("Sales for the first 6 months") #圖名
plt.xlabel("month")#x軸標(biāo)簽
plt.ylabel("sales")#y軸標(biāo)簽
plt.tick_params(axis='both')#x,y軸都有刻度

plt.savefig('.//result//3.2.png')#保存圖片,一定要在show之前保存圖片,否則保存的圖片就為空白
plt.show()
            
          

python Matplotlib 可視化總結(jié)歸納(一) 折線圖、散點(diǎn)圖及其坐標(biāo)軸屬性設(shè)置_第2張圖片

總結(jié):

            
              import matplotlib.pyplot as plt

#繪制折線圖
plt.plot(x,y)

#繪制散點(diǎn)圖
plt.scatter(x,y)

            
          

?三、圖的細(xì)節(jié)問題處理

python Matplotlib 可視化總結(jié)歸納(一) 折線圖、散點(diǎn)圖及其坐標(biāo)軸屬性設(shè)置_第3張圖片

改變?nèi)鐖D所示

  • 圖表名的屬性
  • 折線的屬性
  • 坐標(biāo)軸的屬性
  • 刻度值的屬性

1.圖表名的屬性(title) ?

  • fontsize設(shè)置字體大小,默認(rèn)12,可以寫字體大小或參數(shù),可選參數(shù) ['xx-small', 'x-small', 'small', 'medium', 'large','x-large', 'xx-large']
  • fontweight設(shè)置字體粗細(xì),可選參數(shù) ['light', 'normal', 'medium', 'semibold', 'bold', 'heavy', 'black']
  • fontstyle設(shè)置字體類型,可選參數(shù)[?'normal'?|?'italic'?|?'oblique'?],italic斜體,oblique傾斜
  • verticalalignment設(shè)置水平對(duì)齊方式 ,可選參數(shù) :?'center'?,?'top'?,?'bottom'?,'baseline'?
  • horizontalalignment設(shè)置垂直對(duì)齊方式,可選參數(shù):left,right,center
  • rotation(旋轉(zhuǎn)角度)可選參數(shù)為:vertical,horizontal 也可以為數(shù)字
  • alpha透明度,參數(shù)值0至1之間
            
              plt.title('Graph',fontsize='large',fontweight='bold') 設(shè)置字體大小與格式
plt.title('Graph',color='blue') 設(shè)置字體顏色
plt.title('Graph',loc ='left') 設(shè)置字體位置

            
          

2.折線屬性(plot)

顏色

c? 青紅(cyan) ? r? 紅色(red) m? 品紅(magente) g? 綠色(green)?
y? 黃色(yellow) k? 黑色(black) w? 白色(white) b 藍(lán)色(blue)
            
              #繪制紅色折線
plt.plot(month,sales,color = 'r')
            
          

折線粗細(xì)(linewidth)

            
              plt.plot(month,sales,linewidth = 2)?
            
          

在plt.plot加入如下參數(shù)時(shí),改變折線形式

-? 直線 -- 虛線 -.? 一橫一點(diǎn) :? ?細(xì)小虛線 s? 方形 ? o? 圓形 ?D 菱形
h? 六角形 ?H? 六角形 *? 星號(hào) ? +? 加號(hào) x? x形 d? 菱形 p? 五角形
            
              plt.plot(month,sales,'-.',color ='r)
            
          

python Matplotlib 可視化總結(jié)歸納(一) 折線圖、散點(diǎn)圖及其坐標(biāo)軸屬性設(shè)置_第4張圖片

3.坐標(biāo)軸的屬性?

設(shè)置坐標(biāo)軸粗細(xì)

            
              #設(shè)置坐標(biāo)軸粗細(xì)
ax=plt.gca();#獲得坐標(biāo)軸的句柄
ax.spines['bottom'].set_linewidth(2);###設(shè)置底部坐標(biāo)軸的粗細(xì)
ax.spines['left'].set_linewidth(2);####設(shè)置左邊坐標(biāo)軸的粗細(xì)
ax.spines['right'].set_linewidth(2);###設(shè)置右邊坐標(biāo)軸的粗細(xì)
ax.spines['top'].set_linewidth(2);####設(shè)置上部坐標(biāo)軸的粗細(xì)
            
          

python Matplotlib 可視化總結(jié)歸納(一) 折線圖、散點(diǎn)圖及其坐標(biāo)軸屬性設(shè)置_第5張圖片

設(shè)置橫縱坐標(biāo)的名稱以及對(duì)應(yīng)字體格式(xlabel,ylabel)

  1.                 
                      #參數(shù)值的調(diào)換參考圖標(biāo)名的可選參數(shù)
    
    font= {'family' : 'Times New Roman','weight' : 'normal','size' : 30,} ??
    
    plt.xlabel('round',font)
    
    plt.ylabel('value',font)
                    
                  

    python Matplotlib 可視化總結(jié)歸納(一) 折線圖、散點(diǎn)圖及其坐標(biāo)軸屬性設(shè)置_第6張圖片

坐標(biāo)軸范圍設(shè)置,兩種,分別是xlim,ylinm 和 axis

            
              plt.xlim(-6,6)
plt.ylim(-500,500)
            
          
            
              plt.axis([xmin, xmax, ymin, ymax])

            
          

?

?

?4.坐標(biāo)軸刻度屬性

            
              plt.tick_params(axis='both',labelsize = 15)#x,y軸都有刻度 
            
          

python Matplotlib 可視化總結(jié)歸納(一) 折線圖、散點(diǎn)圖及其坐標(biāo)軸屬性設(shè)置_第7張圖片

?將以上介紹的放在一個(gè)代碼中,實(shí)現(xiàn)設(shè)置屬性的功能

            
              #繪制折線圖
import matplotlib.pyplot as plt
month = [1,2,3,4,5,6]
sales = [10,12,12.4,13,16,18]

#設(shè)置折線屬性
plt.plot(month,sales,'-.',color = 'r',linewidth = '4')

#設(shè)置圖名屬性
plt.title("Sales for the first 6 months",fontsize = 'large',fontweight = 'bold',color = 'b') 

#設(shè)置坐標(biāo)軸屬性
font= {'family' : 'Times New Roman','weight' : 'normal','size' : 30}
plt.xlabel("month",font)#x軸標(biāo)簽
plt.ylabel("sales",font)#y軸標(biāo)簽

#設(shè)置坐標(biāo)軸粗細(xì)
ax=plt.gca();#獲得坐標(biāo)軸的句柄
ax.spines['bottom'].set_linewidth(2);###設(shè)置底部坐標(biāo)軸的粗細(xì)
ax.spines['left'].set_linewidth(2);####設(shè)置左邊坐標(biāo)軸的粗細(xì)
ax.spines['right'].set_linewidth(2);###設(shè)置右邊坐標(biāo)軸的粗細(xì)
ax.spines['top'].set_linewidth(2);####設(shè)置上部坐標(biāo)軸的粗細(xì)

#設(shè)置坐標(biāo)軸刻度的屬性
plt.tick_params(axis='both',labelsize = 15)
plt.savefig('.//result//3.5.png')#保存圖片,一定要在show之前保存圖片,否則保存的圖片就為空白
plt.show()
            
          

雖然很丑,但是將圖名、坐標(biāo)軸等所有屬性都進(jìn)行了設(shè)置?

python Matplotlib 可視化總結(jié)歸納(一) 折線圖、散點(diǎn)圖及其坐標(biāo)軸屬性設(shè)置_第8張圖片

最后,推薦一波自己的公眾號(hào),Python機(jī)器學(xué)習(xí)進(jìn)階之路 ?

python Matplotlib 可視化總結(jié)歸納(一) 折線圖、散點(diǎn)圖及其坐標(biāo)軸屬性設(shè)置_第9張圖片


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 谷城县| 乳源| 河间市| 三明市| 积石山| 枣阳市| 遂昌县| 武穴市| 栖霞市| 封丘县| 临邑县| 松滋市| 南康市| 盱眙县| 台南市| 奎屯市| 双流县| 古田县| 孟村| 成都市| 多伦县| 门源| 西城区| 灵丘县| 沅陵县| 高平市| 晋州市| 南城县| 牟定县| 离岛区| 徐水县| 钟祥市| 珲春市| 浦北县| 宣汉县| 宜宾县| 永修县| 赤峰市| 建水县| 河津市| 安阳市|