为了账号安全,请及时绑定邮箱和手机立即绑定

Matplotlib使刻度标签的字体变小

Matplotlib使刻度标签的字体变小

凤凰求蛊 2019-10-14 15:22:22
在matplotlib图中,如何使用ax1.set_xticklabels()较小的刻度标签来设置字体大小?此外,如何将其从水平旋转到垂直?
查看完整描述

3 回答

?
临摹微笑

TA贡献1982条经验 获得超2个赞


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


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()


查看完整回答
反对 回复 2019-10-14
?
MMMHUHU

TA贡献1834条经验 获得超8个赞

实际上有一个更简单的方法。我刚刚发现:


import matplotlib.pyplot as plt

# We prepare the plot  

fig, ax = plt.subplots()


# We change the fontsize of minor ticks label 

ax.tick_params(axis='both', which='major', labelsize=10)

ax.tick_params(axis='both', which='minor', labelsize=8)

但是,这只能回答label部分问题的大小。


查看完整回答
反对 回复 2019-10-14
?
牧羊人nacy

TA贡献1862条经验 获得超7个赞

或者,您可以执行以下操作:


import matplotlib as mpl

label_size = 8

mpl.rcParams['xtick.labelsize'] = label_size 


查看完整回答
反对 回复 2019-10-14
  • 3 回答
  • 0 关注
  • 2853 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信