gpt4 book ai didi

python - Pandas 散点图中的刻度标签未正确绘制

转载 作者:行者123 更新时间:2023-12-01 19:57:40 25 4
gpt4 key购买 nike

我正在用 Pandas 绘制散点图矩阵,但第一个图的刻度标签有时绘制正确,有时绘制不正确。我无法弄清楚出了什么问题!

这是一个例子:

enter image description here

代码:

from pandas.tools.plotting import scatter_matrix
import pylab
import numpy as np
import pandas as pd

def create_scatterplot_matix(X, name):
"""
Outputs a scatterplot matrix for a design matrix.

Parameters:
-----------
X:a design matrix where each column is a feature and each row is an observation.
name: the name of the plot.
"""
pylab.figure()
df = pd.DataFrame(X)
axs = scatter_matrix(df, alpha=0.2, diagonal='kde')

for ax in axs[:,0]: # the left boundary
ax.grid('off', axis='both')
ax.set_yticks([0, .5])

for ax in axs[-1,:]: # the lower boundary
ax.grid('off', axis='both')
ax.set_xticks([0, .5])

pylab.savefig(name + ".png")

各位,有人吗?!!

编辑(X 的示例):

X = np.random.randn(1000000, 10)

最佳答案

这是预期的行为。 y 轴值显示第 0 列的 y 轴值。第 0 行、第 0 列包含概率密度图。第 0 行、第 1-3 列包含用于在对角线上创建图形的数据。

example Pandas 绘图文档中的内容看起来很相似。

演示:

from pandas.tools.plotting import scatter_matrix
import pylab
import numpy as np
import pandas as pd

def create_scatterplot_matix(X, name):
pylab.figure()

df = pd.DataFrame(X)
axs = scatter_matrix(df, alpha=0.2, diagonal='kde')

pylab.savefig(name + ".png")

create_scatterplot_matix([[0,0,0,0]
,[1,1,1,1]
,[1,1,1,1]
,[2,2,2,2]],'test')

在此示例代码中,我使用了一个极其简单的数据集来进行演示。我还删除了设置 y 和 x 刻度的代码部分。

这是结果图:

enter image description here

每条对角线上都有一个概率密度图。每条非对角线中都是用于创建对角线中的图形的数据。第 0 行的 y 轴显示位于第 0,0 位置的概率密度图的 y 轴。第一、第二和第三行的 y 轴显示用于在对角线上创建概率密度图的 0,1 0,2 和 0,3 位置中的数据的 y 轴。

您可以在我们的示例中看到以下绘制点:[0,0] [1,1] [2,2]。 [1,1] 处的点较暗,因为该位置处的点比其他位置处的点更多。

发生的情况是,您的数据集的所有值都在 0 和 1 之间,这就是为什么 0.5 在两个轴上完美地显示在行/列的中心。然而,数据严重偏向值 0,这就是为什么概率密度图越接近 0 就越猛增。第 0 行中概率密度图的最大值看起来约为(眼球测试)约 8 -10。

我个人会做的是将您的左边界代码编辑为如下所示:

autoscale = True # We want the 0,0th item's y-axis to autoscale
for ax in axs[:,0]: # the left boundary
ax.grid('off', axis='both')
if autoscale == True:
ax.set_autoscale_on(True)
autoscale = False
else:
ax.set_yticks([0, 0.5])

对于我们的示例数据集,使用此技术会生成如下图表:

enter image description here

关于python - Pandas 散点图中的刻度标签未正确绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26101831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com