简体   繁体   English

如何从单个目录中读取多个csv文件并在Python中单独绘制它们?

[英]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. 我想从目录中读取csv文件并绘制它们,并能够单击箭头按钮逐步浏览绘图并查看不同的绘图。 I want to specify which column and be able to title it as well as I have in the code below as well. 我想指定哪个列,并且能够在下面的代码中标注它。

I am able to read the csv file and plot a single plot with specific columns but I am not sure how to do it with multiple. 我能够读取csv文件并绘制具有特定列的单个图,但我不知道如何使用多个。 I've tried glob but it didn't work, I do not want to concatenate them to a single csv file. 我已经尝试了glob但是它不起作用,我不想将它们连接到单个csv文件。 I have provided my code below. 我在下面提供了我的代码。 Any help would be appreciated. 任何帮助,将不胜感激。 Thank you. 谢谢。

import pandas as pd
import matplotlib.pyplot as plt


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

df = pd.read_csv("/user/Desktop/TestNum1.csv", 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()

You just need to add a for loop over all the files and use glob to collect them. 您只需要在所有文件上添加for循环并使用glob来收集它们。

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. for循环中保持plt.show()将确保绘制每个绘图。 It should be pretty easy to search for 'How to add a title to a plot in python' for answers to your other questions. 搜索“如何为python中的绘图添加标题”应该很容易找到其他问题的答案。

暂无
暂无

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

相关问题 如何从一个目录中读取多个文件并在 python 中读取 append 它们? - How to read multiple files from a directory and append them in python? 从目录中读取多个图像并将其转换为.csv文件 - Read multiple images from a directory and turn them into .csv files 如何在单个目录中读取和写入多个文件? 蟒蛇 - How would I read and write from multiple files in a single directory? Python Python脚本读取一个目录中的多个excel文件并将它们转换为另一个目录中的.csv文件 - Python script to read multiple excel files in one directory and convert them to .csv files in another directory 如何通过 python csv() 函数读取目录中的多个 csv 文件? - how to read multiple csv files in a directory through python csv() function? 如何从指定目录中的多个 csv 文件创建单个 dataframe - How to create a single dataframe from multiple csv files in a specified directory csv文件中的注释如何读取和使用? - How can I read the comments in the csv files and use them? 如何使用python从位于同一目录中的多个zip文件夹中读取csv文件? - How to read csv files from multiple zip folders located in the same directory using python? 如何从多个 csv 文件中读取数据并写入 Python 中的单个 Excel 表的同一张表 - How to read data from multiple csv files and write into same sheet of single Excel Sheet in Python 如何使用 dask 从同一目录中读取多个 .csv 文件? - How read multiple .csv files from the same directory using dask?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM