1. 程式人生 > >python一般畫圖方法

python一般畫圖方法

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
顯示中文
mpl.rcParams[‘font.sans-serif’] = [‘SimHei’]
mpl.rcParams[‘axes.unicode_minus’] = False
a[‘b’]= pd.to_datetime(a[‘b’]) 轉換時間

折線圖
plt.plot(x,y,ls=,lw=,c=,marker=,markersize=,markeredgecolor=,markerfacecolor, label=)
x: x軸上的數值
y: y軸上的數值
ls: 折線的風格( ‘-‘, ’–‘, ’-.‘和’:‘)
lw: 線條寬度
c: 顏色
marker: 線條上點的形狀
markersize: 線條上點的形狀
markeredgecolor: 點的邊框色
markerfacecolor: 點的填充色
label: 文字標籤
plt.plot(a[‘b’],a[‘c’],linestyle = ‘-’, linewidth = 2, color = ‘steelblue’, marker = ‘o’,
markersize = 6, markeredgecolor=‘black’, markerfacecolor=‘brown’)
不同類別折線圖在一起顯示
plt.plot(a[‘b’],a[‘c’],‘bs-’,a[‘b’],a[‘d’],‘ro-.’)

plt.plot(jd_stock[‘opening_price’],label=‘Opening_Price’)
plt.plot(jd_stock[‘closing_price’],label=‘Closing_Price’)

餅狀圖
plt.pie(x, explode,labels,colors,autopct=,pctdistance,shadow,startangle,radius,
wedgeprops,textprops,center)
x:資料
–labels: 標籤 --counterclock: 是否逆時針呈現:
–colors:顏色 --wedgeprops: 設定餅圖內外邊界的屬性
–autopct: 百分比 --textprops: 設定餅圖中文字屬性
–pctdistance: 百分比標籤與圓心距離 --center: 設定中心位置
–shadow: 是否新增餅圖陰影效果
–labeldistance: 設定各扇形標籤與圓心距離
–startangle: 設定餅圖的初始擺放角度
–radius: 設定餅圖半徑大小

labels =[“A難度水平”,‘B難度水平’,‘C難度水平’,‘D難度水平’]
students = [0.35,0.15,0.20,0.30]
colors = [‘red’,‘green’,‘blue’,‘yellow’]
explode = (0.1,0.1,0,0)
plt.pie(a[‘b’],explode = explode,labels =labels,autopct=’%3.2f%%’,startangle=45,shadow=True,
colors=colors)

柱狀圖
plt.bar(x, y,width,bottom,color,linewidth,tick_label,align)
• x: 指定x軸上數值
• y: 指定y軸上數值
• width: 指定條形圖寬度
• color: 條形圖的填充色
• edge: 條形圖的邊框色
• bottom: 百分比標籤與圓心距離
• linewidth: 條形圖邊框寬度
• tick_label: 條形圖的刻度標籤
• align: 指定x軸上對齊方式
labels = [‘q’,‘a’,‘c’,‘e’,‘r’,‘j’,‘b’,‘p’]
plt.bar(a[‘b’],a[‘c’],align=‘center’,color=‘y’,tick_label=labels,hatch=’-’)

industry_GDP = pd.read_excel(‘Industry_GDP.xlsx’)
temp = pd.crosstab(industry_GDP[‘Quarter’],industry_GDP[‘Industry_Type’],values=industry_GDP[‘GDP’],aggfunc=np.sum)
plt.bar(x= temp.index.values,height= temp[‘第一產業’],color=‘steelblue’,label=‘第一產業’,tick_label = temp.index.values)
plt.bar(x= temp.index.values,height= temp[‘第二產業’],bottom =temp[‘第一產業’], color=‘green’,label=‘第二產業’,tick_label = temp.index.values)
plt.bar(x= temp.index.values,height = temp[‘第三產業’],bottom =temp[‘第一產業’] + temp[‘第二產業’],color=‘red’,label=‘第三產業’,tick_label = temp.index.values)

