python
复制代码
import matplotlib.pyplot as plt
import numpy as np
plt.figure(figsize=(8, 5)) # 设置图表大小
import mpl_toolkits.axisartist as axisartist
#定义主画板
fig = plt.figure(figsize=(8, 5))
# 定义艺术画板名称为ax,添加子图参数中绑定主画板
ax = axisartist.Subplot(fig,111)
#主画板中添加艺术画板
fig.add_axes(ax)
#实现箭头坐标轴装饰,首先在定义调用坐标轴位置,然后确定轴线_类型,
# 箭头样式:空->,实-|>
# 箭头尺寸:size=1.5
ax.axis['bottom'].set_axisline_style('-|>',size=1.5)
ax.axis['left'].set_axisline_style('->',size=1.8)
#艺术画板中隐藏坐标轴
ax.axis["top"].set_visible(False)
ax.axis["right"].set_visible(False)
plt.style.use('classic')
plt.rcParams['font.sans-serif'] = 'Simsun, Times New Roman' # 选择一个本地的支持中文的字体
plt.rcParams['axes.unicode_minus'] = False #解决符号无法显示
fontsize = 12
x1 = [0,0.5,2,16]
y1 = [5.67,9.19,5.83,3.842]
x2 = [0,0.5,2,16]
y2 = [10.58,15.738,10.08,5.928]
# 创建图表
# 添加网格线
plt.grid(True, linestyle='--', alpha=0.1)
# 绘制折线图
plt.plot(x1, y1, marker='o', linestyle='-', color='b', label='Group1',linewidth=0.5)
plt.plot([x1[0], x1[3]], [y1[0], y1[3]], marker='o', linestyle='-', color='b',linewidth=0.5)
plt.plot(x2, y2, marker='^', linestyle='-', color='r', label='Group2',linewidth=0.5)
plt.plot([x2[0], x2[3]], [y2[0], y2[3]], marker='^', linestyle='-', color='r',linewidth=0.5)
# # 需要标注的关键节点
# key_points1 = [(1,8.1), (12, 4.3), (5, 5.4)]
# key_text1 = ['-2.24','-0.114','-0.246']
# plt.annotate(
# f'{key_text1[0]}', # 标注文本
# xy=key_points1[0], # 标注点的坐标
# xytext=(key_points1[0][0]+0.5,key_points1[0][1]+0.5), # 文本位置
# arrowprops=dict(facecolor='red', arrowstyle='->',edgecolor='r',linewidth=1), # 箭头样式
# fontsize=fontsize, # 字体大小
# color='red' # 文本颜色
# )
# plt.annotate(
# f'{key_text1[1]}', # 标注文本
# xy=key_points1[1], # 标注点的坐标
# xytext=(key_points1[1][0] - 2, key_points1[1][1]-1), # 文本位置
# arrowprops=dict(facecolor='red', arrowstyle='->',edgecolor='r'), # 箭头样式
# fontsize=fontsize, # 字体大小
# color='red' # 文本颜色
# )
# plt.annotate(
# f'{key_text1[2]}', # 标注文本
# xy=key_points1[2], # 标注点的坐标
# xytext=(key_points1[2][0] + 1, key_points1[2][1] + 1), # 文本位置
# arrowprops=dict(facecolor='red', arrowstyle='->',edgecolor='r'), # 箭头样式
# fontsize=fontsize, # 字体大小
# color='red' # 文本颜色
# )
#
# # 需要标注的关键节点
# key_points2 = [(2, 4), (4, 8), (7, 7)]
# key_text2 = ['-3.77','-0.293','-0.506']
# plt.annotate(
# f'{key_text2[0]} ', # 标注文本
# xy=key_points2[0], # 标注点的坐标
# xytext=(int(key_points2[0][0] -1), int(key_points2[0][1] - 1)), # 文本位置
# arrowprops=dict(facecolor='red', arrowstyle='->',edgecolor='red'), # 箭头样式
# fontsize=fontsize, # 字体大小
# color='red' # 文本颜色
# )
# plt.annotate(
# f'{key_text2[1]} ', # 标注文本
# xy=key_points2[1], # 标注点的坐标
# xytext=(key_points2[1][0] -1, key_points2[1][1] - 1), # 文本位置
# arrowprops=dict(facecolor='red', arrowstyle='->',edgecolor='red'), # 箭头样式
# fontsize=fontsize, # 字体大小
# color='red' # 文本颜色
# )
# plt.annotate(
# f'{key_text2[2]}', # 标注文本
# xy=key_points2[2], # 标注点的坐标
# xytext=(key_points2[2][0] -1, key_points2[2][1] - 1), # 文本位置
# arrowprops=dict(facecolor='red', arrowstyle='->',edgecolor='red'), # 箭头样式
# fontsize=fontsize, # 字体大小
# color='red' # 文本颜色
# )
# 添加标题和标签
# plt.title('简单折线图示例', fontsize=16)
plt.xlabel('Time', fontsize=fontsize)
plt.ylabel('Cortisol', fontsize=fontsize)
plt.xlim(0,17)
plt.ylim(0,17)
plt.yticks(np.arange(0,17,1))
plt.xticks(np.arange(0,17,1))
# 添加图例
plt.legend(loc='best', fontsize=fontsize)
plt.savefig(r'折线图.jpg',dpi=300,bbox_inches='tight')
# 显示图表
plt.show()
import webbrowser
webbrowser.open('折线图.jpg')