简体   繁体   English

Python:绘制类型系列的熊猫数据框

[英]Python: plot panda dataframe of type series

I have a dataframe (df) with 10 rows looking like this 我有一个带有10行的数据框(df),如下所示

-2.00    [-24.4907, -24.4594, -24.4321, -24.4012, -24.3...
-1.75    [-23.8154, -23.7849, -23.7601, -23.7326, -23.7...
-1.00    [-23.7131, -23.6954, -23.6767, -23.6616, -23.6...
-0.75    [-22.7675, -22.7505, -22.741, -22.7173, -22.70...
-0.50    [-22.0693, -22.0718, -22.0481, -22.0328, -22.0...
 0.50    [-15.8461, -15.8247, -15.7963, -15.7784, -15.7...
 1.00    [-7.32122, -7.27283, -7.2336, -7.19238, -7.153...
 1.25    [-3.44732, -3.37547, -3.30565, -3.23125, -3.15...
 1.75    [0.541327, 0.568081, 0.597821, 0.627494, 0.667...
 2.50    [3.63716, 3.68494, 3.73379, 3.77966, 3.82584, ...
dtype: object

I'm not 100% sure but I think it contains ndarrays, I'll give you the info that I have: 我不确定100%,但我认为其中包含ndarrays,我将为您提供以下信息:

type(df)
pandas.core.series.Series

whos
df  Series  -2.00    [-24.4907, -24.4<...>82584, ...\ndtype: object

Anyway, I would like to plot all of these arrays in one plot. 无论如何,我想在一个图中绘制所有这些数组。 I'm able to plot one array using 我可以使用绘制一个数组

plt.plot(df[1])

绘制了一行

And since I have a dataframe of type "series" I hoped using 而且由于我有一个“系列”类型的数据框,所以我希望使用

df.plot
plt.plot()

绘制所有行

would be the solution but it doesn't plot anything. 将是解决方案,但它不会显示任何内容。 Do you know what I do wrong? 你知道我做错了吗?

You have to run a for loop and call plt.plot() repeatedly with the data you want. 您必须运行for循环并使用所需的数据重复调用plt.plot()。 When you come out of the for loop, say plt.show() and all of your plots should be added to the same figure. 当您退出for循环时,说出plt.show(),并且所有图都应添加到同一图中。 Here is what worked for me: 这对我有用:

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

val_list = [np.array([1,2]), np.array([4,3])]
data = {"id": [1,2],
        "values": val_list
       }
df = pd.DataFrame(data)
for i in range(2):
    plt.plot(df.iloc[:, 0], df.iloc[i, 1])
    plt.plot(df.iloc[:, 0], df.iloc[i, 1])
plt.show()

The above code added both plots to my figure 上面的代码在我的图上添加了两个图

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

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