散点图矩阵,输入与输出

时间:2019-10-28 19:50:15

标签: python pandas matplotlib plotly seaborn

我正在尝试创建一个散点图矩阵,其中变量的x轴和y轴不相同(变量的数量也不相同)……例如,我希望沿着x绘制三个输入轴和沿y轴绘制的2个输出,因此有6个散点图的散点图矩阵,显示了一个输入对一个输出

我还没有找到在matplotlib,seaborn,pandas或plotly中执行此操作的方法。有没有人曾经做过这样的事情,或者知道创建这种情节的聪明方法?

到目前为止,我发现的所有内容都针对n ^ 2的地块针对自己绘制了相同的n数量的变量

代码:

import pandas as pd
import seaborn as sns
import plotly.express as px

headings = ['a','b','c','d','e']

data = [[21,22,23,24,25],[10,12,13,14,15],[14,2,3,17,5],[6,17,22,9,14],[16,17,18,19,20]]

df = pd.DataFrame(data=data, columns=headings)
pd.plotting.scatter_matrix(df)

sns.pairplot(df)

fig = px.scatter_matrix(df)
fig.show()

输出:

enter image description here

2 个答案:

答案 0 :(得分:1)

一个seaborn 解决方案:

import pandas as pd 
import matplotlib.pyplot as plt
import seaborn as sns

#generate test data
import numpy as np
np.random.seed(123)
n=10
df = pd.DataFrame({"A": np.random.random(n), 
                   "B": 10 * np.random.random(n),
                   "C": 20 * np.random.random(n),
                   "D": -np.random.random(n),
                   "E": np.random.random(n)-20})
 

#prepare df for plotting
df_temp = df.melt(id_vars=["A", "B"], value_vars=["C", "D", "E"], var_name="row_name", value_name="row_vals")
df_plot = df_temp.melt(id_vars=["row_name", "row_vals"], value_vars=["A", "B"], var_name="col_name", value_name="col_vals")

#plot data into a FacetGrid
g = sns.FacetGrid(df_plot, col="col_name", row="row_name", sharex=False, sharey=False)
g.map(sns.scatterplot, "col_vals", "row_vals")
plt.tight_layout()
plt.show()

示例输出: enter image description here

我不相信在这种情况下,seaborn 比使用 matplotlib 直接将行与列变量绘制到网格中具有优势。

答案 1 :(得分:0)

以下设置可让您为散点图数组选择因变量和自变量。如果从主题上讲这是您想要的,我可以根据需要将设置调整为2x3矩阵。我还可以在每个子图中添加回归线。

图:

enter image description here

enter image description here

代码:

# imports
from plotly.subplots import make_subplots
import plotly.graph_objs as go
import pandas as pd
import numpy as np

# data
np.random.seed(123)
frame_rows = 15

n_cols = 5

frame_columns = ['V_'+str(e) for e in list(range(1,n_cols+1))]
df = pd.DataFrame(np.random.uniform(-8,10,size=(frame_rows, len(frame_columns))),
                  index=pd.date_range('1/1/2020', periods=frame_rows),
                    columns=frame_columns)
df=df.cumsum()+100
df.iloc[0]=100

# define dependent and independent variables
y_list = ['V_1', 'V_2']
x_list = ['V_3', 'V_4', 'V_5']

# plotly
n_plots = len(y_list)*len(x_list)
fig = make_subplots(rows=n_plots, cols=1)

row_count=1
names = []
for y in y_list:
    for x in x_list:

        fig.add_trace(go.Scatter(x=df[x].values, y=df[y].values,                                
                                 mode = 'markers',
                                 ),

                      row=row_count,
                      col=1)

        names.append(y+'=f('+x+')')

        # axis titles
        fig.update_xaxes(title = x, row = row_count)
        fig.update_yaxes(title = y, row = row_count)
        row_count+=1

fig.update_layout(height=n_plots*250, width=600)
fig.show()