The Wayback Machine - https://web.archive.org/web/20230115013623/https://github.com/matplotlib/matplotlib/issues/12318
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set_xticks() needs argument for 'fontsize' #12318

Closed
ulfaslak opened this issue Sep 28, 2018 · 3 comments
Closed

set_xticks() needs argument for 'fontsize' #12318

ulfaslak opened this issue Sep 28, 2018 · 3 comments

Comments

@ulfaslak
Copy link

ulfaslak commented Sep 28, 2018

Bug report

Bug summary

Unlike plt.xticks or plt.yticks, ax.set_xticks and ax.set_yticks has no argument fontsize.
To change the fontsize of ticklabels in a subplot, the shortest workaround (to my knowledge) is:

for tick in ax.xaxis.get_majorticklabels():  # example for xaxis
    tick.set_fontsize(12) 

This feels too convoluted. It should not be more complicated than ax.set_xticks(fontsize=12)

Code for reproduction

fig, ax = plt.subplots(1, 2, figsize=(10, 4))

ax[0].plot(range(10), [x**2 for x in range(10)])  # First subplot
ax[0].set_xticks(fontsize=10)                     # where I want fontsize 10 on xticks 

ax[1].plot(range(10), [x**3 for x in range(10)])  # Second subplot
ax[1].set_xticks(fontsize=12)                     # where I want fontsize 12 on xticks

plt.show()

Actual outcome

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-561-19741a4fe5dc> in <module>()
      2 
      3 ax[0].plot(range(10), [x**2 for x in range(10)])  # First subplot
----> 4 ax[0].set_xticks(fontsize=10)                     # where I want fontsize 10 on xticks
      5 
      6 ax[1].plot(range(10), [x**3 for x in range(10)])  # Second subplot

TypeError: set_xticks() got an unexpected keyword argument 'fontsize'

Expected outcome

screen shot 2018-09-28 at 10 31 45

Matplotlib version

  • Operating system: Mac OS 10.13.6
  • Matplotlib version: 2.1.2
  • Matplotlib backend (print(matplotlib.get_backend())): module://ipykernel.pylab.backend_inline
  • Python version: 2.7
  • Jupyter version (if applicable): 4.4.0
  • Other libraries:
@ImportanceOfBeingErnest
Copy link
Member

First, set_xticks does not have fontsize argument because set_xticks sets the location of the ticks, not the labels to be shown. Those can be set via set_xticklabels, and indeed set_xticklabels does have a fontsize argument. However, it requires you to specify the ticklabels, which only makes sense when using a FixedLocator.

ax.set_xticks([1,4,5]) 
ax.set_xticklabels([1,4,5], fontsize=12)  

Usually you would neither set the tick positions nor the labels to any fixed numbers but use the default locator and formatter or use your own locators or formatters. In those cases the easiest way to set the fontsize is to use .tick_params

ax.tick_params(axis="x", labelsize=12)

It does not allow to set other font or text related parameters like font-weight or alignment though. This is indeed a missing bit in the API. A slightly less convenient way is to use pyplot here

plt.setp(ax.get_xticklabels(), fontsize=12, fontweight="bold", horizontalalignment="left")

which then allows to set all properties at once. The drawback is that it is kind of convoluted and requires pyplot.

In conclusion I think there is some need to change the way text properties can be set on axis labels, but set_xticks is surely not the right place to do that.

@timhoffm
Copy link
Member

As written above, set_xticks sets only the positions and thus does not handle labelsize. Use set_xticklabels or tick_params.

@tangyuan59
Copy link

赶紧加上fontsize 寻思啥呢 大兄弟

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants