我创建了一个全年水温的多线图,使用pandas python:
import pandas as pd
filepath = "C:\\Users\\technician\\Desktop\\LowerBD2014.csv"
data = pd.read_csv(filepath, header = 0, index_col = 0)
data.plot(kind = 'line', use_index = True, title="timeseries", figsize=(20,10))
现在,我想为气温添加另一条线。不幸的是,收集数据时的日期和时间并不匹配。我想我可以解决这个问题,我将2个单独的.csv文件导入同一个图表中,但我不确定该怎么做。
任何建议都会很棒。我还可以将所有数据添加到一个文件中,我只是担心如果没有辅助水平轴,气温将无法正确绘制(我也不知道如何执行此操作)。
以下是使用ax = ax为数据集图创建的图:
答案 0 :(得分:1)
正如某位here所说,如果两个csv文件的列相同,则可以按照their代码进行操作。
或强>
您可以尝试将两个CSV文件合并为一个,然后使用它。
file_a = open('first.csv','r')
file_a_data = file_a.read()
file_a.close()
file_b = open('second.csv','r')
file_b_data = file_b.read()
file_b.close()
combined_data = file_a_data + file_b_data
csv = open('test.csv','w')
csv.write(combined_date)
csv.close()
data = pd.read_csv(file_path_to_final_csv, ...,...)
答案 1 :(得分:1)
一旦你的两个csv被导入为两个数据帧,只需绘制第一个分配给命名的matplotlib轴对象(下面的块中的ax),然后将这些轴传递给第二个绘图调用。
import pandas as pd
import numpy as np
# two made-up timeseries with different periods, for demonstration plot below
#air_temp = pd.DataFrame(np.random.randn(12),
# index=pd.date_range('1/1/2016', freq='M', periods=12),
# columns=['air_temp'])
#water_temp = pd.DataFrame(np.random.randn(365),
# index=pd.date_range('1/1/2016', freq='D', periods=365),
# columns=['water_temp'])
# the real data import would look something like this:
water_temp_filepath = "C:\\Users\\technician\\Desktop\\water_temp.csv"
air_temp_filepath = "C:\\Users\\technician\\Desktop\\airtemp.csv"
water_temp = pd.read_csv(water_temp_filepath, header = 0, index_col = 0,
parse_dates=True, infer_datetime_format=True)
air_temp = pd.read_csv(air_temp_filepath, header = 0, index_col = 0,
parse_dates=True, infer_datetime_format=True)
# plot both overlayed
ax = air_temp.plot(figsize=(20,10))
water_temp.plot(ax=ax)