Home > Article > Backend Development > How to Adjust Tick Label Font Size and Rotation in Matplotlib?
Customizing Tick Label Appearance in Matplotlib: Font Size and Rotation
In visualization tasks using Matplotlib, customizing the appearance of tick labels can greatly enhance readability and clarity. This question explores two aspects of tick label customization: font size and rotation.
Regarding font size, the response suggests a straightforward approach using the tick_params method. By specifying the axis and tick type ('major' or 'minor'), you can easily adjust the font size for both major and minor ticks. The example code provided demonstrates this process effectively.
Unfortunately, the answer does not address the request for rotating tick labels. For that purpose, you can utilize the rotation parameter within the set_xticklabels or set_yticklabels methods. Here's how you would do it:
<code class="python">import matplotlib.pyplot as plt # Prepare the plot fig, ax = plt.subplots() # Set the tick label size ax.tick_params(axis='both', which='major', labelsize=10) # Rotate the tick labels by 45 degrees ax.set_xticklabels(ax.get_xticks(), rotation=45) ax.set_yticklabels(ax.get_yticks(), rotation=45)</code>
This addition to the code ensures that the tick labels are not only reduced in size but also rotated, which can be particularly beneficial for vertical or tightly packed data visualization.
The above is the detailed content of How to Adjust Tick Label Font Size and Rotation in Matplotlib?. For more information, please follow other related articles on the PHP Chinese website!