0

The texts on the right on this pyplot graph are clipped, how can I expand the plot area without changing the x-axis?

Minimal example code (similar to but not identical to example image)

import numpy as np

import matplotlib.pyplot as plt
import matplotlib as mp

n40=146-1.07*40
n90=146-1.07*90 
ageAxis =np.array([10, 40, 90])
Normal=np.array([n40, n40, n90])
plt.plot(ageAxis,Normal)

plt.text(90.2,50,'long text here that will be clipped')
ax = plt.gca()
ax.set_ylim([0,165])
ax.set_xlim([0,90])
fig= plt.gcf()
# set size fig.set_size_inches(20, 10.5)
plt.show()

pyplot

4
  • 1
    fig = matplotlib.pyplot.gcf() fig.set_size_inches(18.5, 10.5) try this and see whether it works or not. Commented Feb 6, 2019 at 8:21
  • @Ishtiaque05 well, that will set the physical size of the plot, but the text should be visible no matter the scaling of the plot. Commented Feb 6, 2019 at 8:25
  • 1
    If you just want to save the figure with all the text visible, you could go for fig.save_fig(bbox_inches='tight'). For interactive plots you can use fig.tight_layout(), but note that once you resize the figure, tight_layout needs to be called again. See here for a solution. Commented Feb 6, 2019 at 8:32
  • Sorry there was a typo, it's fig.savefig(bbox_inches='tight'), i.e. one less underscore. Commented Feb 6, 2019 at 8:45

1 Answer 1

1

It seems that it can be done with a combination of set_size_inches and subplots_adjust

Not elegant, I think, but it works:

import numpy as np

import matplotlib.pyplot as plt
import matplotlib as mp

n40=146-1.07*40
n90=146-1.07*90 
ageAxis =np.array([10, 40, 90])
Normal=np.array([n40, n40, n90])
plt.plot(ageAxis,Normal)

plt.text(90.2,50,'long text here that will be clipped')
ax = plt.gca()
ax.set_ylim([0,165])
ax.set_xlim([0,90])
fig= plt.gcf()
fig.set_size_inches(10, 5.5)     # set a suitable size
plt.subplots_adjust(right=0.75)  # adjust plot area
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.