猿问

散点矩阵在图形上显示过多的浮点值

我正在尝试使用 Python 绘制散点矩阵,但左上图的 y 轴上的刻度有大量不必要的数字。我使用 pandas.plotting 中的 scatter_matrix 函数直接从 Pandas 绘制图形


另外,我对 Python 很陌生,如果这是一个愚蠢的问题,我很抱歉,但我找不到适合我需要的正确答案。


我尝试使用 yaxis.set_major_formatter 使用不同的轴格式化选项(不确定这是否不起作用,因为我是从熊猫绘图,但无论如何都没有产生任何结果),pandas.set_option 来自定义显示。


from pandas.plotting import scatter_matrix

scatter_matrix(df, alpha=0.3, figsize=(9,9), diagonal='kde')

df:         Tesla Ret  Ford Ret    GM Ret

Date                                     

2012-01-03        NaN       NaN       NaN

2012-01-04  -0.013177  0.015274  0.004751

2012-01-05  -0.021292  0.025664  0.048227

2012-01-06  -0.008481  0.010354  0.033829

2012-01-09   0.013388  0.007686 -0.003490

2012-01-10   0.013578  0.000000  0.017513

2012-01-11   0.022085  0.022881  0.052926

2012-01-12   0.000708  0.005800  0.008173

2012-01-13  -0.193274 -0.008237 -0.015403

2012-01-17   0.167179 -0.001661 -0.003705

...

我尝试使用: plt.gca().yaxis.set_major_formatter(StrMethodFormatter('{x:,.2f}'))并ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f'))在导入相应的模块后,无济于事。

图中的其他所有内容都应该如此,只是左上图的 y 轴。我希望它像图的其余部分一样显示一两个小数点值。


我非常感谢任何可以解决我的问题的帮助。


扬帆大鱼
浏览 238回答 2
2回答

BIG阳

PS:我根据@ImportanceOfBeingEarnest指出的问题编辑了这个答案(感谢他)。请阅读答案下方的评论以了解我的意思。新的解决方案是获取该特定轴的显示刻度并将它们格式化为小数点后两位。new_labels = [round(float(i.get_text()), 2) for i in axes[0,0].get_yticklabels()]axes[0,0].set_yticklabels(new_labels)旧答案(仍保留为历史记录,因为您将看到下图中生成的 y 刻度不正确)问题是您使用axobject 来格式化标签,但ax返回scatter_matrix的不是单轴对象。它是一个包含 9 轴(3x3 子图)的对象。如果绘制axes变量的形状,则可以证明这一点。axes = scatter_matrix(df, alpha=0.3, figsize=(9,9), diagonal='kde')print (axes.shape)# (3, 3)该解决方案是要么迭代通过所有的轴或只改变有问题的情况下,格式化。PS:下图与你的不匹配,因为我只是使用了你发布的小DataFrame。以下是如何为所有 y 轴执行此操作from pandas.plotting import scatter_matrixfrom matplotlib.ticker import FormatStrFormatteraxes = scatter_matrix(df, alpha=0.3, figsize=(9,9), diagonal='kde')for ax in axes.flatten():    ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f')) 或者,您可以只选择一个特定的轴。在这里可以使用访问您的左上角子图axes[0,0]axes[0,0].yaxis.set_major_formatter(FormatStrFormatter('%.2f'))

开心每一天1111

pandas.scatter_matrix遭受不幸的设计选择。也就是说,它在轴的对角线上绘制 kde 或直方图,显示行的其余部分的刻度。然后这需要伪造刻度和标签以适合数据。在此过程中使用了 aFixedLocator和 a FixedFormatter。因此,刻度标签的格式直接从数字的字符串表示中接管。我会在这里提出一个完全不同的设计。也就是说,对角线轴应该保持空白,而是使用双轴来显示直方图或 kde 曲线。因此,问题中的问题不会发生。import numpy as npimport pandas as pdimport matplotlib.pyplot as pltdef scatter_matrix(df, axes=None, **kw):    n = df.columns.size    diagonal = kw.pop("diagonal", "hist")    if not axes:        fig, axes = plt.subplots(n,n, figsize=kw.pop("figsize", None),                                  squeeze=False, sharex="col", sharey="row")    else:        flax = axes.flatten()        fig = flax[0].figure        assert len(flax) == n*n    # no gaps between subplots    fig.subplots_adjust(wspace=0, hspace=0)    hist_kwds = kw.pop("hist_kwds",  {})    density_kwds = kw.pop("density_kwds",  {})    import itertools    p = itertools.permutations(df.columns, r=2)    n = itertools.permutations(np.arange(len(df.columns)), r=2)    for (i,j), (y,x) in zip(n,p):        axes[i,j].scatter(df[x].values, df[y].values, **kw)        axes[i,j].tick_params(left=False, labelleft=False,                               bottom=False, labelbottom=False)    diagaxes = []    for i, c in enumerate(df.columns):        ax = axes[i,i].twinx()        diagaxes.append(ax)        if diagonal == 'hist':            ax.hist(df[c].values, **hist_kwds)        elif diagonal in ('kde', 'density'):            from scipy.stats import gaussian_kde            y = df[c].values            gkde = gaussian_kde(y)            ind = np.linspace(y.min(), y.max(), 1000)            ax.plot(ind, gkde.evaluate(ind), **density_kwds)        if i!= 0:            diagaxes[0].get_shared_y_axes().join(diagaxes[0], ax)        ax.axis("off")    for i,c in enumerate(df.columns):        axes[i,i].tick_params(left=False, labelleft=False,                               bottom=False, labelbottom=False)        axes[i,0].set_ylabel(c)        axes[-1,i].set_xlabel(c)        axes[i,0].tick_params(left=True, labelleft=True)        axes[-1,i].tick_params(bottom=True, labelbottom=True)    return axes, diagaxesdf = pd.DataFrame(np.random.randn(1000, 4), columns=['A','B','C','D'])axes,diagaxes = scatter_matrix(df, diagonal='kde', alpha=0.5)plt.show()
随时随地看视频慕课网APP

相关分类

Python
我要回答