文章目錄
- 實例1 學會使用tex/latex
- 實例2 學會畫坐標軸
- 2.1過程
- 2.2 典型例子
- 2.2.1 一條帶箭頭的豎線
- 2.2.2 坐標系
- 2.2.3 坐標系上畫三角函數(shù)
實例1 學會使用tex/latex
第一眼看這個圖的時候覺得很震撼,代碼來自官網useTex demo,以及Latex
代碼在本例題末尾
首先需要注意的是,使用latex時可能會比不使用慢,因為需要調用到latex里面的一些程序,但latex語言美觀。
使用latex最簡單的方式就是加’$'符號(記住要加r),同時在開頭使用rc,設置usetex=True
(第一次調用會比較耗時間,之后就比較快了)
import matplotlib.pyplot as plt
plt.rc('text', usetex=True)
import
numpy
as
np
import
matplotlib
.
pyplot
as
plt
plt
.
rc
(
'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$'
)
plt
.
text
(
-
1.5
,
.5
,
r
'$\Omega=\theta+\delta+\phi$'
)
#在位置(-1.5,0.5)處開始寫公式
plt
.
show
(
)
下面的代碼是上面第一個圖的代碼
import
numpy
as
np
import
matplotlib
.
pyplot
as
plt
plt
.
rc
(
'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
(
arrowstyle
=
"<->"
,
connectionstyle
=
"arc3"
)
)
plt
.
text
(
0
,
0.1
,
r
'$\delta$'
,
{
'color'
:
'black'
,
'fontsize'
:
24
,
'ha'
:
'center'
,
'va'
:
'center'
,
'bbox'
:
dict
(
boxstyle
=
"round"
,
fc
=
"white"
,
ec
=
"black"
,
pad
=
0.2
)
}
)
# Use tex in labels
plt
.
xticks
(
(
-
1
,
0
,
1
)
,
(
'$-1$'
,
r
'$\pm 0$'
,
'$+1$'
)
,
color
=
'k'
,
size
=
20
)
# Left Y-axis labels, combine math mode and text mode
plt
.
ylabel
(
r
'\bf{phase field} $\phi$'
,
{
'color'
:
'C0'
,
'fontsize'
:
20
}
)
plt
.
yticks
(
(
0
,
0.5
,
1
)
,
(
r
'\bf{0}'
,
r
'\bf{.5}'
,
r
'\bf{1}'
)
,
color
=
'k'
,
size
=
20
)
# Right Y-axis labels
plt
.
text
(
1.02
,
0.5
,
r
"\bf{level set} $\phi$"
,
{
'color'
:
'C2'
,
'fontsize'
:
20
}
,
horizontalalignment
=
'left'
,
verticalalignment
=
'center'
,
rotation
=
90
,
clip_on
=
False
,
transform
=
plt
.
gca
(
)
.
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*}"
plt
.
text
(
1
,
0.9
,
eq1
,
{
'color'
:
'C2'
,
'fontsize'
:
18
}
,
va
=
"top"
,
ha
=
"right"
)
# 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*}'
plt
.
text
(
0.18
,
0.18
,
eq2
,
{
'color'
:
'C0'
,
'fontsize'
:
16
}
)
plt
.
text
(
-
1
,
.30
,
r
'gamma: $\gamma$'
,
{
'color'
:
'r'
,
'fontsize'
:
20
}
)
plt
.
text
(
-
1
,
.18
,
r
'Omega: $\Omega$'
,
{
'color'
:
'b'
,
'fontsize'
:
20
}
)
實例2 學會畫坐標軸
一個簡單的方法是直接畫兩條線,代表x,y,但是這樣不美觀,也不好用
2.1過程
matplotlib畫圖的時候總是默認的方框,有時候想展示的是x,y左邊軸。
繪制坐標系需要用到axisartist,這個可以參考官網axisartist
例外也可以參考博文Python-matplotlib繪制帶箭頭x-y坐標軸圖形
官網上給的一個復雜的demo是
可見axisartist在畫線上還是很實用的。
所以本小節(jié)從axisartist說起
創(chuàng)建子圖
子圖是通過subplot來繪制的,111表示一行一列只有一個圖,第三個1是表示第一個圖。
如221表示有兩行兩列(則有四個圖)第一個圖,分別展示如下。
import
matplotlib
.
pyplot
as
plt
import
mpl_toolkits
.
axisartist
as
AA
fig
=
plt
.
figure
(
)
#創(chuàng)建畫布
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)
#在第一個軸(即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
)
# 可以將參數(shù)名去掉直接寫(0,0)
#畫一條經過x=0.5的y軸,圖見下
ax
.
axis
[
'x=0.5'
]
=
ax
.
new_floating_axis
(
nth_coord
=
1
,
value
=
0.5
)
import
matplotlib
.
pyplot
as
plt
import
mpl_toolkits
.
axisartist
as
AA
fig
=
plt
.
figure
(
)
#創(chuàng)建畫布
ax
=
AA
.
Subplot
(
fig
,
111
)
fig
.
add_axes
(
ax
)
#將坐標ax加入畫布中
#設置某些坐標軸不可見
ax
.
axis
[
'right'
,
'top'
,
'bottom'
]
.
set_visible
(
False
)
# 增加浮動軸
ax
.
axis
[
'y=0'
]
=
ax
.
new_floating_axis
(
nth_coord
=
0
,
value
=
0
)
#如果不想顯示坐標軸上的數(shù)字可以如下
#ax.axis["left"].toggle(ticklabels=False)
#給坐標軸添加文本
ax
.
axis
[
'y=0'
]
.
label
.
set_text
(
'y=0'
)
#將數(shù)字變成紅色
ax
.
axis
[
"left"
]
.
major_ticklabels
.
set_color
(
"r"
)
#所有軸的數(shù)字都變成紅色
#ax.axis[:].major_ticklabels.set_color("r")
#y軸范圍
ax
.
set_ylim
(
-
4
,
4
)
plt
.
show
(
)
ax
.
axis
[
'y=0'
]
.
set_axisline_style
(
'->'
,
size
=
1.5
)
2.2 典型例子
2.2.1 一條帶箭頭的豎線
(圖一:數(shù)字顯示在左邊,實心箭頭)
(圖2:數(shù)字顯示在右邊)
import
matplotlib
.
pyplot
as
plt
import
mpl_toolkits
.
axisartist
as
axisartist
#定義一個畫軸線的函數(shù)
def
setup_axes
(
fig
,
rect
)
:
ax
=
axisartist
.
Subplot
(
fig
,
rect
)
#建立子圖
fig
.
add_axes
(
ax
)
#子圖加入畫布
ax
.
set_ylim
(
-
0.1
,
1.5
)
ax
.
set_yticks
(
[
0
,
1
]
)
ax
.
axis
[
:
]
.
set_visible
(
False
)
#四條邊框線都消失
ax
.
axis
[
"x"
]
=
ax
.
new_floating_axis
(
1
,
0.5
)
#y軸經過x=0.5
ax
.
axis
[
"x"
]
.
set_axisline_style
(
"->"
,
size
=
1.5
)
#空心箭頭
#('-|>')實心箭頭
return
ax
fig
=
plt
.
figure
(
figsize
=
(
3
,
2.5
)
)
#建立畫布,尺寸為長3寬2.5
fig
.
subplots_adjust
(
top
=
0.8
)
#可不要
ax1
=
setup_axes
(
fig
,
"111"
)
#“111”可以寫成111,即str或者int都可以
ax1
.
axis
[
"x"
]
.
set_axis_direction
(
"left"
)
#數(shù)字顯示在左邊right則右邊
plt
.
show
(
)
2.2.2 坐標系
import
matplotlib
.
pyplot
as
plt
import
mpl_toolkits
.
axisartist
as
axisartist
def
setup_axes
(
fig
,
rect
)
:
ax
=
axisartist
.
Subplot
(
fig
,
rect
)
fig
.
add_axes
(
ax
)
#手動設置y軸,從-0.1開始,我對x軸不做設置
#大家可以對比x跟y有什么不同
ax
.
set_ylim
(
-
0.1
,
1.5
)
ax
.
set_yticks
(
[
0
,
0.1
,
0.5
]
)
#手動寫參數(shù)
#4個邊框線不可見
ax
.
axis
[
:
]
.
set_visible
(
False
)
#第2條線,即y軸
ax
.
axis
[
"y"
]
=
ax
.
new_floating_axis
(
1
,
0
)
ax
.
axis
[
"y"
]
.
set_axisline_style
(
"-|>"
,
size
=
1.5
)
#第一條線,x軸
ax
.
axis
[
"x"
]
=
ax
.
new_floating_axis
(
0
,
0
)
ax
.
axis
[
"x"
]
.
set_axisline_style
(
"-|>"
,
size
=
1.5
)
return
(
ax
)
fig
=
plt
.
figure
(
figsize
=
(
8
,
8
)
)
#設置畫布大?。ńㄗh可以默認為空括號,不設置大?。?
ax1
=
setup_axes
(
fig
,
111
)
ax1
.
axis
[
"x"
]
.
set_axis_direction
(
"bottom"
)
#x軸數(shù)字顯示在線的下方
ax1
.
axis
[
'y'
]
.
set_axis_direction
(
'right'
)
#y軸數(shù)字顯示在線的右邊
plt
.
show
(
)
2.2.3 坐標系上畫三角函數(shù)
import
mpl_toolkits
.
axisartist
as
axisartist
import
numpy
as
np
#定義坐標軸函數(shù)
def
setup_axes
(
fig
,
rect
)
:
ax
=
axisartist
.
Subplot
(
fig
,
rect
)
fig
.
add_axes
(
ax
)
ax
.
set_ylim
(
-
10
,
10
)
#自定義刻度
# ax.set_yticks([-10, 0,9])
ax
.
set_xlim
(
-
10
,
10
)
ax
.
axis
[
:
]
.
set_visible
(
False
)
#第2條線,即y軸,經過x=0的點
ax
.
axis
[
"y"
]
=
ax
.
new_floating_axis
(
1
,
0
)
ax
.
axis
[
"y"
]
.
set_axisline_style
(
"-|>"
,
size
=
1.5
)
# 第一條線,x軸,經過y=0的點
ax
.
axis
[
"x"
]
=
ax
.
new_floating_axis
(
0
,
0
)
ax
.
axis
[
"x"
]
.
set_axisline_style
(
"-|>"
,
size
=
1.5
)
return
(
ax
)
#設置畫布
fig
=
plt
.
figure
(
figsize
=
(
8
,
8
)
)
#建議可以直接plt.figure()不定義大小
ax1
=
setup_axes
(
fig
,
111
)
ax1
.
axis
[
"x"
]
.
set_axis_direction
(
"bottom"
)
ax1
.
axis
[
'y'
]
.
set_axis_direction
(
'right'
)
#在已經定義好的畫布上加入三角函數(shù)
x
=
np
.
arange
(
-
15
,
15
,
0.1
)
y
=
np
.
sin
(
x
)
plt
.
plot
(
x
,
y
)
plt
.
show
(
)
更多文章、技術交流、商務合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
