如何在 yaxis matplotlib 中添加文本 "H_m" 而不是 100%?
How to add text "H_m" instead of 100% in yaxis matplotlib?
我想在我的 y 轴上添加文本“H_m”而不是 100% 符号,并增加标签中的字体大小,但我无法弄清楚。这是我得到的:
import math
import itertools
import csv
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
fig = plt.figure(3, figsize = (10,6))
ax = fig.add_subplot(1,1,1)
plt.rc('xtick', labelsize = 18) # fontsize of the tick labels
plt.rc('ytick', labelsize = 18) # fontsize of the tick labels
plt.grid(True)
#plt.xlim((-0.2, 2))
#plt.ylim((0, 20))
plt.xlabel(r'$t$ [s]', fontsize = 20)
plt.ylabel(r'$H_a$ [A/m]', fontsize = 20)
marker = itertools.cycle(('.', '+', 's', 'o', '*', 'v', 'H', 'D'))
X_1, Y_1 = list(), list()
### Open external file ###
with open('data-lua.dat') as csvfile:
readCSV = csv.reader(csvfile, delimiter='\t')
for row in readCSV:
X_1.append(float(row[0]))
Y_1.append(float(row[1])/7961.783439)
plt.plot(X_1, Y_1, color='k', linestyle='-', marker='o', markersize ='5')
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
plt.savefig('data-lua.png', format = 'png', dpi = 300, bbox_inches = 'tight')
plt.show()
输出:
这是我想要的示例:
先将您的 y 值归一化到 [0,1]
,然后使用 MultipleLocator(0.2)
生成 5 个间隔怎么样?
y = (y - y.min()) / (y.max() - y.min())
ax.set_ylim([0,1])
ax.yaxis.set_major_locator(mtick.MultipleLocator(0.2))
ax.set_yticklabels(["", "0%", "20%", "40%", "60%", "80%", "$H_m$", ""])
注意,所以yticklabels的长度也要相应调整。 yticklabels 看起来像这样:
我想在我的 y 轴上添加文本“H_m”而不是 100% 符号,并增加标签中的字体大小,但我无法弄清楚。这是我得到的:
import math
import itertools
import csv
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
fig = plt.figure(3, figsize = (10,6))
ax = fig.add_subplot(1,1,1)
plt.rc('xtick', labelsize = 18) # fontsize of the tick labels
plt.rc('ytick', labelsize = 18) # fontsize of the tick labels
plt.grid(True)
#plt.xlim((-0.2, 2))
#plt.ylim((0, 20))
plt.xlabel(r'$t$ [s]', fontsize = 20)
plt.ylabel(r'$H_a$ [A/m]', fontsize = 20)
marker = itertools.cycle(('.', '+', 's', 'o', '*', 'v', 'H', 'D'))
X_1, Y_1 = list(), list()
### Open external file ###
with open('data-lua.dat') as csvfile:
readCSV = csv.reader(csvfile, delimiter='\t')
for row in readCSV:
X_1.append(float(row[0]))
Y_1.append(float(row[1])/7961.783439)
plt.plot(X_1, Y_1, color='k', linestyle='-', marker='o', markersize ='5')
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
plt.savefig('data-lua.png', format = 'png', dpi = 300, bbox_inches = 'tight')
plt.show()
输出:
先将您的 y 值归一化到 [0,1]
,然后使用 MultipleLocator(0.2)
生成 5 个间隔怎么样?
y = (y - y.min()) / (y.max() - y.min())
ax.set_ylim([0,1])
ax.yaxis.set_major_locator(mtick.MultipleLocator(0.2))
ax.set_yticklabels(["", "0%", "20%", "40%", "60%", "80%", "$H_m$", ""])
注意