matplotlib - Cut saved plot

Asked

Viewed 449 times

1

When saving my chart it gets a left cutout on top of the names because it is wide. How do I fix it?

inserir a descrição da imagem aqui

cmap = cc.cm['kbc']
norm = matplotlib.colors.Normalize(vmin = ranking_members.rating.min(), vmax = ranking_members.rating.max())

plt.barh(ranking_members.name, ranking_members.members, color = cc.cm['kbc'](norm(ranking_members.rating.values)))

sm = plt.cm.ScalarMappable(cmap = cmap, norm = norm)
sm.set_array([])
cbar = plt.colorbar(sm)
cbar.ax.get_yaxis().labelpad = 15
cbar.set_label('Nota', rotation = 270)

plt.title('Ranking de Popularidade')
plt.xlabel('Número de Membros')
plt.ylabel('Anime')

plt.savefig('C:\\Users\\msamu\\Desenvolvimento\\Data Analysis\\Python\\My Anime List\\Plots\\ranking_members.png', dpi = 300)
  • I’m not sure if it works because there’s no testing, but when adding bbox_inches='tight' as the third parameter (after dpi=300) of the function savefig(), should work.

  • It worked, thank you very much!

1 answer

3

When saving your chart just add one more parameter, called bbox_inches, its function savefig(). Altering:

plt.savefig('C:\\caminho\\...\\ranking_members.png', dpi = 300)

For:

plt.savefig('C:\\caminho\\...\\ranking_members.png', dpi = 300, bbox_inches='tight')

The estate bbox_inches='tight' removes all the extra blank space around the figure, it does not rearrange its figure, but for a quick fix it works. If you want to change more settings to get exactly what you want, you can use the Tight Layout.

Related question on Soen

Browser other questions tagged

You are not signed in. Login or sign up in order to post.