[英]How create Multiple Lines Chart with Python in a single graphic from different files?
I'm currently working with many different time series csv files. 我目前正在处理许多不同的时间序列csv文件。 One file of them contains the observed temperatures for different hours and the n others files contain the predicted temperature from n different predictors.
其中一个文件包含不同小时的观察温度,另外一个文件包含来自n个不同预测变量的预测温度。
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. 我正在寻找Python代码来创建多个行(一个用于观察到的温度,另一个用于预测器的温度)图表按时隙分组。
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 我已经尝试过来自不同网站的许多解决方案但是,他们没有处理每行实际上是不同的csv文件的情况,而只是处理同一csv文件中的每一行的情况
(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. 所以,我正在寻找可以考虑不同的csv文件的python代码,并将每个文件与一行相关联,以获得多线图。
Well, first thing you need to do is load the csv files. 好吧,首先需要做的是加载csv文件。
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. 或者,如果您只需要csv中的一列,则可以执行以下操作。
# 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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.