pythonmatplotlib画图的⼏个实例--latex,坐标系等⽂章⽬录
实例1 学会使⽤tex/latex
第⼀眼看这个图的时候觉得很震撼,代码来⾃官⽹,以及
代码在本例题末尾
⾸先需要注意的是,使⽤latex时可能会⽐不使⽤慢,因为需要调⽤到latex⾥⾯的⼀些程序,但latex语⾔美观。使⽤latex最简单的⽅式就是加’$'符号(记住要加r),同时在开头使⽤rc,设置usetex=True
(第⼀次调⽤会⽐较耗时间,之后就⽐较快了)
import matplotlib.pyplot as plt
<('text', usetex=True)
如⼀个有latex公式的三⾓函数图
<('text', usetex=True)#使⽤latex
x = np.linspace(-3,3,100)
y = np.sin(x)
plt.plot(x,y,'r')
plt.xlabel(r'$\theta$')#⼀定要加r转义,避免将$读错
plt.ylabel(r'$\delta$')
<(-1.5,.5,r'$\Omega=\theta+\delta+\phi$')#在位置(-1.5,0.5)处开始写公式plt.show()
下⾯的代码是上⾯第⼀个图的代码
<('text', usetex=True)
# interface tracking profiles
N =500
delta =0.6
X = np.linspace(-1,1, N)
plt.plot(X,(1- np.tanh(4* X / delta))/2,# phase field tanh profiles
X,(1.4+ np.tanh(4* X / delta))/4,"C2",# composition profile
X, X <0,'k--')# sharp interface
# legend
plt.legend(('phase field','level set','sharp interface'),
shadow=True, loc=(0.01,0.48), handlelength=1.5, fontsize=16)
# the arrow
plt.annotate("", xy=(-delta /2.,0.1), xytext=(delta /2.,0.1),
arrowprops=dict(arrow, connection))
<(0,0.1, r'$\delta$',
{'color':'black','fontsize':24,'ha':'center','va':'center',
'bbox':dict(box, fc="white", ec="black", pad=0.2)})
# Use tex in labels
# Left Y-axis labels, combine math mode and text mode
plt.ylabel(r'\bf{phase field} $\phi$',{'color':'C0','fontsize':20})
# Right Y-axis labels
<(1.02,0.5, r"\bf{level set} $\phi$",{'color':'C2','fontsize':20},
horizontalalignment='left',
verticalalignment='center',
rotation=90,
clip_on=False,
a().transAxes)
# Use multiline environment inside a `text`.
# level set equations
eq1 = r"\begin{eqnarray*}"+ \
r"|\nabla\phi| &=& 1,\\"+ \
r"\frac{\partial \phi}{\partial t} + U|\nabla \phi| &=& 0 "+ \
r"\end{eqnarray*}"
<(1,0.9, eq1,{'color':'C2','fontsize':18}, va="top", ha="right")
round函数有几个参数
# phase field equations
eq2 = r'\begin{eqnarray*}'+ \
r'\mathcal{F} &=& \int f\left( \phi, c \right) dV, \\ '+ \
r'\frac{ \partial \phi } { \partial t } &=& -M_{ \phi } '+ \
r'\frac{ \delta \mathcal{F} } { \delta \phi }'+ \
r'\end{eqnarray*}'
<(0.18,0.18, eq2,{'color':'C0','fontsize':16})
<(-1,.30, r'gamma: $\gamma$',{'color':'r','fontsize':20})
<(-1,.18, r'Omega: $\Omega$',{'color':'b','fontsize':20})
实例2 学会画坐标轴
⼀个简单的⽅法是直接画两条线,代表x,y,但是这样不美观,也不好⽤2.1过程
matplotlib画图的时候总是默认的⽅框,有时候想展⽰的是x,y左边轴。
绘制坐标系需要⽤到axisartist,这个可以参考官⽹
例外也可以参考博⽂
官⽹上给的⼀个复杂的demo是
可见axisartist在画线上还是很实⽤的。
所以本⼩节从axisartist说起
创建⼦图
⼦图是通过subplot来绘制的,111表⽰⼀⾏⼀列只有⼀个图,第三个1是表⽰第⼀个图。如221表⽰有两⾏两列(则有四个图)第⼀个图,分别展⽰如下。
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as AA
fig = plt.figure()#创建画布
ax = AA.Subplot(fig,111)
fig.add_axes(ax)#将坐标ax加⼊画布中
(111)
(221)
上下左右(top, bottom,left,right)共四条线,即四个坐标轴,下⾯隐藏右边和上边
ax.axis['right'].set_visible(False)
ax.axis['top'].set_visible(False)
或者
ax.axis['right','top'].set_visible(False)
在y=0处加横坐标(horizontal axis)
#在第⼀个轴(即x轴,nth_coord是nth coordinate的简称,即第n个坐标轴)
#当nth_coord=1表⽰x轴。
#所以nth_coord=0, value=0表⽰画⼀条x轴,并经过y=0点。
ax.axis['y=0']= ax.new_floating_axis(nth_coord=0, value=0)
# 可以将参数名去掉直接写(0,0)
#画⼀条经过x=0.5的y轴,图见下
ax.axis['x=0.5']= ax.new_floating_axis(nth_coord=1, value=0.5)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。