简体   繁体   English

如何使用Python在不同文件的单个图形中创建多行图表?

[英]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.

相关问题 如何在Python中从多个文件创建行生成器 - How to create a generator of lines from multiple files in Python Python 中的单个图形中的多个箱线图 - Multiple boxplot in a single Graphic in Python 使用 python 在同一图中绘制来自不同数据文件的多行 - plot in the same figure multiple lines from different data files with python 如何在Python中使用不同的名称创建多个文件 - How to create multiple files with different name in Python 如何使用任何Windows程序例如Python将多个文件中的前n行删除到单个输出文件 - How to remove the first n lines from multiple files to a single output file using any windows program E.g Python 如何用多行从2个不同的数据集中创建可视化? - How to create visualization from 2 different data sets with multiple lines? 如何使用python比较来自两个不同文件的字符串? - How to compare strings of lines from two different files using python? Python:如何绘制带有多条线的图形? - Python: How to draw a graphic with multiples lines? 来自不同文件的多行打印 - Multiple lines from different files printing Plotly:如何在来自同一 Pandas 数据帧的不同列的一个绘图图表中绘制多条线? - Plotly: How to plot multiple lines in one plotly chart from different columns from the same pandas dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM