出于某种原因,当我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()

example output image

推荐答案

您希望使用native_scale=True来保留数据在x轴上的编号:

sns.stripplot(data=df, x='x', y='y', native_scale=True, ax=axarr[1])

enter image description here

此功能为new in 0.12.0,适用于条带/数组图,new in 0.13.0适用于其他分类功能.

Python相关问答推荐

一切似乎都可以自己工作,但当我把它放在一起时,它会抛出RegexMatch错误

错误:找不到TensorFlow / Cygwin的匹配分布

带有Postgres的Flask-Data在调用少量API后崩溃

为什么我的(工作)代码(生成交互式情节)在将其放入函数中时不再工作?

sys.modulesgo 哪儿了?

这家einsum运营在做什么?E = NP.einsum(aj,kl-il,A,B)

code _tkinter. Tcl错误:窗口路径名称错误.!按钮4"

在使用Guouti包的Python中运行MPP模型时内存不足

Odoo -无法比较使用@api.depends设置计算字段的日期

Python json.转储包含一些UTF-8字符的二元组,要么失败,要么转换它们.我希望编码字符按原样保留

删除所有列值,但判断是否存在任何二元组

大小为M的第N位_计数(或人口计数)的公式

所有列的滚动标准差,忽略NaN

python中字符串的条件替换

如何在Python中找到线性依赖mod 2

如何并行化/加速并行numba代码?

SQLAlchemy bindparam在mssql上失败(但在mysql上工作)

需要帮助重新调整python fill_between与数据点

在两极中过滤

在Admin中显示从ManyToMany通过模型的筛选结果