Skip to content Skip to sidebar Skip to footer

How Can I Read Multiple Csv Files From A Single Directory And Graph Them Separately In Python?

I want to read csv files from a directory and plot them and be able to click the arrow button to step through a plot and look at a different plot. I want to specify which column an

Solution 1:

You just need to add a for loop over all the files and use glob to collect them.

For example,

import pandas as pd
import matplotlib.pyplot as plt
import glob


cols_in = [1, 3]
col_name = ['Time (s), Band (mb)']

# Select all CSV files on Desktop
files = glob.glob("/user/Desktop/*.csv")

for file in files:

    df = pd.read_csv(file, usecols = cols_in, names = 
    col_name, header = None)

    fig, ax = plt.subplots()
    my_scatter_plot = ax.scatter(df["Time (s)"], df["Band (mb)"])

    ax.set_xlabel("Time (s)")
    ax.set_ylabel("Band (mb)")
    ax.set_title("TestNum1")
    plt.show()

Keeping plt.show() inside the for loop will ensure each plot is plotted. It should be pretty easy to search for 'How to add a title to a plot in python' for answers to your other questions.


Post a Comment for "How Can I Read Multiple Csv Files From A Single Directory And Graph Them Separately In Python?"