matplotlib plot函数使用详解

以上图片中的内容来自于plot函数的帮助文档。

代码示例:

# 绘制图表,并设置图表的轴刻度,刻度范围,轴标签
# 创建2 x 2的4个子图
fig, axes = plt.subplots(2, 2, figsize=(8, 10), dpi=100)

#数据点
x = np.arange(10)
y = x + np.random.randn(10)

# 第一张子图绘制虚线(红色,数据点使用圆形标记),线宽为2
axes[0, 0].plot(x, y, color="r", linestyle="--", marker="o", linewidth=2)

# 第二张图绘制虚线(蓝绿色,数据点使用+好标记)
axes[0, 1].plot(x, y.cumsum(), "g-.+")

# 第三张绘制散点图(蓝色,数据点使用上三角形标记)
axes[1, 0].scatter(x, y, color="b", marker="^")

# 第四张图绘制散点图(黑色,数据点使用下三角形标记),alpha设置图像的透明度
axes[1, 1].scatter(x, y.cumsum(), color="k", marker="v", alpha=0.3)

# 设置座标轴的刻度,座标范围,轴标签
# 默认这些标记在当前的(也就是最后一个)subplot上
plt.xlim([-10, 10])
plt.ylim([-50, 50])
plt.xlabel("axis-x")
plt.ylabel("axis-y")
plt.xticks([-1.0, 0.0, 0.5, 1.5, 2.5, 5, 10])
plt.yticks([-10, 0.0, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0])

# 显式指定每个子图的刻度,座标范围,轴标签
# 设置第一张子图axes[0, 0]
axes[0, 0].set_xlabel("subplot(0, 0)-x")
axes[0, 0].set_ylabel("subplot(0, 0)-y")
axes[0, 0].set_xlim([-10, 10])
axes[0, 0].set_ylim([-5, 15])
axes[0, 0].set_xticks([-1.0, 0.0, 0.5, 1.5, 2.5, 5, 10])
axes[0, 0].set_yticks([-1.0, 0.0, 0.5, 1.5, 2.5, 5, 10])

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章