python数据分析实战-第7章-用matplotlib实现数据可视化

第7章 用matplotlib实现数据可视化  149

7.1 matplotlib库  149

7.2 安装  150

7.3 IPython和IPython QtConsole  150

7.4 matplotlib架构  151

7.4.1 Backend层  152

7.4.2 Artist层  152

7.4.3 Scripting层(pyplot)  153

7.4.4 pylab和pyplot  153

7.5 pyplot  154

7.5.1 生成一幅简单的交互式图表  154

123
import matplotlib.pyplot as plt%matplotlib inlineplt.plot([1,2,3,4])
[<matplotlib.lines.Line2D at 0xb50aa10>]

7.5.2 设置图形的属性  156

1
plt.plot([1,2,3,4],[1,4,9,16],‘ro‘)
[<matplotlib.lines.Line2D at 0xc78c450>]

123
plt.axis([0,5,0,20])plt.title(‘My first plot‘)plt.plot([1,2,3,4],[1,4,9,16],‘ro‘)
[<matplotlib.lines.Line2D at 0xc7ab610>]

7.5.3 matplotlib和NumPy  158

1234567
import mathimport numpy as npt = np.arange(0,2.5,0.1)y1 = list(map(math.sin,math.pi*t))y2 = list(map(math.sin,math.pi*t+math.pi/2))y3 = list(map(math.sin,math.pi*t-math.pi/2))plt.plot(t,y1,‘b*‘,t,y2,‘g^‘,t,y3,‘ys‘)
[<matplotlib.lines.Line2D at 0xc9e7ff0>,
 <matplotlib.lines.Line2D at 0xc9f3110>,
 <matplotlib.lines.Line2D at 0xc9f3430>]

1
plt.plot(t,y1,‘b--‘,t,y2,‘g‘,t,y3,‘r-.‘)
[<matplotlib.lines.Line2D at 0xca757b0>,
 <matplotlib.lines.Line2D at 0xca758b0>,
 <matplotlib.lines.Line2D at 0xca75bd0>]

7.6 使用kwargs  160

1
plt.plot([1,2,4,2,1,0,1,2,1,4],linewidth=2.0)
[<matplotlib.lines.Line2D at 0xcaba9d0>]

处理多个Figure和Axes对象

12345678
t = np.arange(0,5,0.1)y1 = np.sin(2*np.pi*t)y2 = np.sin(2*np.pi*t)

plt.subplot(211)plt.plot(t,y1,‘b-.‘)plt.subplot(212)plt.plot(t,y2,‘r--‘)
[<matplotlib.lines.Line2D at 0xcb219b0>]

12345678
t = np.arange(0.,1.,0.05)y1 = np.sin(2*np.pi*t)y2 = np.cos(2*np.pi*t)

plt.subplot(121)plt.plot(t,y1,‘b-.‘)plt.subplot(122)plt.plot(t,y2,‘r--‘)
[<matplotlib.lines.Line2D at 0xcb8c670>]

7.7 为图表添加更多元素  162

7.7.1 添加文本  162

12345
plt.axis([0,5,0,20])plt.title(‘My first plot‘)plt.xlabel(‘Counting‘)plt.ylabel(‘Square values‘)plt.plot([1,2,3,4],[1,4,9,16],‘ro‘)
[<matplotlib.lines.Line2D at 0xcbdf930>]

12345
plt.axis([0,5,0,20])plt.title(‘My first plot‘,fontsize=20,fontname=‘Times New Roman‘)plt.xlabel(‘Counting‘,color=‘gray‘)plt.ylabel(‘Square values‘,color=‘gray‘)plt.plot([1,2,3,4],[1,4,9,16],‘ro‘)
[<matplotlib.lines.Line2D at 0xcc19fb0>]

123456789
plt.axis([0,5,0,20])plt.title(‘My first plot‘,fontsize=20,fontname=‘Times New Roman‘)plt.xlabel(‘Counting‘,color=‘gray‘)plt.ylabel(‘Square values‘,color=‘gray‘)plt.text(1,1.5,‘First‘)plt.text(2,4.5,‘Second‘)plt.text(3,9.5,‘Third‘)plt.text(4,16.5,‘Fourth‘)plt.plot([1,2,3,4],[1,4,9,16],‘ro‘)
[<matplotlib.lines.Line2D at 0xcc5ca50>]

12345678910
plt.axis([0,5,0,20])plt.title(‘My first plot‘,fontsize=20,fontname=‘Times New Roman‘)plt.xlabel(‘Counting‘,color=‘gray‘)plt.ylabel(‘Square values‘,color=‘gray‘)plt.text(1,1.5,‘First‘)plt.text(2,4.5,‘Second‘)plt.text(3,9.5,‘Third‘)plt.text(4,16.5,‘Fourth‘)plt.text(1.1,12,r‘$y = x^2$‘,fontsize=20,bbox={‘facecolor‘:‘yellow‘,‘alpha‘:0.2})plt.plot([1,2,3,4],[1,4,9,16],‘ro‘)
[<matplotlib.lines.Line2D at 0xcca15f0>]

7.7.2 添加网格  165

