Matplotlib: Change Position of Plot

Matplotlib: Change Position of Plot

I did the following plot with the figure size (which should not be changed):

plt.figure(figsize=(14, 5))

Unfortunately, the x-label is not fully visible. My question: Is it possible, to move the whole grafic to the top (because there would be enough space)?

The code:

plt.figure(figsize=(14, 5))
plt.plot(time_load,load, linewidth=1.5)
plt.grid(True)
plt.tick_params(labelsize=16)
plt.xlabel('Time [hours]',fontsize=16)
plt.xlim([0,24])
plt.xticks([0,4,8,12,16,20,24])
plt.legend(loc="upper left",fontsize = 'large')

Thank you very much for your help!

2

3 Answers

You can also try

plt.subplots_adjust(bottom=0.19)

If 0.19 adds too much or too little space to see the x-label better, then try adjusting little by little up or down.

0

A very simple approach is to use

plt.tight_layout()

See:

Thank you very much for your help!

You can use the axes() command that allows you to specify the location as axes. You can also use the xlim() command and ylim() command. In your case, you can use the latter. So just as an example, the xlim method can be used like this:

plt.xlim(X.min()*1.1, X.max()*1.1)
plt.ylim(C.min()*1.1, C.max()*1.1)

This will make some space for the data points to be seen clearly. Hope that helps. Just seen the code so you're using xlim method. You want to create the subplot somewhere else, for that you can use the axis() method.

plt.axes([0.3, 0.3, .5, .5])

You can adjust plot accordingly. This one will create a subplot in the upper right corner of the figure.

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross
Author

Alexander Ross

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.