我正在尝试生成一个包含四个季度的图,每个季度都有一些文字说明该季度的情况。但是,当我尝试换行文本时,我不知道如何设置文本限制。例如,在附图中,我想将文本限制为x=0。但是,它会一直持续到 x 轴限制的末尾。请查找附件中的代码以及代码生成的相应绘图。
import matplotlib
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from textwrap import wrap
font = {'size': 22}
matplotlib.rc('font', **font)
fig = plt.figure(figsize=(8, 8))
plt.axis([-10, 10, -10, 10])
ax = plt.gca()
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('center')
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
plt.xlabel('Energy ($kWh$)')
ax.xaxis.set_label_coords(0.85, .48)
ax.xaxis.set_ticks_position('bottom')
ax.set_xlim(-10, 10)
ax.xaxis.set_ticks([])
plt.ylabel('Discomfort ($\%$)', rotation=0)
ax.yaxis.set_label_coords(0.7, 0.01)
ax.yaxis.set_ticks_position('left')
ax.set_ylim(-10, 10)
ax.yaxis.set_ticks([])
ax.annotate(
'Reference Point', xy=(0, 0), xycoords='data',
xytext=(-10, 2), textcoords='data', wrap=True,
arrowprops=dict(facecolor='black'))
t = "This is a really long string that I'd rather have wrapped so that
it doesn't go outside of the figure, but if it's long enough it will go
off the top or bottom!"
ax.text(-10, 3.5, t, ha='left', wrap=True, fontsize=20)
plt.tight_layout()
plt.savefig('sample.png')