1. 数据
这里使用模拟数据
import random
import matplotlib.pyplot as pltvalues = [i * random.random() for i in range(100)]
values[0.0,0.2596417433881839,1.0607353016866907,
...89.24287458194097,78.48300255421442]
2.坐标原点-左下角
坐标系原点在左下角,正常坐标系
def plot_normal(values):"""1. 正常坐标系"""plt.figure(figsize=(6.4, 3.2), dpi=100)# 画图plt.plot(values)# 坐标轴范围plt.axis((0, 100, 0, 100))# 坐标轴区间: x 为 10 , y 为 20plt.xticks([i * 10 for i in range(11)])plt.yticks([i * 20 for i in range(6)])plt.tight_layout()plt.show()plot_normal(values)
3.坐标原点-左上角
坐标系原点在左上角-图像坐标
def plot_origin_left_top(values):"""2. 坐标原点在左上角"""plt.figure(figsize=(6.4, 3.2), dpi=100)ax = plt.subplot()ax.plot(values)# 坐标轴范围ax.axis((0, 100, 0, 100))# 坐标轴区间: x 为 10 , y 为 20ax.set_xticks([i * 10 for i in range(11)])ax.set_yticks([i * 20 for i in range(6)])# 坐标原点在左上角ax.xaxis.set_ticks_position("top")ax.invert_yaxis()# 显示plt.tight_layout()plt.show()plot_origin_left_top(values)
4.坐标原点-在中间
坐标系原点在中间位置
def plot_origin_center(values):"""3. 坐标原点在中间"""plt.figure(figsize=(6.4, 3.2), dpi=100)ax = plt.subplot()ax.plot(values)# 坐标轴范围ax.axis((-100, 100, -100, 100))# 坐标轴原点在中间ax.spines["top"].set_color("none")ax.spines["left"].set_position("zero")ax.spines["right"].set_color("none")ax.spines["bottom"].set_position("zero")# 显示plt.tight_layout()plt.show()plot_origin_center(values)
5.坐标系-辅助点
坐标系,添加辅助点和并标记点文字信息
def plot_point_and_text(values, points):"""4. 绘制辅助点和辅助点文字"""plt.figure(figsize=(6.4, 3.2), dpi=100)ax = plt.subplot()ax.plot(values)# 辅助点point_xx = [point[0] for point in points]point_yy = [point[1] for point in points]point_cc = ["r" for i in range(len(points))]point_labels = ["A", "B", "C", "D"]ax.scatter(x=point_xx, y=point_yy, c=point_cc)for index, point in enumerate(points):ax.text(x=point[0] * 1.01,y=point[1] *1.01,s=point_labels[index],fontsize=10,color="r",style="italic",weight="light",)# 坐标轴范围ax.axis((0, 100, 0, 100))# 坐标轴区间: x 为 10 , y 为 20ax.set_xticks([i * 10 for i in range(11)])ax.set_yticks([i * 20 for i in range(6)])# 显示plt.tight_layout()plt.show()points = [[30, 30], [50, 50], [80, 80]]
plot_point_and_text(values, points)
6.坐标系-辅助线
坐标系,添加横向和纵向辅助参考线
def plot_hline(values, lines):"""5. 绘制辅助线"""plt.figure(figsize=(6.4, 3.2), dpi=100)ax = plt.subplot()ax.plot(values)# 方式1: 基于坐标轴的辅助线ax.axhline(y=40, c="lightgrey", ls="dotted")ax.axhline(y=80, c="lightgrey", ls="dotted")# 方式2: 画线for line in lines:line_xx = [point[0] for point in line]line_yy = [point[1] for point in line]ax.plot(line_xx, line_yy, c="r", ls="dotted")# 坐标轴范围ax.axis((0, 100, 0, 100))# 坐标轴区间: x 为 10 , y 为 20ax.set_xticks([i * 10 for i in range(11)])ax.set_yticks([i * 20 for i in range(6)])# 显示plt.tight_layout()plt.show()lines = [([50, 0], [50, 100]),([20, 40], [80, 80])]
plot_hline(values, lines)
- 灰色为方式一,添加的灰色辅助线
- 红色为方式二,添加的红色辅助线