Matplotlib使刻度标签的字体变小 [英] Matplotlib make tick labels font size smaller

查看:120
本文介绍了Matplotlib使刻度标签的字体变小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在matplotlib图中,如何使用ax1.set_xticklabels()缩小刻度标签的字体大小?

In a matplotlib figure, how can I make the font size for the tick labels using ax1.set_xticklabels() smaller?

此外,如何将其从水平方向旋转到垂直方向?

Further, how can one rotate it from horizontal to vertical?

推荐答案

请注意,较新版本的MPL具有此任务的快捷方式.此问题的其他答案中显示了一个示例: https://stackoverflow.com/a/11386056/42346

Please note that newer versions of MPL have a shortcut for this task. An example is shown in the other answer to this question: https://stackoverflow.com/a/11386056/42346

下面的代码仅用于说明目的,不一定进行优化.

The code below is for illustrative purposes and may not necessarily be optimized.

import matplotlib.pyplot as plt
import numpy as np

def xticklabels_example():
    fig = plt.figure() 

    x = np.arange(20)
    y1 = np.cos(x)
    y2 = (x**2)
    y3 = (x**3)
    yn = (y1,y2,y3)
    COLORS = ('b','g','k')

    for i,y in enumerate(yn):
        ax = fig.add_subplot(len(yn),1,i+1)

        ax.plot(x, y, ls='solid', color=COLORS[i]) 

        if i != len(yn) - 1:
            # all but last 
            ax.set_xticklabels( () )
        else:
            for tick in ax.xaxis.get_major_ticks():
                tick.label.set_fontsize(14) 
                # specify integer or one of preset strings, e.g.
                #tick.label.set_fontsize('x-small') 
                tick.label.set_rotation('vertical')

    fig.suptitle('Matplotlib xticklabels Example')
    plt.show()

if __name__ == '__main__':
    xticklabels_example()

这篇关于Matplotlib使刻度标签的字体变小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