简体   繁体   English

使用for循环分别绘制每个数据框行(在单独的图上)

[英]Plotting each dataframe row separately (on separate plots) using for loop

I have been trying to plot each row of my 265 row dataframe using a for-loop. 我一直在尝试使用for循环绘制265行数据帧的每一行。 However, all the code does is it plots all the 265 rows in one single graph, rather than 265 separate graphs, which I am aiming for. 但是,所有代码所做的只是在一个图形中绘制了所有265行,而不是我针对的是265个独立图形。 Below is my code: 下面是我的代码:

for q in range(265):
    print('This is for row',q)
    row = dataframe.iloc[q].plot()
    row.plot()

Each call to plot() simply adds another artist to the currently opened plot. 每次对plot()的调用只会将另一位艺术家添加到当前打开的情节中。 If you want to save your plots separately, your loop should open a new plot, plot() the graph in it, save it, then close it for each row in the dataframe. 如果要单独保存绘图,则循环应打开一个新绘图,在其中绘制图(),保存它,然后为数据框中的每一行关闭它。

Speaking of which - DataFrames have an iterrows method which will give you direct access to each row so that you don't have to locate by index on every iteration of the loop. 说到这-DataFrames具有iterrows方法,可以直接访问每一行,因此不必在循环的每次迭代中都按索引定位。

import matplotlib.pyplot as plt
import pandas as pd
dataframe = pd.DataFrame(np.random.randint(0,10,size=(10, 4)), columns=range(4))
for q in range(dataframe.shape[0]):
    print('This is for row',q)
    plt.figure(figsize=(5,5))
    plt.plot(dataframe.iloc[q,:])

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

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