
發布
注冊
/
登錄python數據可視化的案例
Python數據可視化編程實戰PDF高清文檔下載
《Python數據可視化編程實戰》是一本使用Python實現數據可視化編程的實戰指南,介紹了如何使用Python最流行的庫,通過60余種方法創建美觀的數據可視化效果。
全書共8章,分別介紹了準備工作環境、了解數據、繪制并定制化圖表、學習更多圖表和定制化、創建3D可視化圖表、用圖像和地圖繪制圖表、使用正確的圖表理解數據以及更多matplotlib知識。
《Python數據可視化編程實戰》適合那些對Python編程有一定基礎的開發人員,可以幫助讀者從頭開始了解數據、數據格式、數據可視化,并學會使用Python可視化數據。
展開 Python學習筆記—數據可視化之美(一)
在大佬的強力推薦下,最近作者開始學習《Python數據可視化之美》這本書。通過之前的學習,大家可以掌握較為簡單的繪圖方法。那么如何將繪制的圖更加美觀的顯示出來呢?這也將是筆者學習的東西。接下來,讓我們開始學習的新篇章吧!!!
案例:
假設我們有四組數據,分別表示四組不同的正向骨架曲線(其結果在excel中顯示如下圖所示),那么怎么將結果一步步的美化呢?
首先,我們先在python中畫出該圖。
15 python數據可視化(精講箱圖)
00 載入擴展庫
import numpy as np
import matplotlib.pyplot as plt
01 箱圖
x=np.random.rand(2000)*10
plt.boxplot(x)
plt.grid(axis='y',ls=':',c='b')
02 patch_artist的使用
x=np.random.rand(2000)*10
plt.boxplot(x,patch_artist=True)
plt.grid(axis='y',ls=':',c='b')
03 whis的使用
x=np.random.rand(2000)*10
plt.boxplot(x,whis=0.1,patch_artist=True,showfliers=False)
plt.grid(axis='y',ls=':',c='b')
plt.ylim(0,10)
04 showfliers的使用
x=np.random.rand(1000)*10
y=np.array([20,22,25])
x1=np.hstack([x,y])
plt.boxplot([x,x1])
plt.grid(axis='y',ls=':',c='b')
x=np.random.rand(1000)*10
y=np.array([20,22,25])
x1=np.hstack([x,y])
plt.boxplot([x,x1],showfliers=False)
plt.grid(axis='y',ls=':',c='b')
plt.ylim(0,25)
05 vert的使用
x=np.random.rand(2000)*10
plt.boxplot(x,vert
展開 16 python數據可視化(精講誤差圖)
,color='r',lw=1)
03 修改數據點的顯示
x = np.arange(8)
y = 2.5 * np.sin(x / 20 * np.pi)
yerr=0.2
plt.errorbar(x,y,yerr,ls='-.',color='r',lw=1,
marker='^',ms=10,mec='b',mfc='m',mew='2')
04 修改誤差棒的顯示
x = np.arange(8)
y = 2.5 * np.sin(x / 20 * np.pi)
yerr=0.2
plt.errorbar(x,y,yerr,ls='-.',color='r',lw=1,
marker='^',ms=10,mec='b',mfc='m',mew='2',
ecolor='k',elinewidth=3)
05 修改誤差棒邊界的顯示
x = np.arange(8)
y = 2.5 * np.sin(x / 20 * np.pi)
yerr=0.2
plt.errorbar(x,y,yerr,ls='-.'
展開 
22 python數據可視化(精講坐標軸)
00 載入擴展庫
import numpy as np
import matplotlib.pyplot as plt
01 設置坐標軸位置
x=np.arange(3)
y=[0,1,0]
plt.axes([0.1,0.7,0.3,0.3],frameon=True,facecolor='y',aspect='equal')
plt.plot(x,y,color='b',ls='--')
plt.axes([0.4,0.4,0.3,0.3],frameon=False,facecolor='y',aspect='equal')
plt.plot(x,y,color='b',ls='--')
plt.axes([0.7,0.1,0.3,0.3],frameon=True,facecolor='g',aspect='auto')
plt.plot(x,y,color='r',ls='--')
02 隱藏坐標軸
plt.axes([0.1,0.6,0.3,0.3],frameon=True,facecolor='y',aspect='equal')
plt.plot(x,y,color='b',ls='--')
plt.axes([0.7,0.2,0.3,0.3],frameon=True,facecolor='y',aspect='auto')
plt.plot(x,y,color='b',ls='--')
plt.axis('off')
03 控制刻度線和刻度標簽的顯示 plt.setp()
x=np.arange(3)
y=[0,1,0]
ax1=plt.subplot(211)
ax1.plot(x,y)
ax2=plt.subplot(212)
plt.setp(ax2.get_xticklabels(
展開 13 python數據可視化(精講直方圖)
00 載入擴展庫
import numpy as np
import matplotlib.pyplot as plt
01 直方圖
x=np.random.randint(0,10,100)
bins=np.arange(0,11,1)
plt.hist(x,bins=bins,rwidth=0.5,align='left',color='r')
02 堆積(疊加)
x=np.random.randint(0,10,100)
y=np.random.randint(0,10,50)
bins=np.arange(0,11,1)
plt.hist([x,y],bins=bins,rwidth=0.5,align='left',stacked=True,color=['b','g'])
03 并列
x=np.random.randint(0,10,100)
y=np.random.randint(0,10,50)
z=np.random.randint(0,10,30)
bins=np.arange(0,11,1)
plt.hist([x,y,z],bins=bins,rwidth=0.5,align='left',stacked=False,color=['b','g','m'])
展開 11 python數據可視化(統計圖)
00 載入擴展庫
import numpy as np
import matplotlib.pyplot as plt
01 plt.bar(x,y,width,align,hatch,color,ec,fc,ls,lw,)
x=np.array([1,2,3,4,5,6])
y=np.array([3,1,4,5,8,9])
plt.bar(x,y,width=0.5,align='edge',ec='r',fc='b',ls='-.',lw='3',hatch='//')
plt.xticks(x,('q','a','c','e','r','j'),size=20,weight='heavy')
02 plt.barh(x,y,height,align,hatch,color,ec,fc,ls,lw,)
x=np.array([1,2,3,4,5,6])
y=np.array([3,1,4,5,8,9])
plt.barh(x,y,height=0.5,align='center',ec='r',fc='b',ls='-.',lw='3',hatch='//')
plt.yticks(x,('q','a','c','e','r','j'),size=20,weight='heavy')
03 plt.hist(x,bins,align,density,rwidth)
x=np.random.randint(0,11,100)
bins=np.arange(0,12,1)
plt.hist(x,bins,align='left',density=True,rwidth=0.5,ec='r',fc='g')
04 plt.pie(x,autopct,explode,labels
展開 10 python數據可視化(補遺)
); plt.axvspan(xmin,xmax,fc,ec,ls,lw,alpha)
plt.legend(loc)
Location String Location Code
=============== =============
'best' 0
'upper right' 1
'upper left' 2
'lower left' 3
'lower right' 4
'right' 5
'center left' 6
'center right' 7
'lower center' 8
'upper center' 9
'center' 10
=============== =============
plt.text(x,y,string,color,size,rotation)
plt.annotate(string,xy,xytext,color,szie,arrowprops=dict(headwidth,headlength,width,fc,ec,lw))
01 定義函數
import numpy as np
import matplotlib.pyplot as plt
t=np.arange(0,10,1/256)
xt=1.5*np.sin(2*np.pi*0.2*t)+3*np.sin(2*np.pi*0.3*t)
02 數據可視化
展開 08 python數據可視化(函數plt.plot)
dash-dot line style
':' dotted line style
02 定義函數
import numpy as np
import matplotlib.pyplot as plt
t=np.arange(0,10,1/256)
xt=1.5*np.sin(2*np.pi*0.2*t)+3*np.sin(2*np.pi*0.3*t)
03 數據可視化
plt.plot(t,xt,c='r',ls='-.',lw=2)
plt.plot([1,3,7],xt[[256,256*3,256*7]],lw=2,marker='s',ms=20,mec='r',mew=2,mfc='b')
展開 20 python數據可視化(精講趨勢線)
00 載入擴展庫
import numpy as np
import matplotlib.pyplot as plt
01 默認趨勢線
x=np.linspace(0,10,200)
y=np.sin(x)
plt.plot(x,y)
plt.xlim(0,10)
plt.ylim(-1.5,1.5)
plt.arrow(0,-0.2,np.pi/2,1)
02 給趨勢線增加箭頭
plt.arrow(0,-0.2,np.pi/2,1,
head_width=0.2,head_length=0.3,width=0.1)
03 設置趨勢線的顏色
plt.arrow(0,-0.2,np.pi/2,1,
head_width=0.1,head_length=0.3,width=0.05,color='r')
plt.arrow(0,-0.2,np.pi/2,1,
head_width=0.2,head_length=0.2,width=0.1,
facecolor='r',edgecolor='g')
04 設置趨勢線的邊界
plt.arrow(0,-0.2,np.pi/2,1,
head_width=0.2,head_length=0.3,width=0.1,
facecolor='r',edgecolor='g',
linewidth=1,linestyle='-.')
展開 14 python數據可視化(精講餅圖)
00 載入擴展庫
import numpy as np
import matplotlib.pyplot as plt
01 餅圖
x=[15, 30, 20, 35]
label=['Frogs', 'Hogs', 'Dogs', 'Logs']
plt.pie(x,autopct='%.2f%%', labels=label,startangle=45)
02 分裂餅圖
x=[15, 30, 20, 35]
label=['Frogs', 'Hogs', 'Dogs', 'Logs']
explode = [0, 0.1, 0, 0.2]
plt.pie(x,autopct='%.2f%%',explode=explode, labels=label,startangle=45)
03 改變餅圖文字位置
x=[15, 30, 20, 35]
label=['Frogs', 'Hogs', 'Dogs', 'Logs']
explode = [0, 0.1, 0, 0]
plt.pie(x,autopct='%.0f%%',explode=explode, labels=label,startangle=45,
pctdistance=0.4,labeldistance=0.9)
04 改變餅圖屬性
x=[15, 30, 20, 35]
plt.pie(x,autopct='%.0f%%',
textprops=dict(color='w',size=15,weight='bold' ))
05 環形圖
x=[15,30,20,35]
plt.pie(x,autopct='%.0f%%',radius=1
展開 
13 python數據可視化(精講直方圖)
00 載入擴展庫
import numpy as np
import matplotlib.pyplot as plt
01 直方圖
x=np.random.randint(0,10,100)
bins=np.arange(0,11,1)
plt.hist(x,bins=bins,rwidth=0.5,align='left',color='r')
02 堆積(疊加)
x=np.random.randint(0,10,100)
y=np.random.randint(0,10,50)
bins=np.arange(0,11,1)
plt.hist([x,y],bins=bins,rwidth=0.5,align='left',stacked=True,color=['b','g'])
03 并列
x=np.random.randint(0,10,100)
y=np.random.randint(0,10,50)
z=np.random.randint(0,10,30)
bins=np.arange(0,11,1)
plt.hist([x,y,z],bins=bins,rwidth=0.5,align='left',stacked=False,color=['b','g','m'])
展開 19 python數據可視化(坐標軸相關設置)
00 載入擴展庫
import numpy as np
import matplotlib.pyplot as plt
01 默認設置
x=np.arange(1,8,1)
y=2*x
plt.plot(x,y,marker='o',mfc='r')
plt.xlim(0,8)
plt.ylim(0,18)
02 改變figure的大小
x=np.arange(1,8,1)
y=2*x
plt.figure(figsize=(4,4))
plt.plot(x,y,marker='o',mfc='r')
plt.xlim(0,8)
plt.ylim(0,18)
03 改變figure大小的另一種方式
x=np.arange(1,8,1)
y=2*x
plt.figure().add_axes([0,0,1.5,0.7])
plt.plot(x,y,marker='o',mfc='r')
plt.xlim(0,8)
plt.ylim(0,18)
04 橫坐標顯示為星期
import numpy as np
import matplotlib.pyplot as plt
from calendar import day_name
x=np.arange(1,8,1)
y=2*x
fig=plt.figure()
fig.add_subplot(211)
plt.plot(x,y,marker='o',mfc='r')
plt.xlim(0,8)
plt.ylim(0,18)
fig.add_subplot(212)
plt.plot(x,y,marker='o',mfc='r')
plt.xticks(x,day_name[:7],rotation=20)
plt.xlim(0,8
展開 23 python數據可視化(撲克的四種花色)
00 載入擴展庫
import numpy as np
import matplotlib.pyplot as plt
01 紅桃
x=np.arange(3)
y=[0,1,0]
plt.scatter(x,y,marker='$\heartsuit$',s=500,color='r')
02 方塊
plt.scatter(x,y,marker='$\diamondsuit$',s=500,color='r')
03 黑桃
plt.scatter(x,y,marker='$\spadesuit$',s=500,color='k')
04 梅花
plt.scatter(x,y,marker='$\clubsuit$',s=500,color='k')
展開 09 python數據可視化(plt.scatter,散點圖)
01 plt.scatter(x,y,marker,c,s,alpha,edgecolor,lw,label)
c: the color of marker
s: the size of marker
alpha: the transparency of mark
edgecolor:the color of edge of marker
lw: the linewidth of edge
02 定義函數
import numpy as np
import matplotlib.pyplot as plt
t=np.arange(0,10,1/256)
xt=1.5*np.sin(2*np.pi*0.2*t)+3*np.sin(2*np.pi*0.3*t)
03 數據可視化
plt.scatter([1,3,7],xt[[256,256*3,256*7]],marker='o',s=500,c='g',alpha=0.8,edgecolor='r',lw=1)
展開