如何在Matplotlib中剪切长标签

时间:2019-05-28 16:14:07

标签: python pandas numpy matplotlib

我想剪切长标签,以便仅显示前4个字母,而不更改原始数据框中的值。

示例:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sn

np.random.seed(89)

# Toy Dataset
d = pd.DataFrame(np.random.randint(0,3, size=(100, 1)), columns=['var'])
d['var'] = (np.where(d['var'] == 1,'Long loooooong loooooong text',d['var']))

# Plot
f, axes = plt.subplots()
sns.countplot(y='var', data=d, orient='h');

当前输出enter image description here

理想的输出:

enter image description here

3 个答案:

答案 0 :(得分:2)

我将提供一个稍微通用的解决方案:遍历您的y-tick标签,然后创建一个新标签列表,如果任何标签超过4个字符,则最多存储4个字符。最后,将此新标签列表分配为y-ticks

# Plot
f, axes = plt.subplots()
ax = sns.countplot(y='var', data=d, orient='h');

new_labels = []

for i in ax.yaxis.get_ticklabels():
    label = i.get_text()
    if len(label) > 4:
        new_labels.append(label[0:4])
    else:    
        new_labels.append(label)

ax.yaxis.set_ticklabels(new_labels)  

或者您也可以使用列表理解作为

在一行中创建新标签。
new_labels = [i.get_text()[0:4] if len(i.get_text()) > 4 else i.get_text() 
              for i in ax.yaxis.get_ticklabels()]

enter image description here

答案 1 :(得分:2)

最好通过已经提供简短格式的数据来解决此问题。

df2 = d.copy()
df2["var"] = df2["var"].apply(lambda x: x[:4])

# Plot
f, axes = plt.subplots()
sns.countplot(y='var', data=df2, orient='h');

enter image description here

答案 2 :(得分:0)

尝试:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

np.random.seed(89)

# Toy Dataset
d = pd.DataFrame(np.random.randint(0,3, size=(100, 1)), columns=['var'])
d['var'] = (np.where(d['var'] == 1,'Long loooooong loooooong text'[:4],d['var']))

# Plot
f, axes = plt.subplots()
sns.countplot(y='var', data=d, orient='h');

获取字符串的前4个字母:

first_four_letters = your_string[:4]