0

我正在尝试从 pandas 数据框的多列制作子图。以下代码以某种方式工作,但我想通过将所有图例移到绘图之外(右侧)并est_fmc为每个绘图添加变量来改进它。

L = new_df_honeysuckle[["Avg_1h_srf_mc", "Avg_1h_prof_mc", "Avg_10h_fuel_stick", "Avg_100h_debri_mc", "Avg_Daviesia_mc", 
                        "Avg_Euclaypt_mc", "obs_fmc_average", "obs_fmc_max", "est_fmc"]].resample("1M").mean().interpolate().plot(figsize=(10,15), 
                        subplots=True, linewidth = 3, yticks = (0, 50, 100, 150, 200))

plt.legend(loc='center left', markerscale=6, bbox_to_anchor=(1, 0.4))

任何帮助高度赞赏。

在此处输入图像描述

4

1 回答 1

2

由于 pandas 的绘图功能不允许精细控制,所以最简单的方法是使用 mpl 的 subplotting 功能并通过循环处理来处理。不清楚您是要添加“est_fmc”行还是对其进行注释,所以我添加了该行。有关注释,请参阅

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import colors as mcolors
import numpy as np
import itertools

columns = ["Avg_1h_srf_mc", "Avg_1h_prof_mc", "Avg_10h_fuel_stick", "Avg_100h_debri_mc", "Avg_Daviesia_mc", "Avg_Euclaypt_mc", "obs_fmc_average", "obs_fmc_max",'est_fmc']

date_rng = pd.date_range('2017-01-01','2020-02-01', freq='1m')

df = pd.DataFrame({'date':pd.to_datetime(date_rng)})

for col in columns:
    tmp = np.random.randint(0,200,(37,))
    df = pd.concat([df, pd.Series(tmp, name=col, index=df.index)], axis=1)
    
fig, axs = plt.subplots(len(cols[:-1]), 1, figsize=(10,15), sharex=True)
fig.subplots_adjust(hspace=0.5)
colors = mcolors.TABLEAU_COLORS

for i,(col,cname) in enumerate(zip(columns[:-1], itertools.islice(colors.keys(),9))):
    axs[i].plot(df['date'], df[col], label=col, color=cname)
    axs[i].plot(df['date'], df['est_fmc'], label='est_fmc', color='tab:olive')
    axs[i].set_yticks([0, 50, 100, 150, 200])
    axs[i].grid()
    axs[i].legend(loc='upper left', bbox_to_anchor=(1.02, 1.0))

plt.show()

在此处输入图像描述

于 2021-05-14T08:14:35.473 回答