python-3.x - 使用 ax.annotate 时如何适合行尾标签?
问题描述
我有这张图表,我很满意,只是我不能在行尾放置 4 个标签,而且当我将它保存为 pdf 时它看起来不太好。
我已经尝试在 ax.annotate 中将 figsize 更改为 figsize=(9,6) 和 xy=(0.984,y),这是我能得到的。
我还能调整什么以使右侧的标签更明显?
我的代码。
import matplotlib.pyplot as plt
import matplotlib.transforms as mtrans
from matplotlib.ticker import FixedLocator, FormatStrFormatter
#%matplotlib notebook
plt.style.use('seaborn-poster')
fig,ax = plt.subplots(figsize=(8,6), facecolor='w', edgecolor='blue')
my_colors=['orangered','seagreen', 'goldenrod', 'darkviolet', 'sienna']
ax.set_ylim(merged.min().min(), merged.max().max())
ax.set_ylim(-0.7, 31)
ax=merged.plot(y=['Kinver', 'Nomads', 'Plough', 'Alveley Royals', 'Alveley Oaks'],marker='o',figsize=(9,6),legend=None, color=my_colors, ax=ax)
selectcols=merged[['Kinver', 'Nomads', 'Plough', 'Alveley Royals', 'Alveley Oaks']]
for line, name in zip(ax.lines, selectcols.columns):
y = line.get_ydata()[-1]
ax.annotate(name, xy=(0.984,y), xytext=(6,0), color=line.get_color(),
xycoords = ax.get_yaxis_transform(), textcoords="offset points",
size=14, va="center")
merged.plot(y='Footloose', marker='o', figsize=(9,6), legend=None, color= 'steelblue', ax=ax, alpha=1,
transform=mtrans.offset_copy(ax.transData, fig=fig, x=0.0, y=4, units='points'))
y = 12
ax.annotate('Footloose', xy=(0.984,y), xytext=(6,0), color='steelblue',
xycoords = ax.get_yaxis_transform(), textcoords="offset points",
size=14, va="center")
merged.plot(y=['Top Pub'], marker='o', figsize=(9,6), legend=None, color= 'darkred', ax=ax, alpha=1,
transform=mtrans.offset_copy(ax.transData, fig=fig, x=0.0, y=3*(1), units='points'))
y = 22
ax.annotate('Top Pub', xy=(0.984,y), xytext=(6,0), color='darkred',
xycoords = ax.get_yaxis_transform(), textcoords="offset points",
size=14, va="center")
y = 2
merged.plot(y='Gaters', marker='o', figsize=(9,6), legend=None, color= 'springgreen', ax=ax, alpha=1,
transform=mtrans.offset_copy(ax.transData, fig=fig, x=0.0, y=-1.8, units='points'))
ax.annotate('Gaters', xy=(0.984,y), xytext=(6,0), color='springgreen',
xycoords = ax.get_yaxis_transform(), textcoords="offset points",
size=14, va="center")
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
fig.text(0.25, 0.95, "Clubs of", ha="center", va="bottom", size=17,color="black")
fig.text(0.34, 0.95, "S", ha="center", va="bottom", size=21,color="blue")
fig.text(0.37, 0.95, "V", ha="center", va="bottom", size=21,color="red")
fig.text(0.40, 0.95, "P", ha="center", va="bottom", size=21,color="gold")
fig.text(0.43, 0.95, "L", ha="center", va="bottom", size=21,color="darkgreen")
fig.text(0.59, 0.95,"in years 2014-2019", ha="center", va="bottom", size=17,color="black")
x_axis = ax.axes.get_xaxis()
x_label = x_axis.get_label()
x_label.set_visible(False)
ax.set_ylabel('Points won', color='darkblue', fontdict={'fontsize': 14, 'fontweight': 'medium'})
years=[2014,2015, 2016,2017,2018, 2019]
plt.xticks(range(len(years)),(years))
plt.margins(x=0.012,y=0.2)
ax.get_xaxis().tick_bottom()
#ax.get_yaxis().tick_left()
ax.tick_params(axis='y', which='major', labelsize=12,bottom=False, top=False, labelbottom=True,
left=False, right=False, labelright=False)
ax.tick_params(axis='x', which='major', labelsize=14,bottom=False, top=False, labelbottom=True,
left=False, right=False, labelright=False)
#fig.subplots_adjust(left=.06, right=.75, bottom=0, top=.94)
ax.yaxis.set_major_formatter(plt.FuncFormatter('{:.0f}'.format))
ax.grid(True, 'major', 'y', ls='--', lw=.6, c='darkgray', alpha=.5)
plt.show()
plt.savefig('SVPLclubs1.pdf')
谢谢!
解决方案
fig.subplots_adjust(left=.075, right=.65, bottom=0, top=.94) 帮助了我!
推荐阅读
- python - 使用 Pandas 就地排序值时出现错误消息
- php - Wordpress 高级自定义字段变量未呈现
- python - python中的Matplotlib图形问题
- r - Lapply changing class of input
- google-bigquery - BigQuery:从 Coldline Cloud Storage 类查询 parquet 文件作为外部数据源的定价
- r - 有没有办法根据第三列中的字符用第二列中的字符替换数据框的一列中的字符?
- python - 在 Python3 中打印字符串的镜像
- python - 如何控制 x 轴刻度的数量?
- c - 是否有将双指针转换为单指针的编译器警告?
- c++ - C ++“相同字母”代码无法正常工作