1234567891011
plt.axis([0,5,0,20])plt.title(‘My first plot‘,fontsize=20,fontname=‘Times New Roman‘)plt.xlabel(‘Counting‘,color=‘gray‘)plt.ylabel(‘Square values‘,color=‘gray‘)plt.text(1,1.5,‘First‘)plt.text(2,4.5,‘Second‘)plt.text(3,9.5,‘Third‘)plt.text(4,16.5,‘Fourth‘)plt.text(1.1,12,r‘$y = x^2$‘,fontsize=20,bbox={‘facecolor‘:‘yellow‘,‘alpha‘:0.2})plt.grid(True)plt.plot([1,2,3,4],[1,4,9,16],‘ro‘)
[<matplotlib.lines.Line2D at 0xcd2e6b0>]

7.7.3 添加图例  166

1234567891011
plt.axis([0,5,0,20])plt.title(‘My first plot‘,fontsize=20,fontname=‘Times New Roman‘)plt.xlabel(‘Counting‘,color=‘gray‘)plt.ylabel(‘Square values‘,color=‘gray‘)plt.text(2,4.5,‘Second‘)plt.text(3,9.5,‘Third‘)plt.text(4,16.5,‘Fourth‘)plt.text(1.1,12,‘$y = x^2$‘,fontsize=20,bbox={‘facecolor‘:‘yellow‘,‘alpha‘:0.2})plt.grid(True)plt.plot([1,2,3,4],[1,4,9,16],‘ro‘)plt.legend([‘First series‘])
<matplotlib.legend.Legend at 0xcd71750>

123456789101112131415
import matplotlib.pyplot as pltplt.axis([0,5,0,20])plt.title(‘My first plot‘,fontsize=20,fontname=‘Times New Roman‘)plt.xlabel(‘Counting‘,color=‘gray‘)plt.ylabel(‘Square values‘,color=‘gray‘)plt.text(1,1.5,‘First‘)plt.text(2,4.5,‘Second‘)plt.text(3,9.5,‘Third‘)plt.text(4,16.5,‘Fourth‘)plt.text(1.1,12,‘$y = x^2$‘,fontsize=20,bbox={‘facecolor‘:‘yellow‘,‘alpha‘:0.2})plt.grid(True)plt.plot([1,2,3,4],[1,4,9,16],‘ro‘)plt.plot([1,2,3,4],[0.8,3.5,8,15],‘g^‘)plt.plot([1,2,3,4],[0.5,2.5,4,12],‘b*‘)plt.legend([‘First series‘,‘Second series‘,‘Third series‘],loc=2)
<matplotlib.legend.Legend at 0xcdca450>

Location Code Location String
0 best
1 upper-right
2 upper-left
3 lower-right
4 lower-left
5 right
6 center-left
7 center-right
8 lower-center
9 upper-center
10 centerChapter

7.8 保存图表  168

7.8.1 保存代码  169

7.8.2 将会话转换为HTML文件  170

7.8.3 将图表直接保存为图片  171

123456789101112131415
plt.axis([0,5,0,20])plt.title(‘My first plot‘,fontsize=20,fontname=‘Times New Roman‘)plt.xlabel(‘Counting‘,color=‘gray‘)plt.ylabel(‘Square values‘,color=‘gray‘)plt.text(1,1.5,‘First‘)plt.text(2,4.5,‘Second‘)plt.text(3,9.5,‘Third‘)plt.text(4,16.5,‘Fourth‘)plt.text(1.1,12,‘$y = x^2$‘,fontsize=20,bbox={‘facecolor‘:‘yellow‘,‘alpha‘:0.2})plt.grid(True)plt.plot([1,2,3,4],[1,4,9,16],‘ro‘)plt.plot([1,2,3,4],[0.8,3.5,8,15],‘g^‘)plt.plot([1,2,3,4],[0.5,2.5,4,12],‘b*‘)plt.legend([‘First series‘,‘Second series‘,‘Third series‘],loc=2)plt.savefig(‘my_chart.png‘)

7.9 处理日期值  171

12345678
import datetimeimport numpy as npimport matplotlib.pyplot as pltevents = [datetime.date(2015,1,23),datetime.date(2015,1,28),datetime.date(2015,2,3),datetime.date(2015,2,21),datetime.date(2015,3,15),datetime.date(2015,3,24),datetime.date(2015,4,8),datetime.date(2015,4,24)]readings = [12,22,25,20,18,15,17,14]plt.plot(events,readings)
[<matplotlib.lines.Line2D at 0xcdfa2d0>]

12345678910111213141516
import datetimeimport numpy as npimport matplotlib.pyplot as pltimport matplotlib.dates as mdatesmonths = mdates.MonthLocator()days = mdates.DayLocator()timeFmt = mdates.DateFormatter(‘%Y-%m‘)events = [datetime.date(2015,1,23),datetime.date(2015,1,28),datetime.date(2015,2,3),datetime.date(2015,2,21),datetime.date(2015,3,15),datetime.date(2015,3,24),datetime.date(2015,4,8),datetime.date(2015,4,24)]readings = [12,22,25,20,18,15,17,14]fig, ax = plt.subplots()plt.plot(events,readings)ax.xaxis.set_major_locator(months)ax.xaxis.set_major_formatter(timeFmt)ax.xaxis.set_minor_locator(days)

