开始使用免费开始使用

显示并标注图表

正如您之前所见,如果 pandas DataFrame 的索引是日期,pandas 会自动将 x 轴格式化为易读的形式。此外,.plot() 方法还允许您指定多种参数,以定制时间序列图(如线条颜色、线条宽度和图形尺寸)。

您可能注意到用法 ax = df.plot(...),并好奇 ax 对象的作用。这是因为 plot 函数会返回一个 matplotlibAxesSubplot 对象,通常会将其赋值给名为 ax 的变量。这样做还能方便您为图表添加更多标注和设置,例如坐标轴标签。

本练习是课程的一部分

用 Python 可视化时间序列数据

查看课程

练习说明

显示 discoveries DataFrame 的折线图。

  • 将线条颜色指定为 'blue'
  • 将线条宽度设为 2。
  • 将图形尺寸设为长度 8、宽度 3。
  • fontsize 指定为 6。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Plot a line chart of the discoveries DataFrame using the specified arguments
ax = ____.____(____='blue', ____=(8, ____), ____=2, fontsize=____)

# Specify the title in your plot
ax.set_title('Number of great inventions and scientific discoveries from 1860 to 1959', fontsize=8)

# Show plot
plt.show()
编辑并运行代码