In [1]: import matplotlib.pyplot as plt
In [2]: X = range(100)
list(X)[:5]
Out[2]: [0, 1, 2, 3, 4]
In [3]: Y=[value **2 for value in X]
Y[:5]
Out[3]: [0, 1, 4, 9, 16]
In [15]: plt.plot(X,Y)
Out[15]: # if not plt.show()
[<matplotlib.lines.Line2D at 0x5219b00>]
In [13]: plt.plot(X,Y)
plt.show()
Out[13]:
In [29]: import numpy as np
import matplotlib.pyplot as plt
In [30]: XList = np.linspace(-3,2,200)
YList = XList**2 - 2*XList +1 #ploting Y= X^2 - 2X -1
In [31]: plt.plot(XList, YList)
plt.show()
In [49]: import matplotlib.pyplot as plt
In [54]: XList, YList=[], []
for line in open('my_data.txt', 'r'):
values =[float(v) for v in line.split()]
XList.append(values[0]) #x-axis
YList.append(values[1]) #y-axis
In [55]: plt.plot(XList, YList)
plt.show()
In [56]: import matplotlib.pyplot as plt
In [57]: with open('my_data.txt', 'r') as f:
X,Y= zip(*[
[ float(s) for s in line.split() ] for line in f
])#2D-list, each element is a list in this 2D-list
In [58]: plt.plot(X,Y)
plt.show()
In [59]: import matplotlib.pyplot as plt
import numpy as np
In [60]: data = np.loadtxt('my_data.txt') #return 2D array or called full-blown matrices
In [61]: plt.plot(data[:,0], data[:,1])
#0-->columnIndex-->X- axis, 1-->columnIndex --> Y-axis
plt.show()
##################################
'my_data.txt'
0 0 6
1 1 5
2 4 4
4 16 3
5 25 2
6 36 1
##################################
In [62]: import numpy as np
import matplotlib.pyplot as plt
In [63]: data = np.loadtxt('my_data.txt')
data.T
Out[63]: array([[ 0., 1., 2., 4., 5., 6.],
[ 0., 1., 4., 16., 25., 36.],
[ 6., 5., 4., 3., 2., 1.]])
In [67]: for columnList in data.T[1:]: #excluding data.T[0]
plt.plot(data[:,0], columnList) # the len(columnList) == the number of curves
plt.show()
In [68]: data
Out[68]: array([[ 0., 0., 6.], [ 1., 1., 5.], [ 2., 4., 4.], [ 4., 16., 3.], [ 5., 25., 2.], [ 6., 36., 1.]])
#######################################################################
cp2_customizing the Color and Styles
1 Alias Colors
- b Blue
- g Green
- r Red
- c Cyan m Magenta
- y Yellow
- k Black
- w White
1.0.1 Gray-level strings: matplotlib will interpret a string representation of a floating point- value as a shade of gray, such as 0.75 for a medium light gray.
In [1]: import numpy as np
import matplotlib.pyplot as plt
In [2]: #Gaussian Distribution
#Probability density function
def pdf(X, mu, sigma):
a = 1./(sigma*np.sqrt(2.*np.pi))
b =-1./(2* sigma**2)
return a * np.exp(b* (X-mu)**2 ) #f(x)
In [3]: X = np.linspace(-6,6,1000)
for i in range(5):
#generates five sets of 50 samples from a normal distribution
samples = np.random.standard_normal(50)
mu, sigma = np.mean(samples), np.std(samples)
#For each of the 5 sets, we plot the estimated probability density in light gray
plt.plot(X, pdf(X, mu, sigma), color='0.75')
plt.plot(X, pdf(X,0., 1.), color='k')# standard normal distribution(mean=0,stdev=1)
plt.show()
In [6]: samples=np.random.standard_normal(50)
samples.shape
Out[6]: (50L,)
In [7]: mu, sigma = np.mean(samples), np.std(samples)
mu,sigma
Out[7]: (-0.20688694794049467, 0.8455170647061793)
Controlling a line pattern and thickness
# In[20]:
import numpy as np
import matplotlib.pyplot as plt
# ### Normal Distribution~(mean,Variance)=(0,1)
# #### Gaussians Probability Density Functions~(mean,Variance)=(u,sigma**2 )
# In[21]:
#Gaussians PDF
def pdf(X, mu, sigma):
a=1./( sigma * np.sqrt(2. * np.pi))
b=-1./( 2. * sigma **2)
return a*np.exp(b*(X-mu)**2)
# In[22]:
X = np.linspace(-6,6,1024)
# In[23]:
plt.plot(X, pdf(X, 0., 1.), color='k', linestyle='solid')
plt.plot(X, pdf(X, 0., .5), color='k', linestyle='dashed')
plt.plot(X, pdf(X, 0., .25), color='k', linestyle='dashdot')
plt.show()
The line width
# In[62]:
import numpy as np
import matplotlib.pyplot as plt
# In[65]:
def pdf(X, mu, sigma):
a = 1./(sigma*np.sqrt(2. * np.pi))
b= -1./(2.* sigma**2)
return a*np.exp(b* (X-mu)**2)
# In[69]:
X = np.linspace(-6,6,1024)
for i in range(64):
samples = np.random.standard_normal(50)
mu, sigma = np.mean(samples), np.std(samples)
plt.plot(X, pdf(X, mu, sigma), color='.75', linewidth=0.5)
plt.plot(X, pdf(X,0,1), color='k', linewidth=3.) #u =0, std.dev=1 ~ standard normal distribution
plt.show()
markevery=32 will plot every 32th marker starting from the first data point
# In[104]:
import numpy as np
import matplotlib.pyplot as plt
# In[100]:
X = np.linspace(-6,6,1024)
Y1 = np.sinc(X) #The sinc function is sin(pi x)/(pi x).
Y2= np.sinc(X)+1
Y2
# In[103]:
plt.plot(X,Y1, marker='o', color='.75')
#markevery=32 will plot every 32th marker starting from the first data point
plt.plot(X,Y2, marker='o', color='k', markevery=32)
plt.show()
# In[117]:
import numpy as np
import matplotlib.pyplot as plt
# In[118]:
X = np.linspace(-6,6,1024)
Y = np.sinc(X)
# In[120]:
plt.plot(X,Y,
linewidth=3.,
color='k',
markersize=9,
markeredgewidth=3.5,
markerfacecolor='.75',
markeredgecolor='k',
marker='o',
markevery=32)
plt.show()
# In[121]:
import numpy as np
import matplotlib as mpl
from matplotlib import pyplot as plt
# In[173]:
mpl.rc('lines', linewidth=5)
mpl.rc('axes', facecolor='y', edgecolor='w')
mpl.rc('xtick', color='m')
mpl.rc('ytick', color='m')
mpl.rc('text', color='m')
mpl.rc('figure', facecolor='r', edgecolor='y')
#mpl.rc('axes', color_cycle = ('w', '.5', '.75'))
# In[172]:
X = np.linspace(0,7,1024)
# In[170]:
plt.plot(X, np.sin(X))
plt.plot(X, np.cos(X))
plt.show()
If a matplotlibrc file is found in your current directory (that is, the directory from where you launched your script from), it will override matplotlib's default settings. You can also save your matplotlibrc file in a specific location to make your own default settings. In the interactive Python shell, run the following command: import matplotlib mpl.get_configdir() This command will display the location where you can place your matplotlibrc file so that those settings will be your own default settings.
Line Plots¶
The Series object’s index is passed to matplotlib for plotting on the x-axis, though you can disable this by passing use_index=False.
The x-axis ticks and limits can be adjusted with the xticks and xlim options, and y-axis respectively with yticks and ylim.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#Y-axis #X-axis
s = pd.Series( np.random.randn(10).cumsum(), index=np.arange(0,100,10) )
s.plot()
plt.show()
DataFrame’s plot method plots each of its columns as a different line on the same subplot, creating a legend automatically; The plot attribute contains a “family” of methods for different plot types. For example, df.plot() is equivalent to df.plot.line().
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame(np.random.randn(10,4).cumsum(0),
columns=['A','B','C','D'],
index = np.arange(0,100,10))
df.plot()
plt.show()
Figures and Subplots
Plots in matplotlib reside within a Figure object.
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
#pass no label or label='_nolegend_'
ax.plot(randn(1000).cumsum(), color='k', label='one')
ax.plot(randn(1000).cumsum(), color='k', linestyle='--', label='two')
ax.plot(randn(1000).cumsum(), color='k', linestyle='dotted',label='three')
ticks = ax.set_xticks([0,250,500,750,1000])
labels = ax.set_xticklabels(['one','two', 'three', 'four', 'five'], rotation=30, fontsize='small')
ax.set_title('My first matplotlib plot')
ax.set_xlabel('Stages')
OR
props = {
'title': 'My first matplotlib plot',
'xlabel': 'Stages'
}
ax.set(**props)
ax.legend(loc='best')
plt.show()
matplotlib draws on the last figure and subplot used (creating one if necessary), thus hiding the figure and subplot creation.
plt.plot(np.random.randn(50).cumsum(), color='black', ls='--')
matplotlib includes a convenience method, plt.subplots, that creates a new figure and returns a NumPy array containing the created subplot objects, the axes array can be easily indexed like a two-dimensional array; for example, axes[0, 1].
Adjusting the spacing around subplots
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)
# 1
fig, axes = plt.subplots(2,2, sharex=True, sharey= True)
for i in range(2):
for j in range(2):
axes[i,j].hist(np.random.randn(500), bins =5, color='k', alpha=0.5) #hist: frequency
plt.subplots_adjust(wspace=0.05, hspace=0.05)
# 2
from numpy.random import randn
arr=randn(30)
arrCumSum=arr.cumsum()
plt.plot(arrCumSum, color='k', linestyle='dashed', drawstyle='steps-post', label='steps-post', marker='o')
plt.legend(loc='best') #label='steps-post'
plt.show()
Annotations and Drawing on a Subplot
spx.csv file
......
import numpy as np
import pandas as pd
from datetime import datetime
#index_col : int or sequence or False, default None
data = pd.read_csv('../examples/spx.csv',parse_dates=True, index_col=0)
spx = data['SPX'] #'SPX' column # spx: pandas.core.series.Series
crisis_data=[
(datetime(2007, 10, 11), 'Peak of bull market'), #tuple
(datetime(2008, 3, 12), 'Bear Stearns Fails'),
(datetime(2008, 9, 15), 'Lehman Bankruptcy')
]
# // matplotlib Configuration
plt.rc('figure', figsize=(10,10))
font_options={
'family': 'monospace',
'weight': 'bold',
'size': 16
}
plt.rc('font', **font_options)
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
# spx: date: X-axis # SPX: y-axis
spx.plot(ax=ax, color='green', linestyle='-')
for date, label in crisis_data:
ax.annotate( label,
ha='left',
va='top',
xytext=(date, spx.asof(date) + 225), #The xytext parameter specifies the text position
xy=(date, spx.asof(date) + 75), #The xy parameter specifies the arrow's destination
arrowprops=dict(facecolor='blue', headwidth=10, headlength=4, width=2 ),
#arrowprops={'facecolor':'blue', 'headwidth':10, 'headlength':4, 'width':2} #OR
)
#Zoom in on 2007-2010
ax.set_xlim(['1/1/2007', '1/1/2011'])
ax.set_ylim([600,1800])
ax.set_title('Important dates in the 2008-2009 financial crisis')
plt.show()
Adding arrows
The aspect of the arrow is controlled by a dictionary passed to the arrowprops parameter: 'arrowstyle': The parameters ''<-'', ''<'', ''-'', ''wedge'',''simple'', and ''fancy'' control the style of the arrow 'facecolor': This is the color used for the arrow. It will be used to set the background and the edge color 'edgecolor': This is the color used for the edges of the arrow's shape 'alpha': This is used to set the transparency level so that the arrow blends with the background
The shrink parameter controls the gap between the arrow's endpoints and the arrow itself.
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1,1,1) # return first plot’s reference
#Left-Bottom corner position
#Width, Height
rect = plt.Rectangle( (0.2, 0.75), 0.4, 0.15, color='k', alpha=0.3 )
#center point position
circ = plt.Circle( (0.7, 0.2), radius=0.15, color='b', alpha=0.3 )
polygon = plt.Polygon( [ [0.15, 0.15],
[0.35, 0.4],
[0.2,0.6]
],
color='g',
alpha=0.5
)
ax.add_patch(rect)
ax.add_patch(circ)
ax.add_patch(polygon)
plt.show() # plt.savefig('figpath.png',dpi=400, bbox_inches='tight')
#dpi, which controls the dots-per-inch resolution
#bbox_inches, which can trim the whitespace around the actual figure.
from io import BytesIO
buffer = BytesIO()
plt.savefig(buffer)
plot_data = buffer.getvalue()
Adding shapes
Internally, a shape is described as a path called patch in matplotlib API. Paths for several kinds of shapes are available in the matplotlib.patches module. As is the case for lines, creating a path won't be enough to render it; you will have to signal that you want to render it. This is done by pyplot.gca().add_patch().
A lot of path constructors are available. Let's review those used in the example:
Circle: This takes the coordinates of its center and the radius as the parameters
Rectangle: This takes the coordinates of its lower-left corner and its size as the parameters
Ellipse: This takes the coordinates of its center and the half-length of its two axes as the parameters
FancyBox: This is like a rectangle but takes an additional boxstyle parameter (either 'larrow', 'rarrow', 'round', 'round4', 'roundtooth', 'sawtooth', or 'square')
import matplotlib.patches as patches
import matplotlib.pyplot as plt
#Circle #center point position
shape = patches.Circle((0,0), radius=1., color='.75')
plt.gca().add_patch(shape)
#Rectangle #Left-Bottom corner position
shape = patches.Rectangle((2.5,-.5), 2., 1., color='.75')
plt.gca().add_patch(shape) #Length, Width
#Ellipse #center position
shape = patches.Ellipse((0,-2.), 2., 1., angle=45., color='.75')
plt.gca().add_patch(shape) #2a:long-axis=2*2, 2b: short-axis=1*2
#Fancy box #inner-rectangle Left-Bottom
shape = patches.FancyBboxPatch((2.5, -2.5), 2., 1., boxstyle='sawtooth', color='.75')
plt.gca().add_patch(shape)
#display all
plt.grid(True)
plt.axis('scaled')
plt.show()
Working with polygons
import numpy as np
import matplotlib.patches as patches
import matplotlib.pyplot as plt
theta = np.linspace(0,2*np.pi, 9) # 8 points
points = np.vstack((np.cos(theta), np.sin(theta))).transpose()
plt.gca().add_patch(patches.Polygon(points, color='.75'))
plt.grid(True)
plt.axis('scaled')
plt.show()
Working with path attributes
import numpy as np
import matplotlib.patches as patches
import matplotlib.pyplot as plt
theta = np.linspace(0,2*np.pi, 6)# 5 points
points = np.vstack(( np.cos(theta), np.sin(theta) )).transpose()
plt.gca().add_patch( plt.Circle( (0,0), radius=1., color='.75' ) )
plt.gca().add_patch( plt.Polygon( points, closed=None, fill=None, lw=3., ls='dashed', edgecolor='k') )
plt.grid(True)
plt.axis('scaled')
plt.show()