Python drawing title Matplotlib overlays axis labels when using twiny

I am trying to plot two separate quantities on the same graph using twiny as follows:

fig = figure() ax = fig.add_subplot(111) ax.plot(T, r, 'b-', T, R, 'r-', T, r_geo, 'g-') ax.set_yscale('log') ax.annotate('Approx. sea level', xy=(Planet.T_day*1.3,(Planet.R)/1000), xytext=(Planet.T_day*1.3, Planet.R/1000)) ax.annotate('Geostat. orbit', xy=(Planet.T_day*1.3, r_geo[0]), xytext=(Planet.T_day*1.3, r_geo[0])) ax.set_xlabel('Rotational period (hrs)') ax.set_ylabel('Orbital radius (km), logarithmic') ax.set_title('Orbital charts for ' + Planet.N, horizontalalignment='center', verticalalignment='top') ax2 = ax.twiny() ax2.plot(v,r,'k-') ax2.set_xlabel('Linear speed (ms-1)') show() 

and the data is presented perfectly, but I have a problem with the fact that the title of the figure overlaps with the axis labels on the secondary x-axis, so that it is barely distinguishable (I wanted to publish an example of the figure here, but I don’t know, t have a fairly high reputation).

I would like to know if there is an easy way to simply shift the name directly by a few tens of pixels so that the diagram looks more beautiful.

+86
python matplotlib title figure
Oct 05 '12 at 16:26
source share
3 answers

I'm not sure if this is a new feature in later versions of matplotlib, but, at least for 1.3.1, it is simple:

 plt.title(figure_title, y=1.08) 

This also works for plt.suptitle() , but for now (for now) for plt.xlabel() , etc.

+162
Apr 28 '14 at 10:14
source share

Forget to use plt.title and put the text directly with plt.text . The following is an example of excessive exaggeration:

 import pylab as plt fig = plt.figure(figsize=(5,10)) figure_title = "Normal title" ax1 = plt.subplot(1,2,1) plt.title(figure_title, fontsize = 20) plt.plot([1,2,3],[1,4,9]) figure_title = "Raised title" ax2 = plt.subplot(1,2,2) plt.text(0.5, 1.08, figure_title, horizontalalignment='center', fontsize=20, transform = ax2.transAxes) plt.plot([1,2,3],[1,4,9]) plt.show() 

enter image description here

+28
Oct 05
source share
 ax.set_title('My Title\n', fontsize="15", color="red") plt.imshow(myfile, origin="upper") 

If you put '\n' immediately after the title bar, the plot is drawn just below the title. It can be a quick fix.

0
Sep 29 '17 at 6:53 on
source share



All Articles