Добавление сводной статистики на график временного ряда
С помощью API pandas для matplotlib и метода table можно совместить график временного ряда и числовую сводку на одном графике:
# Plot the time series data in the DataFrame
ax = df.plot()
# Compute summary statistics of the df DataFrame
df_summary = df.describe()
# Add summary table information to the plot
ax.table(cellText=df_summary.values,
colWidths=[0.3]*len(df.columns),
rowLabels=df_summary.index,
colLabels=df_summary.columns,
loc='top')
Это упражнение является частью курса
Визуализация данных временных рядов в Python
Инструкции к упражнению
Изучите meat_mean в консоли — это DataFrame, содержащий средние значения для всех временных рядов в meat.
- Присвойте все значения
meat_meanаргументуcellText. - Присвойте все значения индекса
meat_meanаргументуrowLabels. - Присвойте названия столбцов
meat_meanаргументуcolLabels.
Интерактивное практическое упражнение
Попробуйте выполнить это упражнение, дополнив этот пример кода.
# Plot the meat data
ax = meat.plot(fontsize=6, linewidth=1)
# Add x-axis labels
ax.set_xlabel('Date', fontsize=6)
# Add summary table information to the plot
ax.table(cellText=meat_mean.____,
colWidths = [0.15]*len(meat_mean.columns),
rowLabels=meat_mean.____,
colLabels=meat_mean.____,
loc='top')
# Specify the fontsize and location of your legend
ax.legend(loc='upper center', bbox_to_anchor=(0.5, 0.95), ncol=3, fontsize=6)
# Show plot
plt.show()