python - Saving plot from ipython notebook produces a cut image -
i plotting plot 2 ylabels using ipython notebook , image looks when visualized inside notebook.
here how it:
import matplotlib.pyplot plt fig, ax1 = plt.subplots() plt.title('title') plt.xlabel('x') plt.plot(x, y1, '-', color='blue', label='snr') ax1.set_ylabel('y1', color='blue') tl in ax1.get_yticklabels(): tl.set_color('blue') ax2 = ax1.twinx() plt.plot(x, y2, '--', color='red', label='ngal') ax2.set_ylabel('y2', color='red') tl in ax2.get_yticklabels(): tl.set_color('red')
the problem when try save command
plt.savefig('output.png', dpi=300)
since output image cut on right side: don't see right ylabel if right numbers large.
by default, matplotlib leaves little room x , y axis labels , tick labels, therefore need adjust figure include more padding. fortunately not easier do. before call savefig, can call call
fig.tight_layout() plt.savefig('output.png', dpi=300)
alternatively, can pass bbox_inches='tight' savefig adjust figure include of x , y labels
plt.savefig('output.png', dpi=300, bbox_inches='tight')
Comments
Post a Comment