天天看點

python,matplotlib繪圖文字text下标問題

在正常的一個繪圖之後,我們可以得到

代碼:

import numpy as np
import matplotlib.pyplot as plt


xs1 = np.ones(100)
ys1 = range(1, 101)
zs1 = np.zeros(100)

xs2 = np.ones(100) * 2
ys2 = range(1, 101)
zs2 = np.zeros(100)

xs3 = np.ones(100) * 3
ys3 = range(1, 101)
zs3 = np.zeros(100)

xs4 = np.ones(100) * 4
ys4 = range(1, 101)
zs4 = np.zeros(100)

# Plot
# ax = plt.figure().add_subplot(projection='3d')

from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure('figure10',dpi=500, figsize=(8,8))
ax = Axes3D(fig)

ax.get_proj = lambda: np.dot(Axes3D.get_proj(ax), np.diag([0.8, 1, 0.5, 1]))

ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
ax.w_yaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
ax.w_zaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))
ax.plot(xs1, ys1, zs1, lw=2, color=[0.8, 0.33, 0])
ax.plot(xs2, ys2, zs2, lw=2, color=[0.33, 0.8, 0])
ax.plot(xs3, ys3, zs3, lw=2, color=[0, 0.5, 0.8])
ax.plot(xs4, ys4, zs4, lw=2, color=[0.8, 0.1, 0.5])
ax.set_xlabel("The Number of actuator")
ax.set_ylabel("Flight time")
ax.set_zlabel("The value of u_a")
# ax.set_zlabel("The value of $u_a$")
# plt.legend(labels=['$u_{a1}$','$u_{a2}$','$u_{a3}$','$u_{a4}$'], loc='best')
plt.legend(labels=['u_a1','u_a2','u_a3','u_a4'], loc='best')
plt.rcParams['font.sans-serif'] = ['Arial']
plt.rcParams['axes.unicode_minus'] = False
ax.set_title("Addective fault simulation")
ax.set(xlim=[1, 4], ylim=[0, 100], zlim=[-0.5, 0.3])
ax.view_init(32, -32)
plt.xticks([1,2,3,4])
# plt.show()
plt.savefig('./fig/figure10(a).png')
plt.close()
           

繪圖結果:

python,matplotlib繪圖文字text下标問題

問題是,我們希望下标部分是真實存在的,那麼我們可以使用以下代碼,如果是下标隻有一個字母或數字,隻需要在這個部分的兩側添加$ $即可,如果涉及到多個字元,那麼隻需把期望的下标部分加上{}。

ax.set_zlabel("The value of $u_a$")
plt.legend(labels=['$u_{a1}$','$u_{a2}$','$u_{a3}$','$u_{a4}$'], loc='best')

           

效果如下:

python,matplotlib繪圖文字text下标問題