简体   繁体   English

如何为每个数据框列创建散点图

[英]How to create a scatter plot for each dataframe column

I am trying to write some code in order to create an animation of scatter plot data through tine.我正在尝试编写一些代码,以便通过 tine 创建散点图数据的动画。 In order to do this I have a dataset with multiple columns where each column represents a numbered timestep.为了做到这一点,我有一个包含多列的数据集,其中每列代表一个编号的时间步长。

I would like the code to cycle through each timestep column for the y axis and use a constant x axis, so that a separate scatter plot is generated for each timestep.我希望代码在 y 轴的每个时间步长列中循环并使用常量 x 轴,以便为每个时间步长生成单独的散点图。 I tried to do this by coding a for loop that specifies an incrementing column number for the y axis.我试图通过编写一个 for 循环来为 y 轴指定一个递增的列号来做到这一点。

My current code generates three out of seven scatter plots in my sample data but returns the following error:我当前的代码在我的示例数据中生成七个散点图中的三个,但返回以下错误:

IndexError: index 9 is out of bounds for axis 0 with size 9

I have tried other similar solutions on stack overflow but that didn't correct my problem.我已经尝试过其他类似的堆栈溢出解决方案,但这并没有解决我的问题。

The data is here if anyone wants to use what I am using: https://www.dropbox.com/s/7vwa0lud44td2ak/test_splot_anim_noTS.csv?dl=0 data file如果有人想使用我正在使用的数据,则数据在这里: https : //www.dropbox.com/s/7vwa0lud44td2ak/test_splot_anim_noTS.csv?dl=0 数据文件

Any help or advice would be much appreciated.任何帮助或建议将不胜感激。

import numpy as np
import pandas a pd
import matplotlib as mpl
import matplotlib.pyplot as plt

data=pd.read_csv("test_splot_anim_noTS.csv") 

for n in range (6, 13):
    data.plot(kind='scatter', x='metres', y=n)
    plt.ylim(-4,4)
    plt.savefig('n.jpeg')

panda.DataFrame.plot , single line plot panda.DataFrame.plot ,单线图

data=pd.read_csv("test_splot_anim_noTS.csv")
data.set_index('metres', drop=True, inplace=True)
data.plot()

在此处输入图片说明

With matplotlib , single plot with all columns:使用matplotlib ,所有列的单图:

import matplotlib.pyplot as plt

plt.plot(data)
plt.show()

Separate scatter plots, files saved:单独散点图,文件保存:

for col in data.columns:
    plt.scatter(data.index, data[col])
    plt.ylim(-4, 4)
    plt.savefig(f'{col}.jpeg')
    plt.show()

With Seaborn:使用 Seaborn:

for col in data.columns:
    sns.scatterplot(data.index, data[col])
    plt.ylim(-4,4)
    plt.savefig(f'{col}.jpeg')
    plt.show()
 data=pd.read_csv("test_splot_anim_noTS.csv")
 for column in data.columns[1:]:
     data.plot(kind='scatter', x='metres',y=column)
     plt.ylim(-4,4)
     plt.savefig('{}.jpeg'.format(column))

I may have done it!我可能已经做到了!

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

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