我想剪切长标签,以便仅显示前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');
理想的输出:
答案 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()]
答案 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');
答案 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]