将格式应用于 Jupyter notebook 中的所有子图
Apply formatting to all subplots in Jupyter notebook
在 Jupyter Notebook 中绘制具有不同时间分辨率(每小时、每天、每月)的 3 pandas 数据帧时,我想对所有三个子图应用一致的格式,仅显示月份而不是年份(即一月、2 月、3 月,而不是 2010 年 1 月、2010 年 2 月、2010 年 3 月)。
问题:如何在所有子图中应用格式?
导入库
import matplotlib
import matplotlib.dates
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
import seaborn as sns
%matplotlib inline
创建 3 个数据帧
hourly = pd.DataFrame({'val': np.random.rand(24*365)}, index=pd.date_range('2010-01-01', '2010-12-31 23:00', freq='1H'))
daily = pd.DataFrame({'val': np.random.rand(365)}, index=pd.date_range('2010-01-01', '2010-12-31 23:00', freq='1D'))
monthly = pd.DataFrame({'val': np.random.rand(12)}, index=pd.date_range('2010-01-01', '2010-12-31 23:00', freq='1M'))
绘图并将格式应用于三个子图
def plot2(hourly, daily, monthly):
f, ax = plt.subplots(3, 1, sharex = False, figsize=(16, 14))
hourly[['val']].plot(ax=ax[0], legend=False)
daily[['val']].plot(ax=ax[1], legend=False)
monthly[['val']].plot(ax=ax[2], legend=False)
for axA in ax:
month = matplotlib.dates.MonthLocator()
monthFmt = matplotlib.dates.DateFormatter('%b')
axA.xaxis.set_major_locator(month)
axA.xaxis.set_major_formatter(monthFmt)
for item in axA.get_xticklabels():
item.set_rotation(0)
sns.despine()
plt.tight_layout()
return f, ax
plot2(hourly, daily, monthly)
结果图显示了第二个和第三个图所需的格式,但不是第一个图。
Figure showing the first plot is not formatted properly, but the second and third plots are formatted properly
我正在使用 Python 3.5
似乎 pandas 有一些问题。直接使用 matplotlib 效果更好:
def plot2(hourly, daily, monthly):
f, ax = plt.subplots(3, 1, sharex = False, figsize=(16, 14))
ax[0].plot(hourly.index, hourly[['val']])
ax[1].plot(daily.index, daily[['val']])
ax[2].plot(monthly.index, monthly[['val']])
for axA in ax[::-1]:
month = matplotlib.dates.MonthLocator()
monthFmt = matplotlib.dates.DateFormatter('%b')
axA.xaxis.set_major_locator(month)
axA.xaxis.set_major_formatter(monthFmt)
for item in axA.get_xticklabels():
item.set_rotation(0)
sns.despine()
plt.tight_layout()
return f, ax
在 Jupyter Notebook 中绘制具有不同时间分辨率(每小时、每天、每月)的 3 pandas 数据帧时,我想对所有三个子图应用一致的格式,仅显示月份而不是年份(即一月、2 月、3 月,而不是 2010 年 1 月、2010 年 2 月、2010 年 3 月)。
问题:如何在所有子图中应用格式?
导入库
import matplotlib
import matplotlib.dates
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
import seaborn as sns
%matplotlib inline
创建 3 个数据帧
hourly = pd.DataFrame({'val': np.random.rand(24*365)}, index=pd.date_range('2010-01-01', '2010-12-31 23:00', freq='1H'))
daily = pd.DataFrame({'val': np.random.rand(365)}, index=pd.date_range('2010-01-01', '2010-12-31 23:00', freq='1D'))
monthly = pd.DataFrame({'val': np.random.rand(12)}, index=pd.date_range('2010-01-01', '2010-12-31 23:00', freq='1M'))
绘图并将格式应用于三个子图
def plot2(hourly, daily, monthly):
f, ax = plt.subplots(3, 1, sharex = False, figsize=(16, 14))
hourly[['val']].plot(ax=ax[0], legend=False)
daily[['val']].plot(ax=ax[1], legend=False)
monthly[['val']].plot(ax=ax[2], legend=False)
for axA in ax:
month = matplotlib.dates.MonthLocator()
monthFmt = matplotlib.dates.DateFormatter('%b')
axA.xaxis.set_major_locator(month)
axA.xaxis.set_major_formatter(monthFmt)
for item in axA.get_xticklabels():
item.set_rotation(0)
sns.despine()
plt.tight_layout()
return f, ax
plot2(hourly, daily, monthly)
结果图显示了第二个和第三个图所需的格式,但不是第一个图。 Figure showing the first plot is not formatted properly, but the second and third plots are formatted properly
我正在使用 Python 3.5
似乎 pandas 有一些问题。直接使用 matplotlib 效果更好:
def plot2(hourly, daily, monthly):
f, ax = plt.subplots(3, 1, sharex = False, figsize=(16, 14))
ax[0].plot(hourly.index, hourly[['val']])
ax[1].plot(daily.index, daily[['val']])
ax[2].plot(monthly.index, monthly[['val']])
for axA in ax[::-1]:
month = matplotlib.dates.MonthLocator()
monthFmt = matplotlib.dates.DateFormatter('%b')
axA.xaxis.set_major_locator(month)
axA.xaxis.set_major_formatter(monthFmt)
for item in axA.get_xticklabels():
item.set_rotation(0)
sns.despine()
plt.tight_layout()
return f, ax