问题: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?


回答 0

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

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

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

在此处输入图片说明

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

enter image description here


回答 1

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

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部分问题的大小。

There is a simpler way actually. I just found:

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)

This only answers to the size of label part of your question though.


回答 2

要同时指定字体大小和旋转度,请尝试以下操作:

plt.xticks(fontsize=14, rotation=90)

To specify both font size and rotation at the same time, try this:

plt.xticks(fontsize=14, rotation=90)

回答 3

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

import matplotlib as mpl
label_size = 8
mpl.rcParams['xtick.labelsize'] = label_size 

Alternatively, you can just do:

import matplotlib as mpl
label_size = 8
mpl.rcParams['xtick.labelsize'] = label_size 

回答 4

plt.tick_params(axis='both', which='minor', labelsize=12)
plt.tick_params(axis='both', which='minor', labelsize=12)

回答 5

另一种选择

我有两个并排的图,想分别调整刻度线标签。

上面的解决方案很接近,但是对我来说却没有用。我从此matplotlib 页面找到了解决方案。

ax.xaxis.set_tick_params(labelsize=20)

这可以解决问题,并且很直接。在我的用例中,需要调整的是右图。自从创建新的刻度标签以来,对于左侧的图,我可以在设置标签的相同过程中调整字体。

ax1.set_xticklabels(ax1_x, fontsize=15)
ax1.set_yticklabels(ax1_y, fontsize=15)

因此我使用了正确的情节,

ax2.xaxis.set_tick_params(labelsize=24)
ax2.yaxis.set_tick_params(labelsize=24)

轻微的微妙…我知道…但是我希望这可以帮助某人:)

如果有人知道如何调整数量级标签的字体大小,则加分。

在此处输入图片说明

Another alternative

I have two plots side by side and would like to adjust tick labels separately.

The above solutions were close however they were not working out for me. I found my solution from this matplotlib page.

ax.xaxis.set_tick_params(labelsize=20)

This did the trick and was straight to the point. For my use case, it was the plot on the right that needed to be adjusted. For the plot on the left since I was creating new tick labels I was able to adjust the font in the same process as seting the labels.

ie

ax1.set_xticklabels(ax1_x, fontsize=15)
ax1.set_yticklabels(ax1_y, fontsize=15)

thus I used for the right plot,

ax2.xaxis.set_tick_params(labelsize=24)
ax2.yaxis.set_tick_params(labelsize=24)

A minor subtlety… I know… but I hope this helps someone :)

Bonus points if anyone knows how to adjust the font size of the order of magnitude label.

enter image description here


回答 6

在当前版本的Matplotlib中,您可以执行axis.set_xticklabels(labels, fontsize='small')

In current versions of Matplotlib, you can do axis.set_xticklabels(labels, fontsize='small').


回答 7

您还可以使用如下一行来更改标签显示参数(如fontsize):

zed = [tick.label.set_fontsize(14) for tick in ax.yaxis.get_major_ticks()]

You can also change label display parameters like fontsize with a line like this:

zed = [tick.label.set_fontsize(14) for tick in ax.yaxis.get_major_ticks()]

回答 8

对于较小的字体,我使用

ax1.set_xticklabels(xticklabels, fontsize=7)

而且有效!

For smaller font, I use

ax1.set_xticklabels(xticklabels, fontsize=7)

and it works!


回答 9

以下为我工作:

ax2.xaxis.set_tick_params(labelsize=7)
ax2.yaxis.set_tick_params(labelsize=7)

上面的优点是您无需提供的array,也不需要labels使用上的任何数据axes

The following worked for me:

ax2.xaxis.set_tick_params(labelsize=7)
ax2.yaxis.set_tick_params(labelsize=7)

The advantage of the above is you do not need to provide the array of labels and works with any data on the axes.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。