0

I have some code that takes a bunch of csv files and makes individual plots using the data. I was able to get help on the code (see here: Automate making multiple plots in python using several .csv files) but now I want to alter it such that wherever the code says name will be replaced with the name of the file in the files vector. For some reasons this works in the final line of code plt.savefig('{}.png'.format(fn)) but nowhere else.

import pandas as pd
from dateutil import parser, rrule
from datetime import datetime, time, date
import time
import os
import matplotlib.pyplot as plt

files = ['a.pd.csv',
    't.pd.csv',
    'r.pd.csv',
    'n.pd.csv',
    'm.pd.csv',
    'k.pod.csv']

for f in files:
    fn = f.split('.')[0]
    dat = pd.read_csv(f)
    df0 = dat.loc[:, ['TimeStamp', 'RF']]
        # Change time format
    df0["time"] = pd.to_datetime(df0["TimeStamp"])
    df0["day"] = df0['time'].map(lambda x: x.day)
    df0["month"] = df0['time'].map(lambda x: x.month)
    df0["year"] = df0['time'].map(lambda x: x.year)
    df0.to_csv("name_1.csv", na_rep="0")  # write to csv

    # Combine for daily rainfall
    df1 = pd.read_csv('name_1.csv', encoding='latin-1',
                  usecols=['day', 'month', 'year', 'RF', 'TimeStamp'])
    df2 = df1.groupby(['day', 'month', 'year'], as_index=False).sum()
    df2.to_csv("name_2.csv", na_rep="0", header=None)  # write to csv

    # parse date
    df3 = pd.read_csv("name_2.csv", header=None, index_col='datetime',
                 parse_dates={'datetime': [1,2,3]},
                 date_parser=lambda x: pd.datetime.strptime(x, '%d %m %Y'))

    def dt_parse(date_string):
        dt = pd.datetime.strptime(date_string, '%d %m %Y')
        return dt

    # sort datetime
    df4 = df3.sort()
    final = df4.reset_index()

    # rename columns
    final.columns = ['date', 'bleh', 'rf']

    final[['date','rf']].plot()

    plt.suptitle('Name Rain 2015-2016', fontsize=20) #changename
    plt.xlabel('DOY', fontsize=18)
    plt.ylabel('Rain / mm', fontsize=16)

    plt.savefig('{}.png'.format(fn))
6
  • Which lines exactly? Do you mean just the suptitle()? If so something like plt.suptitle('Name: {}'.format(fn), fontsize=20) would work. Commented Feb 24, 2017 at 8:01
  • Yup! That works.. Thanks. One other question though.. Do you know how to make the first letter capital in the text that I substitute in the {brackets}? Commented Feb 24, 2017 at 15:04
  • Change fn to fn.title () Commented Feb 24, 2017 at 18:20
  • Hm.That didn't work for me... Also can you check my other question: stackoverflow.com/questions/42444020/… Commented Feb 24, 2017 at 20:05
  • To change the graph title to start with a capital I meant you could use plt.suptitle('Name: {}'.format(fn.title()), fontsize=20) Commented Feb 25, 2017 at 20:37

1 Answer 1

0

Fixed my own problem: df2.to_csv('{}_2.csv'.format(fn), na_rep="0", header=None) # write to csv

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.