简体   繁体   中英

iterate through each row in dataframe and plot values (scatter) in corresponding x and y columns

I have a dataframe like this with many more variants and values in each x and y list:

                   x         y
variant                       
*BCDS%q3rn  [45, 59]  [18, 14]
F^W#Bfr18   [82, 76]   [12, 3]

How can I iterate through each variant (each row has a unique string) and plot the x and y values in a scatterplot? This would result in ~40 plots, which is what I want so I can draw a relationship for each variant. Please advise. Thank you!

You can walk through the columns of a Pandas ' DataFrame -object and plot them, either with the build-in plot -function (using Matplotlib under the hood) or by calling Matplotlib directly:

import pandas as pd
import matplotlib.pyplot as plt
# create random test data
import numpy as np
df = pd.DataFrame(np.random.randint(0,10,size=(10, 4)), columns=['Col 1','Col 2','Col 4','Col 5'])

fig, axs = plt.subplots(1,2)

for col in df:
    # pandas plotting
    df[col].plot(ax=axs[0])
    #matplotlib plotting
    axs[1].plot(df[col])
axs[0].set_title('pandas plotting')
axs[1].set_title('matplotlib plotting')

在此处输入图像描述

You can convert each column into a list and iterate through them to plot. Below I have created line plots but you could easily convert the code to create scatter plots.

variants = df.index.values.tolist()
x_data = df["x"].to_numpy().tolist()
y_data = df["y"].to_numpy().tolist()

for idx in range(len(variants)):
    plt.plot(x_data[idx], y_data[idx],label= variants[idx])
plt.ylabel('Y')
plt.xlabel('X')
plt.legend()
plt.show()

For your sample data, the plot would look like this: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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