0

I'm currently working with many different time series csv files. One file of them contains the observed temperatures for different hours and the n others files contain the predicted temperature from n different predictors.

I am looking for Python code to create multiple lines (one for the observed temperature and the others for predictor's temperature) chart grouped by time slot.

I already tried many solution from different web site but, they didn't treat the case where each line is in fact a different csv file but only the case where each line in a column in a same csv file

(here is just an exemple from on web site)

# libraries
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

# Data
df=pd.DataFrame({'x': range(1,11), 'y1': np.random.randn(10), 'y2': np.random.randn(10)+range(1,11), 'y3': np.random.randn(10)+range(11,21) })

# multiple line plot
plt.plot( 'x', 'y1', data=df, marker='o', markerfacecolor='blue', markersize=12, color='skyblue', linewidth=4)
plt.plot( 'x', 'y2', data=df, marker='', color='olive', linewidth=2)
plt.plot( 'x', 'y3', data=df, marker='', color='olive', linewidth=2, linestyle='dashed', label="toto")
plt.legend()

So, I'm looking for python code which could consider different csv file and associate each file with a line in order to obtain a multiple line chart.

JEG
  • 154
  • 1
  • 15

1 Answers1

0

Well, first thing you need to do is load the csv files.

df_temp = pd.read_csv("temperatures.csv")
df_1 = pd.read_csv("predictors1.csv")
df_2 = pd.read_csv("predictors2.csv")
...

Alternatively, if you need only a single column from the csv, you can do the following.

# load the first whole csv
df = pd.read_csv("temperatures.csv")
df["y1"] = pd.read_csv("predictors1.csv")["needed_column"]
df["y2"] = pd.read_csv("predictors1.csv")["needed_column"]

Then your own code should be fine, just adjust the column names as needed.

A short note though, when I was testing, I got a warning about ambiguous definitions of the data columns. Do one of the following to get rid of the warning.

# If using the singe DataFrame version
plt.plot( df.x, df.y1 , marker='o', markerfacecolor='blue', markersize=12, color='skyblue', linewidth=4)
plt.plot( df.x, df.y2 , marker='', color='olive', linewidth=2)
plt.plot( df.x, df.y3 , marker='', color='olive', linewidth=2, linestyle='dashed', label="toto")

# If using the multiple DataFrames version.
plt.plot( df.x, df.y , marker='o', markerfacecolor='blue', markersize=12, color='skyblue', linewidth=4)
plt.plot( df_1.x, df_1.y , marker='', color='olive', linewidth=2)
plt.plot( df_2.x, df_2.y , marker='', color='olive', linewidth=2, linestyle='dashed', label="toto")
emilanov
  • 362
  • 4
  • 15