使用Python+matplotlib绘图进行可视化,在图形中创建轴域并设置轴域并设置轴域的位置和大小,同时演示位置坐标轴标签和图例位置的用法。
- 首先需要安装numpy和matplotlib库,这里添加清华大学镜像源,下载更快
- windows打开cmd输入:
pip install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple
pip3 install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 4 * np.pi, 0.1)
y1 = np.sin(x)
y2 = np.cos(x)
ax1 = plt.axes([0.1, 0.15, 0.8, 0.3])
l1, = ax1.plot(x, y1, 'r-', lw=2)
ax1.set_xlabel('x', fontsize=14, position=(1, 0))
ax1.set_ylabel('y', fontsize=14, rotation=0, position=(0, 1))
ax1.spines['right'].set_visible(False)
ax1.spines['top'].set_visible(False)
ax2 = plt.axes([0.1, 0.6, 0.8, 0.3])
l2, = ax2.plot(x, y2, 'g--', lw=2)
ax2.set_xlabel('x', fontsize=14)
ax2.set_ylabel('y', fontsize=14, rotation=0)
ax1.legend([11, 12],
['sin curve', 'cos curve'],
loc='lower right',
bbox_to_anchor=(1, 1.01))
plt.title('sin-cos curve', fontsize=18)
plt.show()