tomasstolker / species

Toolkit for atmospheric characterization of directly imaged exoplanets
https://species.readthedocs.io
MIT License
22 stars 10 forks source link

Change fontsize of axis labels with plot_spectrum? #101

Open aidenc02 opened 1 month ago

aidenc02 commented 1 month ago

Hi, I was wondering if there is any way to edit the size of axis labels with plot_spectrum? I can't seem to find a way to do it on the docs. Thank you!

wbalmer commented 1 month ago

(out of the blue answer from Definitely Not Tomas, but I was looking through the repo just now, and I recently had the same problem as you so I have an answer and might as well respond)

The fontsizes are currently hard coded, so you can either edit the package .py files directly to change them (ctrl+F "fontsize=" and replace), or you can try to postprocess your figures by using plt.gca() and cycling through the axes. Example below for the plot_spectrum output.

spec_plot = plot_spectrum(..., output='myspectrum.png')

axs = specplot.axes

# iterate through all axs, change labels and ticks
for ax in axs:
    for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +
                 ax.get_xticklabels() + ax.get_yticklabels()):
        item.set_fontsize(12) # 12, or whichever fontsize you now prefer

# you can also change the labels themselves, e.g. here I change capital F to f
axs[0].set_ylabel('$f_\\lambda$ (10$^{-16}$ W m$^{-2}$ µm$^{-1}$)')
axs[1].set_ylabel('$\\Delta$$f_\\lambda$ ($\\sigma$)')

# then save the result using savefig
plt.savefig('myspectrum.png', bbox_inches='tight', dpi=300)

Note that if in plot_spectrum you set output=None, this might not work because when output=None, the function calls plt.show() on line 1484 which wipes the current figure and axes lists. So I end up saving the file twice, the first being the uncorrected one and then overwriting that file with the plt.savefig call.

tomasstolker commented 1 month ago

Thanks for opening this issue @aidenc02 and for providing the solution @wbalmer 👍 !

Indeed, as I wanted to limit the number of parameters a bit, the way to do this is through the Matplotlib Figure object that is returned by all the plot functions of species.

Feel free to leave this issue open since it is not really explained in the documentation how to adjust the plot properties, so I think your question will be useful for others as well!

aidenc02 commented 1 month ago

@tomasstolker @wbalmer Okay great, thanks so much both of you!

tomasstolker commented 3 weeks ago

To make this more straightforward, I have added the font_size parameter to plot_spectrum. This is a dictionary in which the various font sizes can be set, e.g. font_size={'xlabel': 10., 'xlabel': 8., 'title': 12., 'legend': 9.}. The legend font size is best set with the legend parameter though. Tick font sizes can not be set but could be added later. The update can be pulled from Github.