【发布时间】:2021-09-06 14:11:44
【问题描述】:
我正在尝试绘制一个图表来表示从 1980 年 1 月 1 日到 2013 年 12 月 31 日的每月河流流量数据集。
请查看此graph
计划是将“Jan Feb Mar Apr May...Dec”绘制为 x 轴,将排放量 (m3/s) 绘制为 y 轴。图表上的实际线条代表年份。或者,图表上的线条将显示 1980 年至 2013 年每年的月平均值(从 1 月到 12 月)。
DAT = pd.read_excel('Modelled Discharge_UIB_1980-2013_Daily.xlsx',
sheet_name='Karhmong', header=None, skiprows=1,
names=['year', 'month', 'day', 'flow'],
parse_dates={ 'date': ['year', 'month', 'day'] },
index_col='date')
上面是显示它是什么类型的数据
date flow
1980-01-01 104.06
1980-01-02 103.81
1980-01-03 103.57
1980-01-04 103.34
1980-01-05 103.13
... ...
2013-12-27 105.65
2013-12-28 105.32
2013-12-29 105.00
2013-12-30 104.71
2013-12-31 104.42
因为我想比较所有年份,所以我尝试了以下命令
DAT1980 = DAT[DAT.index.year==1980]
DAT1980
DAT1981 = DAT[DAT.index.year==1981
DAT1981
...等
就 x 轴的月份分组而言,我尝试使用命令对月份进行分组
datmonth = np.unique(DAT.index.month)
到目前为止,所有这些命令都没有导致错误
但是当我绘制图表时,我得到了这个错误
绘图命令
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(12,6))
ax.plot(datmonth, DAT1980, color='purple', linestyle='--', label='1980')
ax.grid()
plt.legend()
ax.set_title('Monthly River Indus Discharge Comparison 1980-2013')
ax.set_ylabel('Discharge (m3/s)')
ax.set_xlabel('Month')
axs.set_xlim(3, 5)
axs.xaxis.set_major_formatter
fig.autofmt_xdate()
ax.legend(loc='upper left', bbox_to_anchor=(1, 1))
我得到“ValueError:x 和 y 必须具有相同的第一维,但形状 (12,) 和 (366, 1)”作为错误
然后我尝试了
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(12,6))
ax.plot(DAT.index.month, DAT.index.year==1980, color='purple', linestyle='--', label='1980')
ax.grid()
ax.plot(DAT.index.month, DAT.index.year==1981, color='black', marker='o', linestyle='-', label='C1981')
ax.grid()
plt.legend()
ax.set_title('Monthly River Indus Discharge Comparison 1980-2013')
ax.set_ylabel('Discharge (m3/s)')
ax.set_xlabel('Month')
#axs.set_xlim(1, 12)
axs.xaxis.set_major_formatter
fig.autofmt_xdate()
ax.legend(loc='upper left', bbox_to_anchor=(1, 1))
它比上一张图效果更好,但仍然不是我想要的 (please check out the graph here)
因为我的意图是创建一个类似于this的图表
我衷心感谢您提出的任何建议!非常感谢您,如果您需要任何进一步的信息,请随时询问,我会尽快回复。
【问题讨论】:
标签: python matplotlib plot dimensions timeserieschart