【问题标题】:Python Seaborn Relplot Scientific notationPython Seaborn Relplot 科学记数法
【发布时间】:2021-09-13 01:23:09
【问题描述】:

我有一个 seaborn relplot。我想显示科学计数法。目前,图像在 x 和 y 刻度上占用很大空间。我想通过将轴转换为科学计数法来最小化它。

我的代码:

sns.relplot(x='Vmpp',y='cVmpp',data=cdf)

我的解决方案和目前的输出:

#I tried a solution reported for the seaborn heatmap. It did produce a plot (I think heat plot?) but did not work. 
sns.relplot(x='Vmpp',y='cVmpp',data=cdf,fmt='.2g')

目前的输出:

AttributeError: 'PathCollection' object has no property 'fmt'

【问题讨论】:

    标签: python matplotlib seaborn scientific-notation


    【解决方案1】:

    sns.relplot() 是一个figure-level 函数。如果您只需要简单的散点图,您可能需要改用sns.scatterplot()

    在任何情况下,您都可以使用通常的 matplotlib 方式微调绘图。特别是,可以使用ax.ticklabel_format(axis='both', style='scientific', scilimits=(0, 0)) 强制任何刻度标签数字的科学记数法。

    我还建议设置alpha 值,因为您有很多重叠点。这是一个完整的例子:

    import matplotlib.pyplot as plt
    import seaborn as sns
    
    sns.set()
    titanic = sns.load_dataset('titanic')
    
    fig, ax = plt.subplots()
    ax.ticklabel_format(axis='both', style='scientific', scilimits=(0, 0))
    sns.scatterplot(x='age', y='fare', data=titanic, alpha=0.5);
    

    编辑: 正如@mwaskom 指出的那样,您也可以使用sns.relplot() 以这种方式更改刻度标签,在这种情况下只需在格式化程序之前调用绘图函数。你不需要指定坐标轴,因为ticklabel_format() 也可以通过matplotlib.pyplot 接口工作:

    # [...] imports and data as above 
    
    sns.relplot(x='age', y='fare', data=titanic, alpha=0.5)
    plt.ticklabel_format(axis='both', style='scientific', scilimits=(0, 0));
    

    【讨论】:

    • 没有理由不能使用relplot 以这种方式更改刻度标签的格式,您只需要在绘图之后而不是之前这样做。
    • @mwaskom 感谢您指出这一点。我相应地编辑了答案。顺便说一句,我喜欢 seaborn 图书馆,所以也谢谢你。
    • @mwaskom 甚至我也想知道。但是,我尝试了两者。令人惊讶的是,它只适用于scatterplot,而不适用于relplot。有什么原因吗?
    猜你喜欢
    • 2019-10-24
    • 2016-02-21
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    • 2016-05-26
    相关资源
    最近更新 更多