0

I have this chart which I'm quite happy with except that I cannot fit 4 labels at the end of the lines and it does not look good when I am saving it as a pdf.

mychart

I have already tried changing figsize to figsize=(9,6), and xy=(0.984,y) in ax.annotate and this is as good as I can get.

what else can I tweak to make the labels on the right more visible?

My code.

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')

Thank you!

Community
  • 1
  • 1
Bluetail
  • 1,093
  • 2
  • 13
  • 27
  • 1
    Use `x0, x1 = plt.xlim()` and `plt.xlim(x0, x1 + (x1-x0)*0.1)` to create more whitespace at the right. Note that you need to call `plt.savefig` before `plt.show` because the latter clears the plot. – JohanC May 26 '20 at 22:22
  • That's a lot of code. You **really** need to simplify your question. – Quang Hoang May 26 '20 at 22:22
  • thanks, JohanC. I do not know why, I opened it today, re-run it, and all the labels fit. weird, cos I have not changed anything! – Bluetail May 27 '20 at 14:40

1 Answers1

0

fig.subplots_adjust(left=.075, right=.65, bottom=0, top=.94) helped me!

Bluetail
  • 1,093
  • 2
  • 13
  • 27