英文:
Write text outside plot but inside figure
问题
我想在每行的左边添加一段文本,但我一直无法做到。
我尝试使用
plt.annotate('距离 $\leq6.5$ $km$', (1, 1))
plt.text(1, 1, '距离 $\leq6.5$ $km$')
但它总是在最后一个子图中写入,如截图所示。
这是我的当前程序:
import numpy as np
import numpy.random as rd
import matplotlib.pyplot as plt
Dic_Values = {'inf_6.5':{'v_moy':rd.randint(15,25, 20), 'cad':rd.randint(75,100,20), 'f_moy':rd.randint(130,180,20)},
'6.5_9.5':{'v_moy':rd.randint(15,25, 11), 'cad':rd.randint(75,100,11), 'f_moy':rd.randint(130,180,11)},
'sup_9.5':{'v_moy':rd.randint(15,25, 4), 'cad':rd.randint(75,100,4), 'f_moy':rd.randint(130,180,4)}}
def bilan_course():
types = list(Dic_Values.keys())
c = 0
plt.figure(figsize=(21,7))
Labels = ['S_moy', 'Cad', 'F_moy']
Y_axis = ['S(以 $km \cdot h^{-1}$ 为单位)', 'Cad(以 $步数\cdot 分^{-1}$ 为单位)', 'F(以 $每分钟心跳数\cdot 分^{-1}$ 为单位)']
for ty in types:
Donnees = Dic_Values[ty]
Cles = list(Donnees.keys())
for v in range(len(Cles)):
plt.subplot(int('33'+str(3*c+v+1)))
plt.plot(Donnees[Cles[v]], label=Labels[v])
plt.xlabel('比赛')
plt.ylabel(Y_axis[v])
plt.title(Labels[v])
plt.ylim(0, max(Donnees[Cles[v]])*1.1)
plt.legend(loc='best')
c+=1
plt.subplots_adjust(left=0.1, bottom=0.085, right=0.95, top=0.882, wspace=0.25, hspace=0.5)
plt.annotate('距离 $\leq6.5$ $km$', (1, 1), fontsize=12)
plt.suptitle('总结', fontweight='bold')
plt.show()
英文:
I would like to add a text at the left of each line but I've not been able to do so.
I tried using
plt.annotate('Distance $\leq6.5$ $km$', (1, 1))
plt.text(1, 1, 'Distance $\leq6.5$ $km$')
but it always resulted into writing in the last subplot as shown on the screenshot.
Here is my current program:
import numpy as np
import numpy.random as rd
import matplotlib.pyplot as plt
Dic_Values = {'inf_6.5':{'v_moy':rd.randint(15,25, 20), 'cad':rd.randint(75,100,20), 'f_moy':rd.randint(130,180,20)},
'6.5_9.5':{'v_moy':rd.randint(15,25, 11), 'cad':rd.randint(75,100,11), 'f_moy':rd.randint(130,180,11)},
'sup_9.5':{'v_moy':rd.randint(15,25, 4), 'cad':rd.randint(75,100,4), 'f_moy':rd.randint(130,180,4)}}
def bilan_course():
types = list(Dic_Values.keys())
c = 0
plt.figure(figsize=(21,7))
Labels = ['S_moy', 'Cad', 'F_moy']
Y_axis = ['S (in $km \cdot h^{-1}$)', 'Cad (in $step\cdot min^{-1}$)', 'F (in $beat\cdot min^{-1}$)']
for ty in types:
Donnees = Dic_Values[ty]
Cles = list(Donnees.keys())
for v in range(len(Cles)):
plt.subplot(int('33'+str(3*c+v+1)))
plt.plot(Donnees[Cles[v]], label=Labels[v])
plt.xlabel('Course')
plt.ylabel(Y_axis[v])
plt.title(Labels[v])
plt.ylim(0, max(Donnees[Cles[v]])*1.1)
plt.legend(loc='best')
c+=1
plt.subplots_adjust(left=0.1, bottom=0.085, right=0.95, top=0.882, wspace=0.25, hspace=0.5)
plt.annotate('Distance $\leq6.5$ $km$', (1, 1), fontsize=12)
plt.suptitle('Summary', fontweight='bold')
plt.show()
答案1
得分: 1
我猜现在是时候让你熟悉一下 matplotlib 的“显式”API了 (查看matplotlib API 文档)
简而言之,如果你使用 plt.< 做某事 >
,matplotlib 将始终尝试识别最近使用的坐标轴并使用它!要操作显式坐标轴,请调用你实际想要使用的坐标轴对象上的方法!
以下是一个快速的片段,可以帮助你入门:
import matplotlib.pyplot as plt
f, axes = plt.subplots(3, 3, figsize=(10, 6))
for i, ax in enumerate(axes.flat):
ax.plot([1,2,3])
ax.set_title(f"this is ax {i}")
ax.set_xlabel(f"x-label for ax {i}")
ax.set_ylabel(f"y-label for ax {i}")
ax.text(.25, .1, f"text on ax {i}", ha="left", va="bottom",
transform=ax.transAxes, color="r")
f.tight_layout()
英文:
I guess it's time to make yourself familiar with the "expicit" API of matplotlib
(see matpltolib docs on APIs)
In short, if you use plt.< do something >
matpltolib will always try to identify the most recently used axes and use it!
To address an explicit axes, call the method on the axes object you actually want to use!
Here's a quick snippet that should help you to get started:
import matplotlib.pyplot as plt
f, axes = plt.subplots(3, 3, figsize=(10, 6))
for i, ax in enumerate(axes.flat):
ax.plot([1,2,3])
ax.set_title(f"this is ax {i}")
ax.set_xlabel(f"x-label for ax {i}")
ax.set_ylabel(f"y-label for ax {i}")
ax.text(.25, .1, f"text on ax {i}", ha="left", va="bottom",
transform=ax.transAxes, color="r")
f.tight_layout()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论