temp = pd.crosstab(industry_GDP[‘Quarter’],industry_GDP[‘Industry_Type’],values=industry_GDP[‘GDP’],aggfunc=np.sum)
bar_width = 0.2 #設定寬度
quarter = temp.index.values #取出季度名稱
plt.bar(x= np.arange(0,4),height= temp[‘第一產業’],color=‘steelblue’,label=‘第一產業’,width = bar_width)
plt.bar(x= np.arange(0,4) + bar_width,height= temp[‘第二產業’], color=‘green’,label=‘第二產業’,width=bar_width)
plt.bar(x= np.arange(0,4) + 2*bar_width,height= temp[‘第三產業’], color=‘red’,label=‘第三產業’,width=bar_width)

直方圖
plt.hist(x,bins,range,normed,cumulative,bottom,align,rwidth,color,edgecolor,label)
•x: 資料
•bin: 條形個數
•range: 上下界
•normed: 是否將頻數轉換成頻率
•cumulative: 是否計算累計頻率
•bottom: 為直方圖的每個條形新增基準線,預設為0
•align: 對齊方式
•rwidth: 條形的寬度
•color: 填充色
•edgecolor: 設定直方圖邊框色
•label: 設定直方圖標籤

plt.hist(a[‘b’], bins=20,color=‘c’,edgecolor =‘black’,density=True,normed = True)
a[‘b’].plot(kind=‘kde’,color=‘red’,label=‘核密度圖’)

散點圖
plt.scatter(x,y,s,c,marker,cmap,norm,alpha,linewidths,edgecolorsl)
•x: x資料
•y: y軸資料
•s: 散點大小
•c: 散點顏色
•marker: 散點圖形狀
•cmap: 指定某個colormap值,該引數一般不用,用預設值
•norm: 設定資料亮度
•alpha: 散點的透明度
•linewidths: 散點邊界線的寬度
•edgecolors: 設定散點邊界線的顏色
plt.scatter(x = iris.Petal_Width,y = iris.Petal_Length,s =10,color =‘steelblue’)

不同種類的散點圖關係
plt.scatter(x = iris.Petal_Width[iris[‘Species’] ==‘setosa’],
y = iris.Petal_Length[iris[‘Species’] ==‘setosa’],
s =20,color =‘steelblue’,marker=‘o’,label = ‘setosa’,alpha=0.8)
plt.scatter(x = iris.Petal_Width[iris[‘Species’] ==‘versicolor’],
y = iris.Petal_Length[iris[‘Species’] ==‘versicolor’],
s =30,color =‘indianred’,marker=‘s’,label = ‘versicolor’)
plt.scatter(x = iris.Petal_Width[iris[‘Species’] ==‘virginica’],
y = iris.Petal_Length[iris[‘Species’] ==‘virginica’],
s =40,color =‘green’,marker=‘x’,label = ‘virginica’)

使用迴圈方式
colors_iris = [‘steelblue’,‘indianred’,‘green’]
sepcies =[ ‘setosa’,‘versicolor’,‘virginica’]
merker_iris =[‘o’,‘s’,‘x’]
for i in range(0,3):
plt.scatter(x=iris.Petal_Width[iris[‘Species’] ==sepcies[i]], y=iris.Petal_Length[iris[‘Species’] == sepcies[i]], s=20,color=colors_iris[i], marker=merker_iris[i], label=sepcies[i])

箱線圖
plt.boxplot(x,notch,sym,vert,whis,positions,widths,patch_artist,meanline,showmeans,
boxprops,labels,flierprops
•x: 資料
•width:寬度
•patch_artist: 是否填充箱體顏色
•meanline:是否顯示均值
•showmeans: 是否顯示均值
•meanprops;設定均值屬性,如點的大小,顏色等
•medianprops:設定中位數的屬性,如線的型別,大小等
•showfliers: 是否表示有異常值
•boxprops:設定箱體的屬性,邊框色和填充色
•cappops: 設定箱線頂端和末端線條的屬性,如顏色,粗細等

plt.boxplot(x=a[‘b’],patch_artist=True,showmeans =True,
boxprops={‘color’:‘black’,‘facecolor’:‘steelblue’},showfliers=True,
flierprops={‘marker’:‘o’,‘markerfacecolor’:‘red’,‘markersize’:3},
meanprops={‘marker’:‘D’,‘markerfacecolor’:‘indianred’,‘markersize’:4},
medianprops={‘linestyle’:’–’,‘color’:‘orange’},labels=[’’])