1.绘制直线图:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 4)
print(x)
plt.plot(x, x * (-0.5), color="blue", linestyle="-", label="y = x * (-0.5)", linewidth=2, marker="o")
plt.plot(x, x * 1.5, color="green", linestyle="--", label="y = x * 1.5", linewidth=2, marker="*") # g--表示绿色虚线
plt.plot(x, x * 3.0, color="orange", linestyle=":", label="y = x * 3.0", linewidth=2, marker="+")
plt.plot(x, x * 3.5, color="magenta", linestyle="-.", label="y = x * 3.5", linewidth=2, marker="D")
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
plt.xlabel("输出值")
plt.ylabel("输入值")
plt.title(u"直线图")
plt.legend()
plt.show()
结果为:
2.绘制心形
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-8, 8, 1024) # 输出的范围为(-8, 8)
y1 = 0.618 * np.abs(x) - 0.8 * np.sqrt(64 - x ** 2) # 左部分
y2 = 0.618 * np.abs(x) + 0.8 * np.sqrt(64 - x ** 2)