In [1]:
import matplotlib.pyplot as plt
%matplotlib inline
Let us create some data for X and Y for the plots
In [12]:
x = list(range(0,100))
y = list(map(lambda x:x**2, x))
In [14]:
plt.plot(x,y)
plt.xlabel('x')
plt.ylabel('y')
plt.title('x vs y')
Out[14]:
In [15]:
plt.subplot(1,2,1) #one row, 2 cols, 1st plot:
plt.plot(x,y)
plt.subplot(1,2,2) #the second plot:
plt.plot(y,x)
Out[15]:
In [23]:
fig = plt.figure()
ax = fig.add_axes([0.1,0.1,0.8,0.8]) #rectangle's [left, bottom, width, height]
ax.plot(x,y)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('x vs y')
Out[23]:
In [25]:
fig2 = plt.figure()
ax1 = fig2.add_axes([0.1,0.1,0.8,0.8])
ax_ins = fig2.add_axes([0.2,0.5,0.3,0.3]) #insert in upper left side of plot
Consider the rectangle has max length and width = 1. You can create the first plot as big as you want filling this canvas. Then create the second sort of independent of first, using the same canvas coordinates.
In [26]:
fig3 = plt.figure()
ax1 = fig3.add_axes([0,0,1,1]) #absolute - full size fig
ax_ins = fig3.add_axes([0.5,0.1,0.4,0.4]) #insert in lower right side of plot
In [27]:
fig3 = plt.figure()
ax1 = fig3.add_axes([0,0,0.4,1]) # about half in width, full height
ax2 = fig3.add_axes([0.5,0,0.4,1]) #same, but to the right
In [30]:
fig, axes = plt.subplots(nrows=1,ncols=2)
axes[0].plot(x,y)
axes[0].set_title('x vs y')
axes[1].plot(x,x)
axes[1].set_title('x vs x')
Out[30]:
In [33]:
fig, axes = plt.subplots(nrows=1,ncols=2, figsize=(5,5)) #specifying as **kwargs
axes[0].plot(x,y)
axes[1].plot(x,x)
#use tight layout to resize plots within the canvas so there is no overlaps
fig.tight_layout()
In [34]:
fig.savefig('my_plots.png', dpi=300)
In [38]:
fig, axes = plt.subplots(nrows=1,ncols=1, figsize=(5,3)) #just 1 large plot
axes.plot(x,y, label='x vs x^2')
axes.plot(x,x, label ='a straight line')
axes.legend(loc=0) #loc=0 corresponds to best position available.
#use tight layout to resize plots within the canvas so there is no overlaps
fig.tight_layout()
Note : fig.suplots()
does not always return a vector axis
object array. As shown above, if you have just 1 plot, it has only 1 axis object.
In [51]:
#linewidth or lw - ranges from 1 (default) to any high up
#colors - takes names and HTML notations
#alpha - is for transparency and ranges from [0-1]
#marker - for tick marks and specify in characters
#markersize
#markerfacecolor
#markeredgecolor
#markeredgewidth
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
st_line = list(range(0,10000, 100))
ax.plot(x,y,color='orange', linewidth='3', alpha=0.3, marker='*',
markersize=4, markerfacecolor='green', label='x vs y')
ax.plot(x,st_line, color='green', marker='o', markersize=10, markerfacecolor='green',
label='straight line')
ax.legend()
Out[51]:
In [54]:
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
st_line = list(range(0,10000, 100))
ax.plot(x,y,color='orange', linewidth='3', alpha=0.3, marker='*',
markersize=4, markerfacecolor='green', label='x vs y')
ax.plot(x,st_line, color='green', marker='o', markersize=10, markerfacecolor='green',
label='straight line')
ax.legend()
ax.set_xlim(0,20)
ax.set_ylim(0,3000)
Out[54]:
In [3]:
values = [400, 280, 10]
labels = ['apple', 'android', 'windows']
# you can just call plt.pie. However it prints a bunch of objs on the notebook. I do this just to suppress that.
fix, ax1 = plt.subplots()
# you get the returns from ax1.pie because the font is tiny and to make it bigger
# the autopct is to get the percentage values.
_, texts, autotexts = ax1.pie(values, labels=labels, shadow=True, autopct='%1.1f%%')
# make the font bigger by calling set_fontsize method each obj in texts, autotexts
list(map(lambda x:x.set_fontsize(15), texts))
list(map(lambda x:x.set_fontsize(15), autotexts))
# by default the pie has a perspective. so you here you make it flat
ax1.axis('equal')
ax1.set_title('Cell phone OS by popularity', fontsize=15)
Out[3]:
In [ ]: