当绘制pandas.DataFrame的列时,它们会自动从列名称中获取其标签。现在可能是,人们想要多次绘制相同的柱子(例如,一次重新采样或作为滚动平均值并且一次是背景中的原始曲线) - 参见下面的示例 - 并且仅标记其中一个。这可以通过混合pandas和matplotlib绘图命令(混合)来实现。 1)是否有不同的方法(通过停用pandas.DataFrame.plot命令的自动标签,或从图例中删除这些项目)? 2)如果只使用熊猫不能实现这一点,我需要混合使用它们,我怎么能得到一个带有“matplotlib.pyplot.plot”和智能pandas x DatetimeIndex标签所示图例的情节?
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
index = pd.date_range("2000-01-01", periods = 1000)
df = pd.DataFrame(np.random.randn(1000, 5), columns = ['1st','2nd','3rd','4th','5th'], index = index)
df['2nd'] = df['2nd']+5
plt.figure()
plt.subplot(1,2,1)
plt.plot(df['1st'], color ='grey')
plt.plot(pd.rolling_mean(df['1st'],window = 60, center = True), color ='b', label='rm_1st')
plt.plot(df['2nd'], color ='grey')
plt.plot(pd.rolling_mean(df['2nd'],window = 60, center = True), color ='r', label='rm_2nd')
plt.title('matplotlib.pyplot.plot')
plt.legend()
plt.figure()
plt.subplot(1,3,1)
plt.plot(df['1st'], color ='grey')
plt.plot(pd.rolling_mean(df['1st'],window = 60, center = True), color ='b', label='rm_1st')
plt.plot(df['2nd'], color ='grey')
plt.plot(pd.rolling_mean(df['2nd'],window = 60, center = True), color ='r', label='rm_2nd')
plt.title('matplotlib.pyplot.plot')
plt.legend()
plt.subplot(1,3,2)
df['1st'].plot( color ='grey')
pd.rolling_mean(df['1st'],window = 60, center = True).plot(color ='b', label='rm_1st')
df['2nd'].plot(color ='grey')
pd.rolling_mean(df['2nd'],window = 60, center = True).plot(color ='r', label='rm_2nd')
plt.title('pandas.DataFrame.plot')
plt.legend()
plt.subplot(1,3,3)
plt.plot(df['1st'].index, df['1st'], color ='grey')
pd.rolling_mean(df['1st'],window = 60, center = True).plot(color ='b', label='rm_1st')
plt.plot(df['2nd'].index, df['2nd'],color ='grey')
pd.rolling_mean(df['2nd'],window = 60, center = True).plot(color ='r', label='rm_2nd')
plt.title('hybrid')
plt.legend()