import numpy as np
import matplotlib.pyplot as plt
from pylab import *
numpy 常用来组织源数据: 使用 plot 函数直接绘制上述函数曲线,
可以通过配置 plot 函数参数调整曲线的样式、粗细、颜色、标记等:
曲线图:matplotlib.pyplot.plot(data)
灰度图:matplotlib.pyplot.hist(data)
散点图:matplotlib.pyplot.scatter(data)
箱式图:matplotlib.pyplot.boxplot(data)
# 定义数据部分 x = np.arange(0., 10, 0.2) y1 = np.cos(x) y2 = np.sin(x) y3 = np.sqrt(x)
# 绘制 3 条函数曲线
plt.plot(x, y1, color='blue', linewidth=1.5, linestyle='-', marker='.', label=r'$y = cos{x}$')
plt.plot(x, y2, color='green', linewidth=1.5, linestyle='-', marker='*', label=r'$y = sin{x}$')
plt.plot(x, y3, color='m', linewidth=1.5, linestyle='-', marker='x', label=r'$y = sqrt{x}$')
# 坐标轴上移
ax = plt.subplot(111) ax.spines['right'].set_color('none')
# 去掉右边的边框线 ax.spines['top'].set_color('none') # 去掉上边的边框线
# 移动下边边框线,相当于移动 X 轴
#ax.xaxis.set_ticks_position('bottom')
#ax.spines['bottom'].set_position(('data', 0))
# 移动左边边框线,相当于移动 y 轴
#ax.yaxis.set_ticks_position('left')
#ax.spines['left'].set_position(('data', 0))
# 设置 x, y 轴的取值范围 #plt.ylim(-1.5, 4.0)
# 设置 x, y 轴的刻度值
plt.xticks([2, 4, 6, 8, 10], [r'2', r'4', r'6', r'8', r'10']) plt.yticks([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], [r'-1.0', r'0.0', r'1.0', r'2.0', r'3.0', r'4.0'])
# 添加文字
plt.text(8, 2, r'$x in [0.0, 10.0]$', color='k', fontsize=15) plt.text(8, 1, r'$y in [-1.0, 4.0]$', color='k', fontsize=15)
# 特殊点添加注解
plt.scatter([8,8],[2,2.8], 300, color ='m') # 使用散点图放大当前点
#plt.annotate(r'$2sqrt{2}$', xy=(8, np.sqrt(8)), xytext=(8.5, 2.2), fontsize=16, color='#090909', arrowprops=dict(arrowstyle='->', connectionstyle='arc3, rad=0.1', color='#090909'))
# 设置标题、x轴、y轴
plt.title(r'$the function figure of cos(), sin() and sqrt()$', fontsize=19)
plt.xlabel(r'$the input value of x$', fontsize=18, labelpad=88.8)
plt.ylabel(r'$y = f(x)$', fontsize=18, labelpad=12.5)
# 设置图例及位置 plt.legend(loc='up right')
# plt.legend(['cos(x)', 'sin(x)', 'sqrt(x)'], loc='up right')
# 显示网格线 plt.grid(True)
# 显示绘图 plt.show()