带换行符的 Matplotlib 图例
Matplotlib legend with line breaks
我正在尝试将我的回归系数作为 LaTeX 公式添加到具有多行的子图的图例中:
fig, ((plt1, plt2, plt3), (plt4, plt5, plt6)) = plt.subplots(2, 3, figsize=(22,10), sharex='col', sharey='row')
plot1, = plt1.plot('Normalized Times','Mean', linestyle='None', marker='o', color='#6E9EAF', markersize=marksize, data=Phase1_Temp)
plot1_R, = plt1.plot(Xdata_Phase1_Temp, Y_Phase1_Temp_Pred, linewidth=width_line, color=Orange)
plt1.legend([plot1_R], ["$f(x) = {m}*x +{b}$".format(m=np.round(A[1],2), b=np.round(A[0],2)) "\n" "$R2 = {r}$".format(r=np.round(A[2],2))])
当我 运行 文件时,我为一个句柄调用第二个标签时得到无效语法:
"\n" "$R2 = {r}$".format(r=np.round(A[2],2))])
^
SyntaxError: invalid syntax
有人知道如何解决这个问题吗?
在 python 中,您可以像这样连接字符串:
"hello " "world"
并生产 "hello world"
。但是如果你这样做,就是一个语法错误:
"{} ".format("hello") "world"
所以如果你想从 format()
的输出连接起来,使用 +
:
"{} ".format("hello") + "world"
在你的情况下(为便于阅读而添加换行符):
plt1.legend([plot1_R], [
"$f(x) = {m}*x +{b}$".format(m=np.round(A[1],2), b=np.round(A[0],2))
+ "\n"
+ "$R2 = {r}$".format(r=np.round(A[2],2))
])
考虑使用单个字符串来格式化
import matplotlib.pyplot as plt
A = [5,4,3]
fig, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(2, 3, figsize=(22,10), sharex='col', sharey='row')
plot1, = ax1.plot([0,1], linestyle='None', marker='o', color='#6E9EAF', markersize=5)
plot1_R, = ax1.plot([0,1], linewidth=2, color="orange")
ax1.legend([plot1_R],
["$f(x) = {m}*x +{b}$\n$R2 = {r}$".format(m=np.round(A[1],2),
b=np.round(A[0],2), r=np.round(A[2],2))])
plt.show()
此外,f
-strings 在这里可能会派上用场,因为舍入是在格式级别执行的。
ax1.legend([plot1_R], [f"$f(x) = {A[1]:.2f}*x +{A[0]:.2f}$\n$R2 = {A[2]:.2f}$"])
我正在尝试将我的回归系数作为 LaTeX 公式添加到具有多行的子图的图例中:
fig, ((plt1, plt2, plt3), (plt4, plt5, plt6)) = plt.subplots(2, 3, figsize=(22,10), sharex='col', sharey='row')
plot1, = plt1.plot('Normalized Times','Mean', linestyle='None', marker='o', color='#6E9EAF', markersize=marksize, data=Phase1_Temp)
plot1_R, = plt1.plot(Xdata_Phase1_Temp, Y_Phase1_Temp_Pred, linewidth=width_line, color=Orange)
plt1.legend([plot1_R], ["$f(x) = {m}*x +{b}$".format(m=np.round(A[1],2), b=np.round(A[0],2)) "\n" "$R2 = {r}$".format(r=np.round(A[2],2))])
当我 运行 文件时,我为一个句柄调用第二个标签时得到无效语法:
"\n" "$R2 = {r}$".format(r=np.round(A[2],2))])
^
SyntaxError: invalid syntax
有人知道如何解决这个问题吗?
在 python 中,您可以像这样连接字符串:
"hello " "world"
并生产 "hello world"
。但是如果你这样做,就是一个语法错误:
"{} ".format("hello") "world"
所以如果你想从 format()
的输出连接起来,使用 +
:
"{} ".format("hello") + "world"
在你的情况下(为便于阅读而添加换行符):
plt1.legend([plot1_R], [
"$f(x) = {m}*x +{b}$".format(m=np.round(A[1],2), b=np.round(A[0],2))
+ "\n"
+ "$R2 = {r}$".format(r=np.round(A[2],2))
])
考虑使用单个字符串来格式化
import matplotlib.pyplot as plt
A = [5,4,3]
fig, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(2, 3, figsize=(22,10), sharex='col', sharey='row')
plot1, = ax1.plot([0,1], linestyle='None', marker='o', color='#6E9EAF', markersize=5)
plot1_R, = ax1.plot([0,1], linewidth=2, color="orange")
ax1.legend([plot1_R],
["$f(x) = {m}*x +{b}$\n$R2 = {r}$".format(m=np.round(A[1],2),
b=np.round(A[0],2), r=np.round(A[2],2))])
plt.show()
此外,f
-strings 在这里可能会派上用场,因为舍入是在格式级别执行的。
ax1.legend([plot1_R], [f"$f(x) = {A[1]:.2f}*x +{A[0]:.2f}$\n$R2 = {A[2]:.2f}$"])