【发布时间】:2021-07-14 02:13:01
【问题描述】:
我正在尝试使用 pandas 和 matplotlib 在同一张图上绘制多条不同的线。
我有一系列 100 条合成温度历史记录,我想绘制这些历史记录,全部为灰色,与我用来生成数据的原始真实温度历史记录相对应。
如何在同一张图表上绘制所有这些系列?我知道如何在 MATLAB 中执行此操作,但我在该项目中使用 Python,并且 Pandas 是我发现从输出文件中读取每一列而无需单独指定每一列的最简单方法。数据的列数将从 100 变为 1000,所以我需要一个通用的解决方案。我的代码单独绘制了每个数据系列,但我只需要弄清楚如何将它们都添加到同一个图中。
这是目前为止的代码:
# dirPath is the path to my working directory
outputFile = "output.csv"
original_data = "temperature_data.csv"
# Read in the synthetic temperatures from the output file, time is the index in the first column
data = pd.read_csv(outputFile,header=None, skiprows=1, index_col=0)
# Read in the original temperature data, time is the index in the first column
orig_data = pd.read_csv(dirPath+original_data,header=None, skiprows=1, index_col=0)
# Convert data to float format
data = data.astype(float)
orig_data = orig_data.astype(float)
# Plot all columns of synthetic data in grey
data = data.plot.line(title="ARMA Synthetic Temperature Histories",
xlabel="Time (yrs)",
ylabel=("Synthetic avergage hourly temperature (C)"),
color="#929591",
legend=None)
# Plot one column of original data in black
orig_data = orig_data.plot.line(color="k",legend="Original temperature data")
# Create and save figure
fig = data.get_figure()
fig = orig_data.get_figure()
fig.savefig("temp_arma.png")
这是输出数据的一些示例数据:
这是原始数据:
单独绘制每个图表会给出这些图表 - 我只是希望它们叠加!
【问题讨论】: