Home > Article > Backend Development > How to Customize Tick Label Appearance in Matplotlib: Font Size and Rotation?
In matplotlib, customizing the appearance of tick labels enhances the readability and aesthetics of visualizations. Here's a detailed guide to adjusting the font size and rotation of tick labels:
To modify the font size of tick labels, use the labelsize argument within the tick_params() method. Here's an example:
<code class="python">import matplotlib.pyplot as plt # Create a figure and axis fig, ax = plt.subplots() # Change the font size of major (x-axis) and minor (y-axis) tick labels ax.tick_params(axis='x', which='major', labelsize=10) ax.tick_params(axis='y', which='minor', labelsize=8) # Show the plot plt.show()</code>
To rotate tick labels from horizontal to vertical, use the rotation argument within the tick_params() method. A rotation value of 90 degrees will orient the labels vertically.
<code class="python">import matplotlib.pyplot as plt # Create a figure and axis fig, ax = plt.subplots() # Rotate the x-axis tick labels to 90 degrees ax.tick_params(axis='x', rotation=90) # Show the plot plt.show()</code>
Note that not all tick labels may fit vertically, especially with limited space. In such cases, consider adjusting the spacing or axis limits to accommodate the rotated labels.
The above is the detailed content of How to Customize Tick Label Appearance in Matplotlib: Font Size and Rotation?. For more information, please follow other related articles on the PHP Chinese website!