matplotlibplot函数使⽤详解
以上图⽚中的内容来⾃于plot函数的帮助⽂档。
代码⽰例:
# 绘制图表,并设置图表的轴刻度,刻度范围,轴标签
# 创建2 x 2的4个⼦图
fig, axes = plt.subplots(2, 2, figsize=(8, 10), dpi=100)
matplotlib中subplot
#数据点
x = np.arange(10)
y = x + np.random.randn(10)
# 第⼀张⼦图绘制虚线(红⾊,数据点使⽤圆形标记),线宽为2
axes[0, 0].plot(x, y, color="r", line, 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")
# 显式指定每个⼦图的刻度,坐标范围,轴标签
# 设置第⼀张⼦图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])