Matplotlib
In [7]: import [Link] as plt
In [9]: %matplotlib inline
In [11]: import numpy as np
In [47]: x = [Link](0,5,11)
y = x ** 2
In [34]: x
Out[34]: array([0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. ])
In [36]: y
Out[36]: array([ 0. , 0.25, 1. , 2.25, 4. , 6.25, 9. , 12.25, 16. ,
20.25, 25. ])
Functional Mathode :
In [38]: [Link](x,y)
[Link]('X Label')
[Link]('Y Label')
[Link]('Title')
[Link]()
In [40]: [Link](1,2,1)
[Link](x,y,'r--')
[Link](1,2,2)
[Link](y,x,'b--')
[Link]()
Object Oriented Method :
In [68]: fig = [Link]()
axes = fig.add_axes([0.1,0.1,0.8,0.8])
[Link](x,y)
axes.set_xlabel('X Label')
axes.set_ylabel('Y Label')
axes.set_title('Object Oriented Method')
[Link]()
In [23]: fig, ax = [Link]()
[Link]([1, 2, 3], [4, 5, 6])
[Link]([1, 2, 3], [6, 5, 4])
[Link](["Line 1", "Line 2"])
[Link]()
In [42]: fig = [Link]()
ax1 = fig.add_axes([0.1,0.1,0.8,0.8])
ax2 = fig.add_axes([.2,.5,.4,.3])
[Link](x,y)
ax1.set_title('Larger Plot')
[Link](y,x)
ax2.set_title('Small plot')
[Link]()
subplots():
In [44]: fig, axes = [Link]()
[Link](x, y, 'r')
axes.set_xlabel('x')
axes.set_ylabel('y')
axes.set_title('title')
[Link]()
In [98]: fig, axes = [Link](nrows=1,ncols=2)
In [58]: for ax in axes:
[Link](x,y)
[Link]()
In [100… axes[0].plot(x,y)
axes[0].set_title('First plot')
axes[1].plot(y,x,'g--')
axes[1].set_title('Sceond plot')
plt.tight_layout()
[Link]()
In [124… fig ,axes = [Link](ncols=1,nrows=2,figsize=(8,2))
axes[0].plot(x,y)
axes[1].plot(y,x,'g--')
plt.tight_layout()
[Link]()
In [134… [Link]('[Link]',dpi=200)
In [164… fig = [Link](figsize =(3,2))
ax = fig.add_axes([0,0,1,1])
[Link](x,y,label='am')
[Link](y,x,label= 'yu')
[Link](loc=(0.1,0.1))
[Link]()
In [242… fig = [Link](figsize =(3,2))
ax = fig.add_axes([0,0,1,1])
[Link](x,y,color='#FF8C00',lw =4,alpha= 0.4,ls = '-',marker ='o',ms=10,
markerfacecolor='green',markeredgewidth =5,markeredgecolor='#8B008B')
[Link]()
In [24]: fig = [Link](figsize =(3,2))
ax = fig.add_axes([0,0,1,1])
[Link](x,y,color='#FF8C00',lw =1,alpha= 0.5,ls = '--')
ax.set_xlim([0,1])
ax.set_ylim([0,2])
[Link]()
In [34]: [Link](x,y)
[Link]()
In [56]: from random import sample
data = sample(range(1,100),10)
[Link](data)
[Link]()
In [60]: data = [[Link](0, std, 100) for std in range(1, 4)]
[Link](data,vert=True,patch_artist=True)
[Link]()
Logarithmic scale:
In [19]: fig, axes = [Link](1, 2, figsize=(10,4))
axes[0].plot(x, x**2, x, [Link](x))
axes[0].set_title("Exponential Function")
axes[1].plot(x, x**2, x, [Link](x))
axes[1].set_yscale("log")
axes[1].set_title("Logarithmic scale (y)")
[Link]()
Placement of ticks and custom tick labels:
In [28]: fig, ax = [Link](figsize=(10, 4))
[Link](x, x**2, x, x**3, lw=2)
ax.set_xticks([1, 2, 3, 4, 5])
ax.set_xticklabels([r'$\alpha$', r'$\beta$', r'$\gamma$', r'$\delta$', r'$\epsilon$
yticks = [0, 50, 100, 150]
ax.set_yticks(yticks)
ax.set_yticklabels(["$%.1f$" % y for y in yticks], fontsize=18)
[Link](which='both', linestyle='--', linewidth=0.5)
[Link]()
Scientific notation :
In [37]: import [Link] as plt
import numpy as np
from [Link] import ScalarFormatter
In [45]: x = [Link](1e-4, 1e4, 100)
y = x**2
fig, ax = [Link](figsize=(8, 6))
[Link](x, y)
ax.ticklabel_format(style='sci', scilimits=(0, 0), axis='both')
ax.set_xlabel("X-axis")
ax.set_ylabel("Y-axis")
[Link](True, which='both', linestyle='--', linewidth=0.5)
[Link].get_offset_text().set_fontsize(10)
[Link]()
Axis number and axis label spacing:
In [56]: x = [Link](0, 4, 100)
[Link]['[Link]'] = 10
[Link]['[Link]'] = 15
fig, ax = [Link](figsize=(8, 6))
[Link](x, x**2, label="$x^2$")
[Link](x, [Link](x), label="$e^x$")
ax.set_yticks([0, 50, 100, 150, 200])
[Link] = 20
[Link] = 30
ax.set_xlabel("x-axis", fontsize=12)
ax.set_ylabel("y-axis", fontsize=12)
ax.set_title("Custom Tick and Label Spacing", fontsize=14)
[Link]()
[Link]()
Axis spines:
In [58]: fig, ax = [Link](figsize=(6,2))
[Link]['bottom'].set_color('blue')
[Link]['top'].set_color('blue')
[Link]['left'].set_color('red')
[Link]['left'].set_linewidth(2)
[Link]['right'].set_color("none")
[Link].tick_left()
[Link]()
Axes where x and y is zero :
In [62]: fig, ax = [Link]()
[Link]['right'].set_color('none')
[Link]['top'].set_color('none')
[Link].set_ticks_position('bottom')
[Link]['bottom'].set_position(('data',0))
[Link].set_ticks_position('left')
[Link]['left'].set_position(('data',0))
xx = [Link](-0.75, 1., 100)
[Link](xx, xx**3)
[Link]()
2D plot styles :
In [65]: n = [Link]([0,1,2,3,4,5])
In [67]: fig, axes = [Link](1, 4, figsize=(12,3))
axes[0].scatter(xx, xx + 0.25*[Link](len(xx)))
axes[0].set_title("scatter")
axes[1].step(n, n**2, lw=2)
axes[1].set_title("step")
axes[2].bar(n, n**2, align="center", width=0.5, alpha=0.5)
axes[2].set_title("bar")
axes[3].fill_between(x, x**2, x**3, color="green", alpha=0.5);
axes[3].set_title("fill_between")
[Link]()
Text annotation :
In [69]: fig, ax = [Link]()
[Link](xx, xx**2, xx, xx**3)
[Link](0.15, 0.2, r"$y=x^2$", fontsize=20, color="blue")
[Link](0.65, 0.1, r"$y=x^3$", fontsize=20, color="green")
[Link]()
subplot2grid :
In [73]: fig = [Link]()
ax1 = plt.subplot2grid((3,3), (0,0), colspan=3)
ax2 = plt.subplot2grid((3,3), (1,0), colspan=2)
ax3 = plt.subplot2grid((3,3), (1,2), rowspan=2)
ax4 = plt.subplot2grid((3,3), (2,0))
ax5 = plt.subplot2grid((3,3), (2,1))
fig.tight_layout()
[Link]()
Colormap and contour figures :
In [78]: alpha = 0.7
phi_ext = 2 * [Link] * 0.5
def flux_qubit_potential(phi_m, phi_p):
return 2 + alpha - 2 * [Link](phi_p) * [Link](phi_m) - alpha * [Link](phi_ext -
In [80]: phi_m = [Link](0, 2*[Link], 100)
phi_p = [Link](0, 2*[Link], 100)
X,Y = [Link](phi_p, phi_m)
Z = flux_qubit_potential(X, Y).T
In [82]: fig, ax = [Link]()
p = [Link](X/(2*[Link]), Y/(2*[Link]), Z, cmap=[Link], vmin=abs(Z).min
cb = [Link](p, ax=ax)
[Link]()
In [84]: fig, ax = [Link]()
cnt = [Link](Z, cmap=[Link], vmin=abs(Z).min(), vmax=abs(Z).max(),
[Link]()
3D figures :
In [88]: from mpl_toolkits.mplot3d.axes3d import Axes3D
In [99]: fig = [Link](figsize=(14,6))
ax = fig.add_subplot(1, 2, 1, projection='3d')
p = ax.plot_surface(X, Y, Z, rstride=4, cstride=4, linewidth=0)
ax = fig.add_subplot(1, 2, 2, projection='3d')
p = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=[Link], lin
cb = [Link](p, shrink=0.5)
[Link]()
In [97]: fig = [Link](figsize=(8,6))
ax = fig.add_subplot(1, 1, 1, projection='3d')
p = ax.plot_wireframe(X, Y, Z, rstride=4, cstride=4)
[Link]()
In [101… fig = [Link](figsize=(8,6))
ax = fig.add_subplot(1,1,1, projection='3d')
ax.plot_surface(X, Y, Z, rstride=4, cstride=4, alpha=0.25)
cset = [Link](X, Y, Z, zdir='z', offset=-[Link], cmap=[Link])
cset = [Link](X, Y, Z, zdir='x', offset=-[Link], cmap=[Link])
cset = [Link](X, Y, Z, zdir='y', offset=3*[Link], cmap=[Link])
ax.set_xlim3d(-[Link], 2*[Link]);
ax.set_ylim3d(0, 3*[Link]);
ax.set_zlim3d(-[Link], 2*[Link])
[Link]()