出于某种原因,当我try 使用matplotlib.ticker.FuncFormatter重新格式化海运条带图的x轴时,它会重新格式化位置而不是标签,MWE如下.在普通的matplotlib散点图上使用相同的格式化程序函数可以很好地工作,并给出预期的结果.
还有一些关于标签如何工作的其他事情我不明白,因为从每个轴打印一个显示默认和重新格式化的条形图相同的事情,这与图不匹配,然后为散点图标签提供一个空字符串.
original stripplot label: Text(3, 0, '0.3')
reformatted stripplot label: Text(3, 0, '0.3')
reformatted scatterplot label: Text(0, 0, '')
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame({'x': np.random.randint(0,10,size=100) / 10, 'y': np.random.normal(size=100)})
fig, axarr = plt.subplots(3,1)
sns.stripplot(data=df, x='x', y='y', ax=axarr[0])
axarr[0].set_title('stripplot, default formatting')
print(f' original stripplot label: {axarr[0].get_xticklabels()[3]}')
sns.stripplot(data=df, x='x', y='y', ax=axarr[1])
axarr[1].xaxis.set_major_formatter(plt.FuncFormatter(lambda x, pos: f'{x:.2f}'))
axarr[1].set_title('stripplot, incorrect formatting')
print(f' reformatted stripplot label: {axarr[1].get_xticklabels()[3]}')
axarr[2].scatter(df.x, df.y, s=10, alpha=0.5)
axarr[2].xaxis.set_major_formatter(plt.FuncFormatter(lambda x, pos: f'{x:.2f}'))
axarr[2].set_title('scatterplot, correct formatting')
print(f' reformatted scatterplot label: {axarr[2].get_xticklabels()[3]}')
fig.tight_layout()