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>]
1 2 3 4 5
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>]
1 2 3 4 5 6 7 8 9
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>]
1 2 3 4 5 6 7 8 9 10
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
1 2 3 4 5 6 7 8 9 10 11
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
1 2 3 4 5 6 7 8 9 10 11
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>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import matplotlib.pyplot as plt 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)
import datetime import numpy as np import matplotlib.pyplot as plt 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] plt.plot(events,readings)
[<matplotlib.lines.Line2D at 0xcdfa2d0>]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
import datetime import numpy as np import matplotlib.pyplot as plt import matplotlib.dates as mdates months = 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
1 2 3 4 5
import matplotlib.pyplot as plt import numpy as np x = np.arange(-2*np.pi,2*np.pi,0.01) y = np.sin(3*x)/x plt.plot(x,y)
[<matplotlib.lines.Line2D at 0xcf9eab0>]
1 2 3 4 5 6 7 8 9
import matplotlib.pyplot as plt import numpy as np x = np.arange(-2*np.pi,2*np.pi,0.01) y = np.sin(x)/x y2 = np.sin(2*x)/x y3 = np.sin(3*x)/x plt.plot(x,y) plt.plot(x,y2) plt.plot(x,y3)
[<matplotlib.lines.Line2D at 0xcdc4ff0>]
1 2 3 4 5 6 7 8 9
import matplotlib.pyplot as plt import numpy as np x = np.arange(-2*np.pi,2*np.pi,0.01) y = np.sin(x)/x y2 = np.sin(2*x)/x y3 = np.sin(3*x)/x plt.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
1 2 3 4 5 6 7 8 9 10 11 12 13
import matplotlib.pyplot as plt import numpy as np x = np.arange(-2*np.pi,2*np.pi,0.01) y = np.sin(3*x)/x y2 = np.sin(2*x)/x y3 = np.sin(x)/x plt.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‘$-2pi$‘,r‘$-pi$‘,r‘$0$‘,r‘$+pi$‘,r‘$+2pi$‘]) 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>)
import matplotlib.pyplot as plt index = [0,1,2,3,4] values = [5,7,3,4,6] plt.bar(index,values)
<Container object of 5 artists>
1 2 3 4 5
import numpy as np index = 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>)
1 2 3 4 5 6 7 8 9
import numpy as np index = 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
1 2 3 4 5 6 7 8 9 10
import matplotlib.pyplot as plt import numpy as np index = 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
1 2 3 4 5 6 7 8 9 10 11 12 13
import matplotlib.pyplot as plt import numpy as np index = np.arange(5) values1 = [5,7,3,4,6] values2 = [6,6,4,5,7] values3 = [5,6,5,4,6] bw = 0.3 plt.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>)
1 2 3 4 5 6 7 8 9 10 11 12 13
import matplotlib.pyplot as plt import numpy as np index = np.arange(5) values1 = [5,7,3,4,6] values2 = [6,6,4,5,7] values3 = [5,6,5,4,6] bw = 0.3 plt.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
1 2 3 4 5 6 7 8
import matplotlib.pyplot as plt import numpy as np import pandas as pd data = {‘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>
1 2 3 4 5 6 7 8
import matplotlib.pyplot as plt import numpy as np import pandas as pd data = {‘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
1 2 3 4 5 6 7 8 9 10 11
import matplotlib.pyplot as plt import numpy as np series1 = 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>)
1 2 3 4 5 6 7 8 9 10 11 12
import matplotlib.pyplot as plt import numpy as np index = 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>)
1 2 3 4 5 6 7 8 9 10 11 12
import matplotlib.pyplot as plt import numpy as np index = 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
1 2 3 4 5 6 7
import matplotlib.pyplot as plt import pandas as pd data = {‘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
1 2 3 4 5 6 7 8 9 10 11 12 13
import matplotlib.pyplot as plt x0 = 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‘)