Python:使用matplotlib在图纸上居中绘制和图例
我有一个极坐标图,右边是它的传说。图例可以包含1到40个项目,其长度介于4到32个字符之间。我的目标是使绘图和图例居中,就像它是matplotlib工作表上的一个对象,或者更确切地说是空白 我发现了以下内容,并根据自己的需要进行了调整:Python:使用matplotlib在图纸上居中绘制和图例,python,matplotlib,plot,centering,Python,Matplotlib,Plot,Centering,我有一个极坐标图,右边是它的传说。图例可以包含1到40个项目,其长度介于4到32个字符之间。我的目标是使绘图和图例居中,就像它是matplotlib工作表上的一个对象,或者更确切地说是空白 我发现了以下内容,并根据自己的需要进行了调整: import matplotlib.pyplot as plt import numpy as np x = np.arange(10) fig = plt.figure() ax = plt.subplot(111, polar=True) for ap
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(10)
fig = plt.figure()
ax = plt.subplot(111, polar=True)
for ap in aps:
ax.plot(angles, ap, label="----------32-Characters---------")
# Shrink current axis by 20%
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
# Put a legend to the right of the current axis
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.show()
问题是它不能根据标签的长度进行调整。当标签长度为4-8个字符时,一切都非常适合,但当32个字符长的字符串出现时,它只会将绘图缩小20%,因此它们被切断
我想要的是绘图+图例作为一个对象,以图纸/页面/空白为中心,这样当有一个长标签时,绘图只是向左偏移多一点,以补偿它。
我希望你明白我的意思:我想你可以在问题的答案中找到你需要的东西-你必须画出数字,然后得到图例大小,如果太宽,再重新调整。@A谢谢,我会试试的!