一、數(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(
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()
總結(jié):
import matplotlib.pyplot as plt
#繪制折線圖
plt.plot(x,y)
#繪制散點(diǎn)圖
plt.scatter(x,y)
?三、圖的細(xì)節(jié)問題處理
改變?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)
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ì)
設(shè)置橫縱坐標(biāo)的名稱以及對(duì)應(yīng)字體格式(xlabel,ylabel)
-
#參數(shù)值的調(diào)換參考圖標(biāo)名的可選參數(shù) font= {'family' : 'Times New Roman','weight' : 'normal','size' : 30,} ?? plt.xlabel('round',font) plt.ylabel('value',font)
坐標(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軸都有刻度
?將以上介紹的放在一個(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è)置?
最后,推薦一波自己的公眾號(hào),Python機(jī)器學(xué)習(xí)進(jìn)階之路 ?
更多文章、技術(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ì)您有幫助就好】元
