matplot 多图、LaTeX 格式

时间:2022-11-30 06:36:19

1.子图(subplot):

#coding=utf-8
import matplotlib.pyplot as plt
fig = plt.figure()
# fig.add_subplot(numrows, numcols, fignum)
# 这个图像可以生成 numrows*numcols 这么多个图像,fignum 表示第几个图像,也表示了位置,值为 1- numrows*numcols,类似数组的存放方式
# 当 numrows < 10 numcols < 10 fignum < 10 ,可以使用缩写的方式
# 下面表示生成 3*4 个图形,分别占的位置是 1 3 9 ,所以这三个图形的表示是 341\343\349
# 一个 ax 表示一个可以画的XY
ax1 = fig.add_subplot(341)
ax1.plot([1,2,3],[1,2,3])
ax2 = fig.add_subplot(343)
ax2.plot([1,2,3],[1,2,3])
ax3 = fig.add_subplot(349)
ax3.plot([1,2,3],[1,2,3])
plt.show()
matplot 多图、LaTeX 格式

2. 共享轴:

#coding=utf-8
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0.,np.e,0.01)
y1 = np.exp(-x)
y2 = np.log(x)
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(x,y1)
ax1.set_ylabel('y values for exp(-x)')
# 让 X 轴共享
ax2 = ax1.twinx()
ax2.plot(x,y2,'r')
ax2.set_ylabel('Y values for ln(x)')
ax2.set_xlabel('both')
ax2.set_xlim([0,np.e])
plt.show()
matplot 多图、LaTeX 格式
#coding=utf-8
import matplotlib as mpl
mpl.rcParams['font.size'] = 10
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0.,20,0.01)
fig = plt.figure()
ax1 = fig.add_subplot(311)
y1 = np.exp(x/6.)
ax1.plot(x,y1)
ax1.grid(True)
# 指定了 ycale 所以显示指数
ax1.set_yscale('log')
ax1.set_ylabel('Log Y')
ax2 = fig.add_subplot(312)
y2 = np.cos(np.pi*x)
#
ax2.semilogx(x,y2)
ax2.set_xlim([0,20])
ax2.grid(True)
ax2.set_ylabel('Log X')
ax3 = fig.add_subplot(313)
y3 = np.exp(x/4.)
# 指定下标显示
ax3.loglog(x,y3,basex=3)
ax3.grid(True)
ax3.set_ylabel('Log X and Y')
plt.show()
matplot 多图、LaTeX 格式

#coding=utf-8
import matplotlib as mpl
mpl.rcParams['font.size'] = 11
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(11)
fig = plt.figure()
ax1 = fig.add_subplot(311)
ax1.plot(x,x)
# 如果没有,则 X 轴是不一样的
ax2 = fig.add_subplot(312)
ax2.plot(2*x,2*x)
ax2 = fig.add_subplot(313,sharex=ax1)
ax2.plot(3*x,3*x)
plt.show()
matplot 多图、LaTeX 格式

#coding=utf-8
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import datetime as dt
dates = [dt.datetime.today() + dt.timedelta(days=i) for i in range(10)]
values = np.random.rand(len(dates))
# 使用时间图
plt.plot_date(mpl.dates.date2num(dates),values,linestyle='-')
plt.show()
matplot 多图、LaTeX 格式
plotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import datetime as dt
fig = plt.figure()
ax2 = fig.add_subplot(212)
date2_1 = dt.datetime(2008,8,23)
date2_2 = dt.datetime(2008,10,2)
delta2 = dt.timedelta(days=1)
dates2 = mpl.dates.drange(date2_1,date2_2,delta2)
y2 = np.random.rand(len(dates2))
ax2.plot_date(dates2,y2,linestyle='-')
dateFmt = mpl.dates.DateFormatter('%Y-%m-%d')
ax2.xaxis.set_major_formatter(dateFmt)
daysLoc = mpl.dates.DayLocator()
hoursLoc = mpl.dates.HourLocator(interval=6)
ax2.xaxis.set_major_locator(daysLoc)
ax2.xaxis.set_minor_locator(hoursLoc)
fig.autofmt_xdate(bottom=0.18) # adjust for date labels display
fig.subplots_adjust(left=0.18)
ax1 = fig.add_subplot(211)
date1_1 = dt.datetime(2008, 9, 23)
date1_2 = dt.datetime(2009, 2, 16)
delta1 = dt.timedelta(days=10)
dates1 = mpl.dates.drange(date1_1, date1_2, delta1)
y1 = np.random.rand(len(dates1))
ax1.plot_date(dates1, y1, linestyle='-');
monthsLoc = mpl.dates.MonthLocator()
weeksLoc = mpl.dates.WeekdayLocator()
ax1.xaxis.set_major_locator(monthsLoc)
ax1.xaxis.set_minor_locator(weeksLoc)
monthsFmt = mpl.dates.DateFormatter('%b')
ax1.xaxis.set_major_formatter(monthsFmt)
plt.show()
matplot 多图、LaTeX 格式

使用  LaTeX 格式:

#coding=utf-8
import matplotlib as mpl
mpl.rcParams['font.size'] = 30
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim([1,6])
ax.set_ylim([1,9])
ax.text(2,8,r'$ \mu \alpha \tau \pi \lambda \omega \tau \lambda \iota \beta $')
ax.text(2,6,r"$ \lim_{x \rightarrow 0} \frac{1}{x} $")
ax.text(2,4,r"$ a \ \leq \ b \ \leq \ c \ \rightarrow \ a \ \leq \ c$")
ax.text(2,2,r"$ \sum_{i=1}^{\infty}\ x_i^2$")
ax.text(4,8,r"$ \sin(0) = \cos(\frac{\pi}{2})$")
ax.text(4,6,r"$ \sqrt[3]{x} = \sqrt{y}$")
ax.text(4,4,r"$ \neg (a \wedge b) \Leftrightarrow \neg a \vee \neg b$")
ax.text(4,2,r"$ \int_a^b f(x)dx$")
plt.show()
matplot 多图、LaTeX 格式

参考 《Matplotlib.for.Python.Developers》