简体   繁体   English

Matplotlib pandas dataframe散点类型子图表根据同一列

[英]Matplotlib pandas dataframe scatter type subplots according to the same column

Let's say I have a dataframe with 100 rows and 40 columns where column 40 represents the Y axis values for the scatter plots. 假设我有一个包含100行40列的数据框,其中40列表示散点图的Y轴值。 For 39 scatter plots, I would like to plot column 40 in function of column 1, column 40 in function of column 2, column 40 in function of column 3, etcetera up to column 40 in function of column 39. What would be the best way to produce such a subplot without having to do it all manually? 对于39个散点图,我想绘制第40列,第1列,第40列,第2列,第40列,第3列,依此类推,直到第39列的第40列。产生这种子图的方法,而无需手动进行?

For example (with a smaller dataframe), trying to scatter plot column 3 in function of column 1 and column 3 in function of column 2 in a subplot. 例如(使用较小的数据框),尝试将子列中第1列的函数中的第3列散布,而第2列的函数中的第3列散布。

df = pd.DataFrame({'AAA' : [4,5,6,7], 'BBB' : [10,20,30,40],'CCC' : [100,50,-30,-50]})
df.plot(x=["AAA", "BBB"], y=["CCC"], kind="scatter", subplots=True, sharey=True)

One way would be to create the subplots externally and loop over the column names, creating a plot for each one of them. 一种方法是在外部创建子图并在列名上循环,为每个子图创建一个图。

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'AAA' : [4,5,6,7], 'BBB' : [10,20,30,40],'CCC' : [100,50,-30,-50]})

fig, axes = plt.subplots(1,len(df.columns.values)-1, sharey=True)

for i, col in enumerate(df.columns.values[:-1]):
    df.plot(x=[col], y=["CCC"], kind="scatter", ax=axes[i])

plt.show()


Another method which might work in pandas 0.19 is to use the subplots argument. 在pandas 0.19中可能有效的另一种方法是使用subplots参数。 According to the documentation 根据文档

subplots : boolean, default False 子图:布尔值,默认为False
Make separate subplots for each column 为每列分别创建子图

I interprete this such that the following should work, however, I haven't been able to test it. 我对此进行了解释,以便以下内容可以工作,但是,我无法对其进行测试。

 import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({'AAA' : [4,5,6,7], 'BBB' : [10,20,30,40],'CCC' : [100,50,-30,-50]}) df.plot(x=df.columns.values[:-1], y=["CCC" for _ in df.columns.values[:-1]], kind="scatter", subplots=True, sharey=True) plt.show() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM