0

Example Input Data:

Image of example input data

I am a beginner in python. I use for loop to read several csv files look like above(all of those file are same format).

so far my code was look like below.

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

pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)

ax, fig = plt.subplots(4,4,sharex=False, sharey=False, figsize=(22, 10), dpi=70, linewidth=0.5)
ax = np.array(ax)

def loop_directory(directory):
    for filename in os.listdir(directory):
        if filename.endswith(".csv"):
            file_directory = os.path.join(directory, filename)
            # print(filename)
            df = pd.read_csv(file_directory)

            df = df[df['Tavg'].isin([-999]) == False]

            df[['Year','Month']] = df[['Year','Month']].astype(int).astype(str)
            df["Year&Month"] = df[['Year', 'Month']].agg("/".join,axis=1)

            df["Year&Month"] = pd.to_datetime(df["Year&Month"])
            x = df["Year&Month"]
            y = df["Tavg"]

            for axes,col in zip(x, y):
                axes.plot(df.index, df[col]) # here is the problem, i dont know how to use for loop to plot in subplots
    plt.show()

if __name__ == "__main__":
   loop_directory(r"C:\Users\LAB312\Desktop\vietnam\anomaly")

I've tried for ten more times but didn't work at all.

I want to know how to use those syntaxes ex. ax zip ,etc.

enter image description here

I want to plot in every subplot in one plot. it should have plot every ax.

9S47L852
  • 11
  • 1
  • 1
    Separate the code for data collection code and plotting code. Once you collect the data, use plt subplots or refer to matplotlib examples. – sam May 12 '21 at 13:18
  • Can you clarify what the issue is? Have you checked the matplotlib documentation? – AMC May 12 '21 at 13:38

1 Answers1

0

Firstly, you have your fig and ax reversed in your call to plt.subplots, it should be:

fig, ax = plt.subplots(4,4,sharex=False, sharey=False, figsize=(22, 10), dpi=70, linewidth=0.5)

You can then access each set of axes to call plot by indexing. You can index the 4 by 4 numpy array to get each axes set in your 4 by 4 grid of plots. i.e. ax[0, 0].plot(...), ax[0, 1].plot(...), etc. up to ax[3, 3].plot(...)

Your question needs a bit more information to clarify how you want the data plotted though! I can see you combine the first two columns so that you have 4 columns, but consider how do you want each sample to be plotted.


EDIT: As you want to plot your files sequentially in ax[0, 0], ax[0, 1], etc., you can flatten the 2D numpy array of axes to get a 1D iterable that you can loop through or index with one value. I don't have your files so I can't test it but here's some demo code that should give you an idea of what to do.

As @sam mentioned in the comments, you should seperate your csv collection logic and your plotting logic.

def loop_directory(directory):
    # Get all files, filter for '.csv' and prepend dir path
    files = os.listdir(directory)
    csvs = [os.path.join(directory, f) for f in files if f.endswith('.csv')]
    return csvs

def plot_csvs(csvs):
    fig, ax = plt.subplots(4, 4, sharex=False, sharey=False, figsize=(22, 10), dpi=70, linewidth=0.5)
    ax = np.array(ax).flatten()  # Flatten to 1D, [0 ,0], [0, 1], etc

    # This assumes number of subplots >= number of CSVs
    for i, filename in enumerate(csvs):
        df = pd.read_csv(filename)

        # Do your processing here

        x = df["Year&Month"]
        y = df["Tavg"]

        ax[i].plot(x, y)

    plt.show()
    
csv_dir = '/path/to/csv/dir'
csv_paths = loop_directory(csv_dir)
plot_csvs(csv_paths)
Tim Jim
  • 620
  • 5
  • 19
  • I've shown what kind of plot i want in the newest edition. Could you check it thank you. – 9S47L852 May 13 '21 at 07:31
  • @9S47L852 sorry, can you be a bit more specific? In your 4x4 grid, what do you want in each plot? I'm assuming you want each column vs year&month (i.e., Precip vs year, Tmin vs year, Tmax vs year, and Tavg vs year) but that only needs 4 plots. What do you want to do with the other 12 subplots? – Tim Jim May 13 '21 at 16:42
  • because I've 15 csv files in that folder, and each file have the same column such as Tmax Tmin Tavg Year Month Precip, but I only need Year and Tavg. I want 15 csv file s that can plot in to 15 subplots with x axis and y axis will be Year and Tavg, respectively. – 9S47L852 May 14 '21 at 03:33
  • Does the order matter? – Tim Jim May 14 '21 at 06:47
  • Yes, I want the first plot in ax[0, 0] and the second plot ax[0, 1], etc. – 9S47L852 May 14 '21 at 06:55
  • Ok, this is all stuff you should mention in your question ;) let me edit the answer – Tim Jim May 14 '21 at 06:59
  • 1
    thank you so much, I was very confuse about it so that made my question not so clear. Sorry – 9S47L852 May 14 '21 at 07:16
  • No problem. It's good to try and work out what you want beforehand though! See if the new edit gives you an idea of how to solve your issue. – Tim Jim May 14 '21 at 07:27
  • 1
    I try it today, it's work, really thank you so much – 9S47L852 May 17 '21 at 09:28
  • Great! If it works for you, feel free to accept the answer =) – Tim Jim May 17 '21 at 11:28