Basic usage
Axes will take some getting used to
import matplotlib.pyplot as plt
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot([1,2],[1,2])
plt.show()
mark with a dot (point)
import matplotlib.pyplot as plt
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot([1,2],[1,2],marker="o")
Change line style
import matplotlib.pyplot as plt
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot([1,2],[1,2],linestyle="dashed",color="tab:orange")
Change line thickness and dot size
import matplotlib.pyplot as plt
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot([1,2],[1,2],marker="o",linewidth=3,markersize=15)
Name the axes
import matplotlib.pyplot as plt
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot([1,2],[0,24])
ax.set_xlabel("day",labelpad=5)
ax.set_ylabel("hour",labelpad=10)
Size and Range Specification
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(10,4))
ax=fig.add_subplot(111)
ax.plot([1,2],[1,2])
ax.set_xlim(1,5)
ax.set_ylim(0,3)
give a legend
import matplotlib.pyplot as plt
import numpy as np
t=np.linspace(0,10,100)
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(t,np.sin(t),label="sin(t)")
ax.plot(t,np.cos(t),label="cos(t)")
ax.legend()
Insert text anywhere
With transform=ax.transAxes, you can specify coordinates relative to the graph box instead of coordinates on the graph.
import matplotlib.pyplot as plt
import numpy as np
t=np.linspace(0,10,100)
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(t,np.sin(t),label="sin(t)")
ax.text(0.02,0.95,"(a)",transform=ax.transAxes)
ax.set_xlabel("t")
Use the left and right axes
import matplotlib.pyplot as plt
import numpy as np
t=np.linspace(0,10,100)
fig=plt.figure()
ax1=fig.add_subplot(111)
ax1.plot(t,np.sin(t),color="tab:blue")
ax1.set_xlabel("t")
ax1.set_ylabel("sin(t)")
ax2=ax1.twinx()
ax2.plot(t,np.tan(t),color="tab:orange")
ax2.set_ylim(-10,10)
ax2.set_ylabel("tan(t)")
Use left and right axes, add common legend
import matplotlib.pyplot as plt
import numpy as np
t=np.linspace(0,10,100)
fig=plt.figure()
ax1=fig.add_subplot(111)
ln1=ax1.plot(t,np.sin(t),color="tab:blue",label="sin(t)")
ax1.set_xlabel("t")
ax1.set_ylabel("sin(t)")
ax2=ax1.twinx()
ln2=ax2.plot(t,np.tan(t),color="tab:orange",label="tan(t)")
ax2.set_ylim(-10,10)
ax2.set_ylabel("tan(t)")
ln=ln1+ln2
lab=[l.get_label() for l in ln]
ax2.legend(ln,lab,loc=4)
Save the file
Install dvipng and cm-super
sudo apt update
sudo apt install cm-super
sudo apt install dvipng
plt.savefig("hoge.pdf")
To minimize margins
plt.savefig("hoge.pdf",bbox_inches="tight",pad_inches=0)
If you set pad_inchdes to 0, it may be just barely cut when using png, etc. In that case, put a smaller value. If you want a transparent background, set transparent=True.
LaTeX Specifications
import matplotlib.pyplot as plt
import numpy as np
t=np.linspace(0,5,100)
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(t,np.exp(-t)*np.sin(t))
ax.set_title(r"$x=e^{-t} \sin 5t$")
ax.set_xlabel(r"$t$")
ax.set_ylabel(r"$x$")
Full LaTeX specification
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
t=np.linspace(0,5,100)
mpl.rc("text",usetex=True)
mpl.rcParams["text.latex.preamble"]= r"\usepackage{cmbright,amsmath,amssymb}"
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(t,np.exp(-t)*np.sin(t))
ax.set_title(r"$x=e^{-t} \sin 5t$")
ax.set_xlabel(r"$t$")
ax.set_ylabel(r"$x$")
Change font size
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
t=np.linspace(0,5,100)
mpl.rcParams["font.size"]=16
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(t,np.exp(-t)*np.sin(t))
ax.set_title(r"$x=e^{-t} \sin 5t$")
ax.set_xlabel(r"$t$")
ax.set_ylabel(r"$x$")
Use Japanese
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
t=np.linspace(0,5,100)
mpl.rcParams["font.family"]="IPAexGochic"
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(t,np.exp(-t)*np.sin(t))
ax.set_title(r"$x=e^{-t} \sin 5t$のグラフ")
ax.set_xlabel("時間")
ax.set_ylabel("減衰振動")
Using Japanese in LaTeX Specifications
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
t=np.linspace(0,5,100)
mpl.rc("text",usetex=True)
mpl.rcParams["text.latex.preamble"]= r"\usepackage{cmbright,amsmath,amssymb}" r"\usepackage[whole]{bxcjkjatype}"
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(t,np.exp(-t)*np.sin(t))
ax.set_title(r"$x=e^{-t} \sin 5t$のグラフ")
ax.set_xlabel("時間")
ax.set_ylabel("減衰振動")
Binary file loading
import numpy as np
f=open("hoge.bin")
data=np.fromfile(f,dtype="f8",sep="")
Reading ASCII files
I think pandas.read_table reads ascii files rather smarter than numpy.genfromtxt. If necessary, then use numpy.array to make it into an array.
import numpy as np
import pandas as pd
data=pd.read_table("hoge.dat",sep="\s+",header=None)
data=np.array(data)