python绘图—— matplotlib
生活随笔
收集整理的這篇文章主要介紹了
python绘图—— matplotlib
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
python繪圖—— matplotlib
- 1.模塊導(dǎo)入
- 2.折線圖繪制
- 2.1 設(shè)置畫布大小
- 2.2畫圖
- 2.3 設(shè)置顯示中文
- 2.4 設(shè)置標(biāo)題
- 2.5 設(shè)置圖例
- 2.6 設(shè)置坐標(biāo)軸刻度和刻度標(biāo)簽
- 2.7 設(shè)置坐標(biāo)軸范圍
- 2.8 設(shè)置坐標(biāo)軸標(biāo)題
- 2.9 改變坐標(biāo)軸的默認(rèn)顯示方式
- 2.10 圖片保存
- 3 散點(diǎn)圖
- 4 條形圖
- 5 直方圖
- 6 餅圖
- 7 一圖多子圖
1.模塊導(dǎo)入
from matplotlib import pyplot as plt from matplotlib import font_manager import numpy as np from math import floor,ceil2.折線圖繪制
2.1 設(shè)置畫布大小
plt.figure(figsize=(20,8),dpi = 72)2.2畫圖
x = list(np.arange(1,21)) y = np.random.randint(2,high=10,size=20) plt.plot(x,y,color = 'red',alpha = 0.9,linestyle = ':',linewidth = 1,marker = 's',markersize = 5,markeredgecolor = 'r',markeredgewidth = 2,label = '第一條曲線') """linestyle: '-'is solid line, '--' is dashed line, '-.' is dash-dot line, ':' is dotted linealpha: its range is 0-1marker: 'o' is circle, '*' is star, '1-4' is tri_direction, 'v', '^', '<', '>', 's', 'P' is add.label:圖例內(nèi)容 """ x1 = list(np.arange(1,21)) y1 = np.random.randint(2,high=10,size=20) plt.plot(x1,y1,color = 'blue',alpha = 0.9,linestyle = ':',linewidth = 1,marker = 'v',markersize = 5,markeredgecolor = 'b',markeredgewidth = 1.5,label = '第二條曲線')2.3 設(shè)置顯示中文
fp = font_manager.FontProperties(fname = 'C:\Windows\Fonts\simsun.ttc',size = 18)2.4 設(shè)置標(biāo)題
name = '折線圖的標(biāo)題' plt.title(name,color = 'r',fontproperties = fp,size = 30)2.5 設(shè)置圖例
l = plt.legend(prop = fp,loc = 'upper left',title = 'legend')2.6 設(shè)置坐標(biāo)軸刻度和刻度標(biāo)簽
ticks_range = range(floor(min(x)),floor(max(x))+1,ceil((max(x)-min(x))/10)) ticks_label = [str(value)+'Kpa' for value in ticks_range] plt.xticks(ticks_range,ticks_label,fontproperties = fp,rotation = 45,color = 'red')ticks_range = range(floor(min(y)),floor(max(y))+1,ceil((max(y)-min(y))/10)) ticks_label = [str(value)+'℃' for value in ticks_range] plt.yticks(ticks_range,ticks_label,fontproperties = fp,rotation = 0,color = 'blue')2.7 設(shè)置坐標(biāo)軸范圍
plt.xlim([-1,21]) #plt.xlim(xmin = -1,xmax = 21) plt.ylim([0,12])2.8 設(shè)置坐標(biāo)軸標(biāo)題
plt.xlabel('個(gè)數(shù)',fontproperties = fp,horizontalalignment = 'right' ) """fontsize:可以給一個(gè)數(shù)字 verticalalignment:’top’, ‘bottom’, ‘center’, ‘baseline’ horizontalalignment:’center’, ‘right’, ‘left’ rotation: ‘vertical’,’horizontal’,’vertical’ """ plt.ylabel('個(gè)數(shù)',fontproperties = fp,verticalalignment = 'top' )2.9 改變坐標(biāo)軸的默認(rèn)顯示方式
ax = plt.gca() ax.spines['top'].set_color('none') ax.spines['bottom'].set_color('black') ax.spines['left'].set_color('b') ax.spines['right'].set_color('none')ax.spines['left'].set_position(('data',10)) # position is tuple ax.spines['bottom'].set_position(('data',5))2.10 圖片保存
plt.savefig('./zx.svg') plt.show3 散點(diǎn)圖
from matplotlib import pyplot as plt import numpy as np from math import floor,ceil from matplotlib import font_managerplt.figure(figsize = (20,8),dpi = 72) fp = font_manager.FontProperties(fname = 'C:\Windows\Fonts\simsun.ttc',size = 18) x = list(np.arange(1,31)) y = np.random.randint(20,high = 40 ,size = 30)# 散點(diǎn)圖 plt.scatter(x,y,color = 'r',marker = '*',s = 1000,label = '第一類原料油') """s : size of marker """x1 = list(np.arange(1,31)) y1 = np.random.randint(20,high = 40 ,size = 30) plt.scatter(x1,y1,color = 'b', marker = '1') plt.legend(prop = fp)plt.savefig('./sd.svg') plt.show()4 條形圖
import numpy as np from matplotlib import pyplot as plt from matplotlib import font_manager from math import floor,ceilplt.figure(figsize = (20,8),dpi = 72) # 加載中文字體 fp = font_manager.FontProperties(fname = 'c:\Windows\Fonts\simsun.ttc',size = 18)# 準(zhǔn)備數(shù)據(jù) x = ["數(shù)學(xué)","語文","英語","化學(xué)","物理"] y = [100,96,95,89,79] z = [20,50,30,46,28]# 作條形圖 plt.bar(np.arange(len(x)),y,width = 0.3,color = ['r','b','g','black','yellow'])figbar = plt.bar(np.arange(len(x)),z,width = 0.3,color = 'darkgreen',bottom = y)# x刻度標(biāo)簽 plt.xticks(np.arange(len(x)),["({})".format(value) for value in x],fontproperties = fp,size = 20)# 在條形圖上加標(biāo)注 (水平居中) i = 0 for each in figbar:height = each.get_height()plt.text(each.get_x()+each.get_width()/2, height+0.5+y[i],str(height+y[i]),ha = 'center',size = 20)i += 1 plt.close()# ****************** 橫向條形圖 ************************ plt.figure(figsize = (20,10),dpi = 72) barh = plt.barh(np.arange(len(x)),y,height = 0.3,color = ['r','b','g','black','yellow']) plt.yticks(np.arange(len(x)),['科目_{}'.format(i) for i in x],fontproperties = fp,rotation = 45)for each in barh:width = each.get_width()plt.text(width,each.get_y()+0.3/2,str(width),va = 'center',size = 20)ax = plt.gca() ax.spines['top'].set_color('none') ax.spines['right'].set_color('none')plt.savefig('./txt.svg')plt.show()5 直方圖
import numpy as np from matplotlib import pyplot as plt from matplotlib import font_manager from math import floor,ceilft = font_manager.FontProperties(fname = 'C:\Wondows\Fonts\simsun.ttc',size = 18) plt.figure(figsize = (20,8),dpi = 72) data = np.random.randint(120,high = 200,size = 1000000)# 繪制直方圖 distance = 10 number = floor((max(data)-min(data))/distance) plt.hist(data,number,color = 'r')plt.grid(linestyle = '--', alpha = 0.5)6 餅圖
from matplotlib import pyplot as plt from matplotlib import font_manager import numpy as np from math import floor,ceilfp = font_manager.FontProperties(fname = 'C:\Windows\Fonts\simsun.ttc',size = 18)# 繪制餅圖 data = [30,20,50]patches,l_text,p_text = plt.pie(data,explode = [0,0.05,0],colors = ["red","blue","green"],labels = ['第一類','第二類','第三類'],labeldistance = 1.1,autopct = "%1.1f%%",shadow = False,startangle = 90,pctdistance = 0.6) """ data: 為繪制數(shù)據(jù) explode: 為各扇形部分突出 colors: labels: 為各扇形部分標(biāo)簽 labeldistance: 標(biāo)簽距離圓心距離 autopct: 為各部分百分?jǐn)?shù) shadow: 是否顯示陰影 startangle: 開始角度 pctdistance: 百分?jǐn)?shù)距離圓心距離 """ for i in l_text:i.set_fontproperties(fp)for i in p_text:i.set_fontproperties(fp)plt.legend(prop = fp,loc = 'upper right',)plt.show()7 一圖多子圖
import numpy as np from matplotlib import pyplot as plt from matplotlib import font_manager from math import ceil,floor x = np.arange(1,101) fig = plt.figure(figsize = (20,10),dpi = 72) fig.add_subplot(2,3,1) plt.plot(x,x**2) plt.grid(linestyle = "--", linewidth = 1,alpha = 0.5,color = 'r')fig.add_subplot(2,3,4) plt.barh(x,x**3)by CyrusMay 2022 04 05
總結(jié)
以上是生活随笔為你收集整理的python绘图—— matplotlib的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 强化学习—— 蒙特卡洛树(Monte C
- 下一篇: 数据挖掘 —— 数据预处理