Skip to main content
 首页 » 编程设计

Python/Matplotlib之x 刻度标签格式化为多列

2025年05月04日10飞鱼

是否可以采取:

hello world 12345 

作为一个 x-tick 标签(已经垂直显示)并将其变成这样:

hello world 
12345 

哪个是带有(旋转的)两列的 x-tick 标签?

请您参考如下方法:

您可以使用 textwrap 格式化文本来自标准库的模块:

import matplotlib.pyplot as plt 
import numpy as np 
import textwrap 
 
mu, sigma=100, 15 
N=4 
x=mu + sigma*np.random.randn(N) 
plt.bar(range(N), x,  align='center') 
labels=[ 
    'hello world 12345', 
    'another long one', 
    'what happened to pithy', 
    'yada yada', 
    ] 
labels=[textwrap.fill(text,15) for text in labels] 
 
plt.xticks(range(N), labels) 
plt.show()