7.10 图表类型  173

7.11 线性图  173

12345
import matplotlib.pyplot as pltimport numpy as npx = np.arange(-2*np.pi,2*np.pi,0.01)y = np.sin(3*x)/xplt.plot(x,y)
[<matplotlib.lines.Line2D at 0xcf9eab0>]

123456789
import matplotlib.pyplot as pltimport numpy as npx = np.arange(-2*np.pi,2*np.pi,0.01)y = np.sin(x)/xy2 = np.sin(2*x)/xy3 = np.sin(3*x)/xplt.plot(x,y)plt.plot(x,y2)plt.plot(x,y3)
[<matplotlib.lines.Line2D at 0xcdc4ff0>]

123456789
import matplotlib.pyplot as pltimport numpy as npx = np.arange(-2*np.pi,2*np.pi,0.01)y = np.sin(x)/xy2 = np.sin(2*x)/xy3 = np.sin(3*x)/xplt.plot(x,y,‘k--‘,linewidth=3)plt.plot(x,y2,‘m-.‘)plt.plot(x,y3,color=‘#87a3cc‘,linestyle=‘--‘)
[<matplotlib.lines.Line2D at 0xe00f2d0>]

Code Color
b blue
g green
r red
c cyan
m magenta
y yellow
k black
w white

12345678910111213
import matplotlib.pyplot as pltimport numpy as npx = np.arange(-2*np.pi,2*np.pi,0.01)y = np.sin(3*x)/xy2 = np.sin(2*x)/xy3 = np.sin(x)/xplt.plot(x,y,color=‘b‘)plt.plot(x,y2,color=‘r‘)plt.plot(x,y3,color=‘g‘)plt.xticks([-2*np.pi, -np.pi, 0, np.pi, 2*np.pi],[r‘$-2\pi$‘,r‘$-\pi$‘,r‘$0$‘,r‘$+\pi$‘,r‘$+2\pi$‘])plt.yticks([-1,0,+1,+2,+3],[r‘$-1$‘,r‘$0$‘,r‘$+1$‘,r‘$+2$‘,r‘$+3$‘])
([<matplotlib.axis.YTick at 0xe0358f0>,
  <matplotlib.axis.YTick at 0xe00f7f0>,
  <matplotlib.axis.YTick at 0xe035eb0>,
  <matplotlib.axis.YTick at 0xe05b430>,
  <matplotlib.axis.YTick at 0xe05b8b0>],
 <a list of 5 Text yticklabel objects>)

1234567891011121314151617181920
import matplotlib.pyplot as pltimport numpy as npx = np.arange(-2*np.pi,2*np.pi,0.01)y = np.sin(3*x)/xy2 = np.sin(2*x)/xy3 = np.sin(x)/xplt.plot(x,y,color=‘b‘)plt.plot(x,y2,color=‘r‘)plt.plot(x,y3,color=‘g‘)plt.xticks([-2*np.pi, -np.pi, 0, np.pi, 2*np.pi],[r‘$-2\pi$‘,r‘$-\pi$‘,r‘$0$‘,r‘$+\pi$‘,r‘$+2\pi$‘])plt.yticks([-1,0,+1,+2,+3],[r‘$-1$‘,r‘$0$‘,r‘$+1$‘,r‘$+2$‘,r‘$+3$‘])ax = plt.gca()ax.spines[‘right‘].set_color(‘none‘)ax.spines[‘top‘].set_color(‘none‘)ax.xaxis.set_ticks_position(‘bottom‘)ax.spines[‘bottom‘].set_position((‘data‘,0))ax.yaxis.set_ticks_position(‘left‘)ax.spines[‘left‘].set_position((‘data‘,0))

1234567891011121314151617181920212223
import matplotlib.pyplot as pltimport numpy as npx = np.arange(-2*np.pi,2*np.pi,0.01)y = np.sin(3*x)/xy2 = np.sin(2*x)/xy3 = np.sin(x)/xplt.plot(x,y,color=‘b‘)plt.plot(x,y2,color=‘r‘)plt.plot(x,y3,color=‘g‘)plt.xticks([-2*np.pi, -np.pi, 0, np.pi, 2*np.pi],[r‘$-2\pi$‘,r‘$-\pi$‘,r‘$0$‘,r‘$+\pi$‘,r‘$+2\pi$‘])plt.yticks([-1,0,+1,+2,+3],[r‘$-1$‘,r‘$0$‘,r‘$+1$‘,r‘$+2$‘,r‘$+3$‘])plt.annotate(r‘$\lim_{x\to 0}\frac{\sin(x)}{x}= 1$‘, xy=[0,1],xycoords=‘data‘,xytext=[30,30],fontsize=16,textcoords=‘offset points‘,arrowprops=dict(arrowstyle="->",connectionstyle="arc3,rad=.2"))ax = plt.gca()ax.spines[‘right‘].set_color(‘none‘)ax.spines[‘top‘].set_color(‘none‘)ax.xaxis.set_ticks_position(‘bottom‘)ax.spines[‘bottom‘].set_position((‘data‘,0))ax.yaxis.set_ticks_position(‘left‘)ax.spines[‘left‘].set_position((‘data‘,0))

1234567891011
import matplotlib.pyplot as pltimport numpy as npimport pandas as pddata = {‘series1‘:[1,3,4,3,5],‘series2‘:[2,4,5,2,4],‘series3‘:[3,2,3,1,3]}df = pd.DataFrame(data)x = np.arange(5)plt.axis([0,5,0,7])plt.plot(x,df)plt.legend(data, loc=2)
<matplotlib.legend.Legend at 0xf047270>

7.12 直方图  180

1234
import matplotlib.pyplot as pltimport numpy as nppop = np.random.randint(0,100,100)pop
array([32, 29, 53,  8, 43, 91, 54, 31, 81, 54, 28, 56, 43, 39, 96, 44,  6,
       71, 29, 26, 34, 41, 78, 45,  1, 22, 94, 89, 19,  6, 95,  3, 86, 10,
        1, 54, 51,  5, 20, 18, 87, 12, 92, 50, 82,  3, 56, 23, 57, 92, 25,
        2, 86, 32, 75, 13, 85, 90,  8, 77, 91,  5, 31, 34, 44, 67, 30, 15,
       42, 63, 15, 38, 56, 83,  2, 18, 94, 49, 31, 47, 25,  5, 20, 70, 47,
       65, 29, 44, 23, 35, 73, 96, 34, 27, 11, 70, 96, 21, 71, 69])
1
n,bins,patches = plt.hist(pop,bins=20)

7.13 条状图  181

1234
import matplotlib.pyplot as pltindex = [0,1,2,3,4]values = [5,7,3,4,6]plt.bar(index,values)
<Container object of 5 artists>

12345
import numpy as npindex = np.arange(5)values1 = [5,7,3,4,6]plt.bar(index,values1)plt.xticks(index+0.4,[‘A‘,‘B‘,‘C‘,‘D‘,‘E‘])
([<matplotlib.axis.XTick at 0xf3246f0>,
  <matplotlib.axis.XTick at 0xf261bd0>,
  <matplotlib.axis.XTick at 0xf31c310>,
  <matplotlib.axis.XTick at 0xf3423f0>,
  <matplotlib.axis.XTick at 0xf342790>],
 <a list of 5 Text xticklabel objects>)

123456789
import numpy as npindex = np.arange(5)values1 = [5,7,3,4,6]std1 = [0.8,1,0.4,0.9,1.3]plt.title(‘A Bar Chart‘)plt.bar(index,values1,yerr=std1,error_kw={‘ecolor‘:‘0.1‘,‘capsize‘:6},alpha=0.7,label=‘First‘)plt.xticks(index+0.4,[‘A‘,‘B‘,‘C‘,‘D‘,‘E‘])plt.legend(loc=2)
<matplotlib.legend.Legend at 0xf389970>

7.13.1 水平条状图  183

12345678910
import matplotlib.pyplot as pltimport numpy as npindex = np.arange(5)values1 = [5,7,3,4,6]std1 = [0.8,1,0.4,0.9,1.3]plt.title(‘A Horizontal Bar Chart‘)plt.barh(index,values1,xerr=std1,error_kw={‘ecolor‘:‘0.1‘,‘capsize‘:6},alpha=0.7,label=‘First‘)plt.yticks(index+0.4,[‘A‘,‘B‘,‘C‘,‘D‘,‘E‘])plt.legend(loc=5)
<matplotlib.legend.Legend at 0xf3c9630>

7.13.2 多序列条状图  184

12345678910111213
import matplotlib.pyplot as pltimport numpy as npindex = np.arange(5)values1 = [5,7,3,4,6]values2 = [6,6,4,5,7]values3 = [5,6,5,4,6]bw = 0.3plt.axis([0,5,0,8])plt.title(‘A Multiseries Bar Chart‘,fontsize=20)plt.bar(index,values1,bw,color=‘b‘)plt.bar(index+bw,values2,bw,color=‘g‘)plt.bar(index+2*bw,values3,bw,color=‘r‘)plt.xticks(index+1.5*bw,[‘A‘,‘B‘,‘C‘,‘D‘,‘E‘])
([<matplotlib.axis.XTick at 0xf39dcd0>,
  <matplotlib.axis.XTick at 0xf39ded0>,
  <matplotlib.axis.XTick at 0xf3ef4b0>,
  <matplotlib.axis.XTick at 0xf42d230>,
  <matplotlib.axis.XTick at 0xf42d5d0>],
 <a list of 5 Text xticklabel objects>)

12345678910111213
import matplotlib.pyplot as pltimport numpy as npindex = np.arange(5)values1 = [5,7,3,4,6]values2 = [6,6,4,5,7]values3 = [5,6,5,4,6]bw = 0.3plt.axis([0,8,0,5])plt.title(‘A Multiseries Horizontal Bar Chart‘,fontsize=20)plt.barh(index,values1,bw,color=‘b‘)plt.barh(index+bw,values2,bw,color=‘g‘)plt.barh(index+2*bw,values3,bw,color=‘r‘)plt.yticks(index+0.4,[‘A‘,‘B‘,‘C‘,‘D‘,‘E‘])
([<matplotlib.axis.YTick at 0xf43e810>,
  <matplotlib.axis.YTick at 0xf43e0f0>,
  <matplotlib.axis.YTick at 0xf44f330>,
  <matplotlib.axis.YTick at 0xf473f50>,
  <matplotlib.axis.YTick at 0xf47d310>],
 <a list of 5 Text yticklabel objects>)

7.13.3 为pandas DataFrame生成多序列条状图  185

12345678
import matplotlib.pyplot as pltimport numpy as npimport pandas as pddata = {‘series1‘:[1,3,4,3,5],‘series2‘:[2,4,5,2,4],‘series3‘:[3,2,3,1,3]}df = pd.DataFrame(data)df.plot(kind=‘bar‘)
<matplotlib.axes._subplots.AxesSubplot at 0xf3a3330>

12345678
import matplotlib.pyplot as pltimport numpy as npimport pandas as pddata = {‘series1‘:[1,3,4,3,5],‘series2‘:[2,4,5,2,4],‘series3‘:[3,2,3,1,3]}df = pd.DataFrame(data)df.plot(kind=‘barh‘)
<matplotlib.axes._subplots.AxesSubplot at 0xf3bf030>

7.13.4 多序列堆积条状图  186

1234567891011
import matplotlib.pyplot as pltimport numpy as npseries1 = np.array([3,4,5,3])series2 = np.array([1,2,2,5])series3 = np.array([2,3,3,4])index = np.arange(4)plt.axis([0,4,0,15])plt.bar(index,series1,color=‘r‘)plt.bar(index,series2,color=‘b‘,bottom=series1)plt.bar(index,series3,color=‘g‘,bottom=(series2+series1))plt.xticks(index+0.4,[‘Jan15‘,‘Feb15‘,‘Mar15‘,‘Apr15‘])
([<matplotlib.axis.XTick at 0xf53e9f0>,
  <matplotlib.axis.XTick at 0xf549250>,
  <matplotlib.axis.XTick at 0xf53ef70>,
  <matplotlib.axis.XTick at 0xf5790b0>],
 <a list of 4 Text xticklabel objects>)

123456789101112
import matplotlib.pyplot as pltimport numpy as npindex = np.arange(4)series1 = np.array([3,4,5,3])series2 = np.array([1,2,2,5])series3 = np.array([2,3,3,4])plt.axis([0,15,0,4])plt.title(‘A Multiseries Horizontal Stacked Bar Chart‘)plt.barh(index,series1,color=‘r‘)plt.barh(index,series2,color=‘g‘,left=series1)plt.barh(index,series3,color=‘b‘,left=(series1+series2))plt.yticks(index+0.4,[‘Jan15‘,‘Feb15‘,‘Mar15‘,‘Apr15‘])
([<matplotlib.axis.YTick at 0xf58b1d0>,
  <matplotlib.axis.YTick at 0xf549ff0>,
  <matplotlib.axis.YTick at 0xf58bcf0>,
  <matplotlib.axis.YTick at 0xf5bf2b0>],
 <a list of 4 Text yticklabel objects>)

123456789101112
import matplotlib.pyplot as pltimport numpy as npindex = np.arange(4)series1 = np.array([3,4,5,3])series2 = np.array([1,2,2,5])series3 = np.array([2,3,3,4])plt.axis([0,15,0,4])plt.title(‘A Multiseries Horizontal Stacked Bar Chart‘)plt.barh(index,series1,color=‘w‘,hatch=‘xx‘)plt.barh(index,series2,color=‘w‘,hatch=‘///‘, left=series1)plt.barh(index,series3,color=‘w‘,hatch=‘\\\\\\‘,left=(series1+series2))plt.yticks(index+0.4,[‘Jan15‘,‘Feb15‘,‘Mar15‘,‘Apr15‘])
([<matplotlib.axis.YTick at 0xf5d0c10>,
  <matplotlib.axis.YTick at 0xf5b6790>,
  <matplotlib.axis.YTick at 0xf5dc170>,
  <matplotlib.axis.YTick at 0xf6066b0>],
 <a list of 4 Text yticklabel objects>)

7.13.5 为pandas DataFrame绘制堆积条状图  189

1234567
import matplotlib.pyplot as pltimport pandas as pddata = {‘series1‘:[1,3,4,3,5],‘series2‘:[2,4,5,2,4],‘series3‘:[3,2,3,1,3]}df = pd.DataFrame(data)df.plot(kind=‘bar‘, stacked=True)
<matplotlib.axes._subplots.AxesSubplot at 0xf5e74f0>

7.13.6 其他条状图  190

12345678910111213
import matplotlib.pyplot as pltx0 = np.arange(8)y1 = np.array([1,3,4,6,4,3,2,1])y2 = np.array([1,2,5,4,3,3,2,1])plt.ylim(-7,7)plt.bar(x0,y1,0.9,facecolor=‘r‘,edgecolor=‘w‘)plt.bar(x0,-y2,0.9,facecolor=‘b‘,edgecolor=‘w‘)plt.xticks(())plt.grid(True)for x, y in zip(x0, y1):    plt.text(x + 0.4, y + 0.05, ‘%d‘ % y, ha=‘center‘, va= ‘bottom‘)for x, y in zip(x0, y2):    plt.text(x + 0.4, (-y) - 0.05, ‘%d‘ % y, ha=‘center‘, va= ‘top‘)

7.14 饼图  190

123456
import matplotlib.pyplot as pltlabels = [‘Nokia‘,‘Samsung‘,‘Apple‘,‘Lumia‘]values = [10,30,45,15]colors = [‘yellow‘,‘green‘,‘red‘,‘blue‘]plt.pie(values,labels=labels,colors=colors)plt.axis(‘equal‘)
(-1.11637372803214,
 1.1007797090739162,
 -1.1163737124158366,
 1.1007797083302826)

12345678
import matplotlib.pyplot as pltlabels = [‘Nokia‘,‘Samsung‘,‘Apple‘,‘Lumia‘]values = [10,30,45,15]colors = [‘yellow‘,‘green‘,‘red‘,‘blue‘]explode = [0.3,0,0,0]plt.title(‘A Pie Chart‘)plt.pie(values,labels=labels,colors=colors,explode=explode,startangle=180)plt.axis(‘equal‘)
(-1.4003625034945653,
 1.130639575385504,
 -1.1007797083302826,
 1.1163737124158366)

123456789
import matplotlib.pyplot as pltlabels = [‘Nokia‘,‘Samsung‘,‘Apple‘,‘Lumia‘]values = [10,30,45,15]colors = [‘yellow‘,‘green‘,‘red‘,‘blue‘]explode = [0.3,0,0,0]plt.title(‘A Pie Chart‘)plt.pie(values,labels=labels,colors=colors,explode=explode,shadow=True,autopct=‘%1.1f%%‘,startangle=180)plt.axis(‘equal‘)
(-1.4003625034945653,
 1.130639575385504,
 -1.1007797083302826,
 1.1163737124158366)

1234567
import matplotlib.pyplot as pltimport pandas as pddata = {‘series1‘:[1,3,4,3,5],‘series2‘:[2,4,5,2,4],‘series3‘:[3,2,3,1,3]}df = pd.DataFrame(data)df[‘series1‘].plot(kind=‘pie‘,figsize=(6,6))
<matplotlib.axes._subplots.AxesSubplot at 0xf7d2ed0>

7.15 高级图表  193

7.15.1 等值线图  193

1234567891011
import matplotlib.pyplot as pltimport numpy as npdx = 0.01; dy = 0.01x = np.arange(-2.0,2.0,dx)y = np.arange(-2.0,2.0,dy)X,Y = np.meshgrid(x,y)def f(x,y):    return (1 - y**5 + x**5)*np.exp(-x**2-y**2)C = plt.contour(X,Y,f(X,Y),8,colors=‘black‘)plt.contourf(X,Y,f(X,Y),8)plt.clabel(C, inline=1, fontsize=10)
<a list of 16 text.Text objects>

12345678910
import matplotlib.pyplot as pltimport numpy as npdx = 0.01; dy = 0.01x = np.arange(-2.0,2.0,dx)y = np.arange(-2.0,2.0,dy)X,Y = np.meshgrid(x,y)C = plt.contour(X,Y,f(X,Y),8,colors=‘black‘)plt.contourf(X,Y,f(X,Y),8,cmap=plt.cm.hot)plt.clabel(C, inline=1, fontsize=10)plt.colorbar()
<matplotlib.colorbar.Colorbar at 0x1100f0b0>

7.15.2 极区图  195

123456789
import matplotlib.pyplot as pltimport numpy as npN = 8theta = np.arange(0.,2 * np.pi, 2 * np.pi / N)radii = np.array([4,7,5,3,1,5,6,7])plt.axes([0.025, 0.025, 0.95, 0.95], polar=True)colors = np.array([‘#4bb2c5‘, ‘#c5b47f‘, ‘#EAA228‘, ‘#579575‘, ‘#839557‘, ‘#958c12‘,‘#953579‘, ‘#4b5de4‘])bars = plt.bar(theta, radii, width=(2*np.pi/N), bottom=0.0, color=colors)

123456789
import matplotlib.pyplot as pltimport numpy as npN = 8theta = np.arange(0.,2 * np.pi, 2 * np.pi / N)radii = np.array([4,7,5,3,1,5,6,7])plt.axes([0.025, 0.025, 0.95, 0.95], polar=True)colors = np.array([‘lightgreen‘, ‘darkred‘, ‘navy‘, ‘brown‘, ‘violet‘, ‘plum‘,‘yellow‘, ‘darkgreen‘])bars = plt.bar(theta, radii, width=(2*np.pi/N), bottom=0.0, color=colors)

7.16 mplot3d  197

7.16.1 3D曲面  197

12345678910
from mpl_toolkits.mplot3d import Axes3Dimport matplotlib.pyplot as pltfig = plt.figure()ax = Axes3D(fig)X = np.arange(-2,2,0.1)Y = np.arange(-2,2,0.1)X,Y = np.meshgrid(X,Y)def f(x,y):    return (1 - y**5 + x**5)*np.exp(-x**2-y**2)ax.plot_surface(X,Y,f(X,Y), rstride=1, cstride=1)
<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x11eade50>

1234567891011
from mpl_toolkits.mplot3d import Axes3Dimport matplotlib.pyplot as pltfig = plt.figure()ax = Axes3D(fig)X = np.arange(-2,2,0.1)Y = np.arange(-2,2,0.1)X,Y = np.meshgrid(X,Y)def f(x,y):    return (1 - y**5 + x**5)*np.exp(-x**2-y**2)ax.plot_surface(X,Y,f(X,Y), rstride=1, cstride=1, cmap=plt.cm.hot)ax.view_init(elev=30,azim=125)

7.16.2 3D散点图  198

1234567891011121314151617181920
import matplotlib.pyplot as pltimport numpy as npfrom mpl_toolkits.mplot3d import Axes3Dxs = np.random.randint(30,40,100)ys = np.random.randint(20,30,100)zs = np.random.randint(10,20,100)xs2 = np.random.randint(50,60,100)ys2 = np.random.randint(30,40,100)zs2 = np.random.randint(50,70,100)xs3 = np.random.randint(10,30,100)ys3 = np.random.randint(40,50,100)zs3 = np.random.randint(40,50,100)fig = plt.figure()ax = Axes3D(fig)ax.scatter(xs,ys,zs)ax.scatter(xs2,ys2,zs2,c=‘r‘,marker=‘^‘)ax.scatter(xs3,ys3,zs3,c=‘g‘,marker=‘*‘)ax.set_xlabel(‘X Label‘)ax.set_ylabel(‘Y Label‘)ax.set_zlabel(‘Z Label‘)
Text(0.5,0,‘Z Label‘)

7.16.3 3D条状图  199

12345678910111213141516171819202122
import matplotlib.pyplot as pltimport numpy as npfrom mpl_toolkits.mplot3d import Axes3Dx = np.arange(8)y = np.random.randint(0,10,8)y2 = y + np.random.randint(0,3,8)y3 = y2 + np.random.randint(0,3,8)y4 = y3 + np.random.randint(0,3,8)y5 = y4 + np.random.randint(0,3,8)clr = [‘#4bb2c5‘, ‘#c5b47f‘, ‘#EAA228‘, ‘#579575‘, ‘#839557‘, ‘#958c12‘, ‘#953579‘,‘#4b5de4‘]fig = plt.figure()ax = Axes3D(fig)ax.bar(x,y,0,zdir=‘y‘,color=clr)ax.bar(x,y2,10,zdir=‘y‘,color=clr)ax.bar(x,y3,20,zdir=‘y‘,color=clr)ax.bar(x,y4,30,zdir=‘y‘,color=clr)ax.bar(x,y5,40,zdir=‘y‘,color=clr)ax.set_xlabel(‘X Axis‘)ax.set_ylabel(‘Y Axis‘)ax.set_zlabel(‘Z Axis‘)ax.view_init(elev=40)

7.17 多面板图形  200

7.17.1 在其他子图中显示子图  200

1234
import matplotlib.pyplot as pltfig = plt.figure()ax = fig.add_axes([0.1,0.1,0.8,0.8])inner_ax = fig.add_axes([0.6,0.6,0.25,0.25])

1234567891011
import matplotlib.pyplot as pltimport numpy as npfig = plt.figure()ax = fig.add_axes([0.1,0.1,0.8,0.8])inner_ax = fig.add_axes([0.6,0.6,0.25,0.25])x1 = np.arange(10)y1 = np.array([1,2,7,1,5,2,4,2,3,1])x2 = np.arange(10)y2 = np.array([1,3,4,5,4,5,2,6,4,3])ax.plot(x1,y1)inner_ax.plot(x2,y2)
[<matplotlib.lines.Line2D at 0x12144fd0>]

7.17.2 子图网格  202

12345678
import matplotlib.pyplot as pltgs = plt.GridSpec(3,3)fig = plt.figure(figsize=(6,6))fig.add_subplot(gs[1,:2])fig.add_subplot(gs[0,:2])fig.add_subplot(gs[2,0])fig.add_subplot(gs[:2,2])fig.add_subplot(gs[2,1:])
<matplotlib.axes._subplots.AxesSubplot at 0x12177a70>

123456789101112131415161718
import matplotlib.pyplot as pltimport numpy as npgs = plt.GridSpec(3,3)fig = plt.figure(figsize=(6,6))x1 = np.array([1,3,2,5])y1 = np.array([4,3,7,2])x2 = np.arange(5)y2 = np.array([3,2,4,6,4])s1 = fig.add_subplot(gs[1,:2])s1.plot(x,y,‘r‘)s2 = fig.add_subplot(gs[0,:2])s2.bar(x2,y2)s3 = fig.add_subplot(gs[2,0])s3.barh(x2,y2,color=‘g‘)s4 = fig.add_subplot(gs[:2,2])s4.plot(x2,y2,‘k‘)s5 = fig.add_subplot(gs[2,1:])s5.plot(x1,y1,‘b^‘,x2,y2,‘yo‘)
[<matplotlib.lines.Line2D at 0x1238a410>,
 <matplotlib.lines.Line2D at 0x1238a4d0>]

7.18 小结  204

原文地址:https://www.cnblogs.com/LearnFromNow/p/9349930.html

时间: 2024-10-15 04:49:10

python数据分析实战-第7章-用matplotlib实现数据可视化的相关文章

python数据分析实战-第2章-ptyhon世界简介

第2章 Python世界简介 122.1 Python--编程语言 122.2 Python--解释器 132.2.1 Cython 142.2.2 Jython 142.2.3 PyPy 142.3 Python 2和Python 3 142.4 安装Python 152.5 Python发行版 152.5.1 Anaconda 152.5.2 Enthought Canopy 162.5.3 Python(x,y) 172.6 使用Python 172.6.1 Python shell 17

python数据分析实战-第9章-数据分析实例气象数据

第9章 数据分析实例--气象数据 2309.1 待检验的假设:靠海对气候的影响 2309.2 数据源 2339.3 用IPython Notebook做数据分析 2349.4 风向频率玫瑰图 2469.5 小结 251 123 import numpy as npimport pandas as pdimport datetime 1 ferrara = pd.read_json('http://api.openweathermap.org/data/2.5/history/city?q=Fer

python数据分析实战-第6章-深入pandas数据处理

第6章 深入pandas:数据处理 117 6.1 数据准备 117 合并 1234567891011 #merge是两个dataframe共同包含的项import numpy as npimport pandas as pdframe1 = pd.DataFrame( {'id':['ball','pencil','pen','mug','ashtray'], 'price': [12.33,11.44,33.21,13.23,33.62]})print(frame1)print()frame

python数据分析实战-第3章-numpy库

第3章 NumPy库 32 3.1 NumPy简史 32 3.2 NumPy安装 32 3.3 ndarray:NumPy库的心脏 33 1 import numpy as np 1 a = np.array([1, 2, 3]) 1 a array([1, 2, 3]) 1 type(a), a.dtype, a.ndim, a.size, a.shape, a.itemsize (numpy.ndarray, dtype('int64'), 1, 3, (3,), 8) 1 b = np.a

【python数据分析实战】电影票房数据分析(一)数据采集

目录 1.获取url 2.开始采集 3.存入mysql 本文是爬虫及可视化的练习项目,目标是爬取猫眼票房的全部数据并做可视化分析. 1.获取url 我们先打开猫眼票房http://piaofang.maoyan.com/dashboard?date=2019-10-22 ,查看当日票房信息, 但是在通过xpath对该url进行解析时发现获取不到数据. 于是按F12打开Chrome DevTool,按照如下步骤抓包 再打开获取到的url:http://pf.maoyan.com/second-bo

【python数据分析实战】电影票房数据分析(二)数据可视化

目录 图1 每年的月票房走势图 图2 年票房总值.上映影片总数及观影人次 图3 单片总票房及日均票房 图4 单片票房及上映月份关系图 在上一部分<[python数据分析实战]电影票房数据分析(一)数据采集> 已经获取到了2011年至今的票房数据,并保存在了mysql中. 本文将在实操中讲解如何将mysql中的数据抽取出来并做成动态可视化. 图1 每年的月票房走势图 第一张图,我们要看一下每月的票房走势,毫无疑问要做成折线图,将近10年的票房数据放在一张图上展示. 数据抽取: 采集到的票房数据是

python实现的、带GUI界面电影票房数据可视化程序

代码地址如下:http://www.demodashi.com/demo/14588.html 详细说明: Tushare是一个免费.开源的python财经数据接口包.主要实现对股票等金融数据从数据采集.清洗加工 到 数据存储的过程,能够为金融分析人员提供快速.整洁.和多样的便于分析的数据. 完成本项目后,可以进一步通过类似的方法实现股票数据的可视化操作. (代码在python2.7或python3.6下均能正常运行,已在以下环境中进行过测试: python2.7 + tushare0.9.8

python数据分析实战---数据处理

数据处理 缺失值处理 数据缺失主要包括记录缺失和字段信息缺失等情况,其对数据分析会有较大影响,导致结果不确定性更加显著 1.判断是否有缺失值 import numpy as np import pandas as pd import matplotlib.pyplot as plt from scipy import stats % matplotlib inline # 判断是否有缺失值数据 - isnull,notnull # isnull:缺失值为True,非缺失值为False # not

机器学习实战第8章预测数值型数据:回归

1.简单的线性回归 假定输入数据存放在矩阵X中,而回归系数存放在向量W中,则对于给定的数据X1,预测结果将会是 这里的向量都默认为列向量 现在的问题是手里有一些x和对应的y数据,怎样才能找到W呢?一个常用的方法是找到使误差最小的W,这里的误差是指预测y值与真实y值之间的差值,使用该误差的简单累加将使得正差值和负差值相互抵消,所以我们采用平方误差. 平方误差可以写做: 用矩阵表示可以写成 使用上式对w进行求导: 具体可参考https://blog.csdn.net/nomadlx53/articl