-- The matplotlib Library
-- Installation
conda install matplotlib
-- IPython and IPython QtConsole
ipython qtconsole --matplotlib inline
--matplotlib Architecture
There layers:
scripting
pylab
pyplot
artist
primitive artists
composite artists
backend
figure canvas
renderer
event
-- pylab and pyplot
from pylab import *
import matplotlib.pyplot as plt
import numpy as np
plot(x,y)
array([1,2,3,4])
-- A Simple Interactive Chart
In [1]: import matplotlib.pyplot as plt
In [2]: plt.plot([1,2,3,4])
Out[2]: [<matplotlib.lines.Line2D at 0xa3eb438>]
In [3]: plt.show()
-- Set the Properties of the Plot
In [4]: plt.plot([1,2,3,4],[1,4,9,16],íroí)
Out[4]: [<matplotlib.lines.Line2D at 0x93e6898>]
In [4]: plt.show()
In [4]: plt.axis([0,5,0,20])
: plt.title(íMy first plotí)
: plt.plot([1,2,3,4],[1,4,9,16],íroí)
Out[4]: [<matplotlib.lines.Line2D at 0x97f1c18>]
In [5]: import math
In [6]: import numpy as np
In [7]: t = np.arange(0,2.5,0.1)
: y1 = map(math.sin,math.pi*t)
: y2 = map(math.sin,math.pi*t+math.pi/2)
: y3 = map(math.sin,math.pi*t-math.pi/2)
In [8]: plt.plot(t,y1,íb*í,t,y2,íg^í,t,y3,íysí)
Out[8]:
[<matplotlib.lines.Line2D at 0xcbd2e48>,
<matplotlib.lines.Line2D at 0xcbe10b8>,
<matplotlib.lines.Line2D at 0xcbe15c0>]
In [9]: plt.plot(t,y1,íb--í,t,y2,ígí,t,y3,ír-.í)
Out[9]:
[<matplotlib.lines.Line2D at 0xd1eb550>,
<matplotlib.lines.Line2D at 0xd1eb780>,
<matplotlib.lines.Line2D at 0xd1ebd68>]
-- Using the kwargs
matplotlib.pyplot.plot(*args, **kwargs)
In [10]: plt.plot([1,2,4,2,1,0,1,2,1,4],linewidth=2.0)
Out[10]: [<matplotlib.lines.Line2D at 0xc909da0>]
-- Working with Multiple Figures and Axes
In [11]: t = np.arange(0,5,0.1)
: y1 = np.sin(2*np.pi*t)
: y2 = np.sin(2*np.pi*t)
In [12]: plt.subplot(211)
: plt.plot(t,y1,íb-.í)
: plt.subplot(212)
: plt.plot(t,y2,ír--í)
Out[12]: [<matplotlib.lines.Line2D at 0xd47f518>]
In [ ]: t = np.arange(0.,1.,0.05)
: y1 = np.sin(2*np.pi*t)
: y2 = np.cos(2*np.pi*t)
In [ ]: plt.subplot(121)
: plt.plot(t,y1,íb-.í)
: plt.subplot(122)
: plt.plot(t,y2,ír--í)
Out[94]: [<matplotlib.lines.Line2D at 0xed0c208>]
-- Adding Further Elements to the Chart
In [10]: 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í)
Out[10]: [<matplotlib.lines.Line2D at 0x990f3c8>]
In [ ]: 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í)
Out[116]: [<matplotlib.lines.Line2D at 0x11f17470>]
In [ ]: 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í)
Out[108]: [<matplotlib.lines.Line2D at 0x10f76898>]
In [ ]: 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í)
Out[130]: [<matplotlib.lines.Line2D at 0x13920860>]
-- Adding a Grid
In [ ]: 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í)
Out[108]: [<matplotlib.lines.Line2D at 0x10f76898>]
-- Adding a Legend
In [ ]: 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í])
Out[156]: <matplotlib.legend.Legend at 0x16377550>
In [ ]: 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)
Out[170]: <matplotlib.legend.Legend at 0x1828d7b8>
-- Saving Your Charts
-- Saving the Code
In [171]: import matplotlib.pyplot as plt
%save my_first_chart 171
my_first_chart.py
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)
ipython qtconsole --matplotlib inline -m my_first_chart.py
%load my_first_chart.py
%run my_first_chart.py
-- Converting Your Session as an HTML File
-- Saving Your Chart Directly as an Image
In [ ]: 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í)
-- Handling Date Values
In [ ]: 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)
Out[83]: [<matplotlib.lines.Line2D at 0x12666400>]
In [ ]: 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)
-- Chart Typology
-- Line Chart
In [ ]: 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)
Out[393]: [<matplotlib.lines.Line2D at 0x22404358>]
In [ ]: 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(3*x)/x
: plt.plot(x,y)
: plt.plot(x,y2)
: plt.plot(x,y3)
In [ ]: 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(3*x)/x
: plt.plot(x,y,ík--í,linewidth=3)
: plt.plot(x,y2,ím-.í)
: plt.plot(x,y3,color=í#87a3ccí,linestyle=í--í)
In [ ]: 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í$-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$í])
Out[423]:
([<matplotlib.axis.YTick at 0x26877ac8>,
<matplotlib.axis.YTick at 0x271d26d8>,
<matplotlib.axis.YTick at 0x273c7f98>,
<matplotlib.axis.YTick at 0x273cc470>,
<matplotlib.axis.YTick at 0x273cc9e8>],
<a list of 5 Text yticklabel objects>)
In [ ]: 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í$-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))
In [ ]: 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í$-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))
-- Line Charts with pandas
In [ ]: 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)
: x = np.arange(5)
: plt.axis([0,5,0,7])
: plt.plot(x,df)
: plt.legend(data, loc=2)
-- Histogram
In [ ]: import matplotlib.pyplot as plt
: import numpy as np
: pop = np.random.randint(0,100,100)
: pop
In [ ]: n,bins,patches = plt.hist(pop,bins=20)
-- Bar Chart
In [ ]: import matplotlib.pyplot as plt
: index = [0,1,2,3,4]
: values = [5,7,3,4,6]
: plt.bar(index,values)
In [ ]: 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í])
In [ ]: 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)
-- Horizontal Bar Chart
In [ ]: 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)
-- Multiserial Bar Chart
In [ ]: 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í])
In [ ]: 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í])
-- Multiseries Bar Chart with pandas DataFrame
In [ ]: 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í)
In [ ]: 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í])
In [ ]: 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í])
In [ ]: 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í])
In [ ]: 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)
-- Other Bar Chart Representations
In [ ]: 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í)
-- Pie Charts
In [ ]: import matplotlib.pyplot as plt
: labels = [íNokiaí,íSamsungí,íAppleí,íLumiaí]
: values = [10,30,45,15]
: colors = [íyellowí,ígreení,íredí,íblueí]
: plt.pie(values,labels=labels,colors=colors)
: plt.axis(íequalí)
In [ ]: import matplotlib.pyplot as plt
: labels = [í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í)
In [ ]: import matplotlib.pyplot as plt
: labels = [í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í)
-- Pie Charts with pandas DataFrame
In [ ]: 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[íseries1í].plot(kind=ípieí,figsize=(6,6))
-- Advanced Charts
-- Contour Plot
In [ ]: import matplotlib.pyplot as plt
: import numpy as np
: dx = 0.01; dy = 0.01
: x = 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)
In [ ]: import matplotlib.pyplot as plt
: import numpy as np
: dx = 0.01; dy = 0.01
: x = 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()
-- Polar Chart
In [ ]: import matplotlib.pyplot as plt
: import numpy as np
: N = 8
: theta = 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)
In [ ]: import matplotlib.pyplot as plt
: import numpy as np
: N = 8
: theta = 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)
-- mplot3d
from mpl_toolkits.mplot3d import Axes3D
-- 3D Surfaces
In [ ]: from mpl_toolkits.mplot3d import Axes3D
: import matplotlib.pyplot as plt
: fig = 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)
In [ ]: from mpl_toolkits.mplot3d import Axes3D
: import matplotlib.pyplot as plt
: fig = 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)
-- Scatter Plot in 3D
In [ ]: import matplotlib.pyplot as plt
: import numpy as np
: from mpl_toolkits.mplot3d import Axes3D
: xs = 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í)
-- Bar Chart 3D
In [ ]: import matplotlib.pyplot as plt
: import numpy as np
: from mpl_toolkits.mplot3d import Axes3D
: x = 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)
-- Multi-Panel Plots
-- Display Subplots within Other Subplots
In [ ]: import matplotlib.pyplot as plt
: fig = 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])
In [ ]: import matplotlib.pyplot as plt
: import numpy as np
: fig = 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)
-- Grids of Subplots
In [ ]: import matplotlib.pyplot as plt
: gs = 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:])
In [ ]: import matplotlib.pyplot as plt
: import numpy as np
: gs = 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í)
No comments:
Post a Comment