使用matplotlib绘制坐标轴

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')
相关推荐
线条11 天前
Matplotlib 安装部署与版本兼容问题解决方案(pyCharm)
matplotlib
HuashuiMu花水木1 天前
Matplotlib笔记4----------图像处理
图像处理·笔记·matplotlib
程序员阿超的博客3 天前
Python 数据分析与机器学习入门 (五):Matplotlib 数据可视化基础
python·信息可视化·数据分析·matplotlib·数据可视化·python教程·pyplot
音程15 天前
Matplotlib绘制矩阵图,plt.matshow/imshow 与 ax.pcolor(pcolormesh)方法的使用
matplotlib
搏博15 天前
基于Python、tkinter、sqlite3 和matplotlib的校园书店管理系统
python·信息可视化·sqlite·matplotlib
勤奋的知更鸟17 天前
Matplotlib 绘图库使用技巧介绍
开发语言·python·matplotlib
@小红花18 天前
Matplotlib快速入门
matplotlib
仟濹21 天前
「Matplotlib 入门指南」 Python 数据可视化分析【数据分析全栈攻略:爬虫+处理+可视化+报告】
python·信息可视化·数据分析·matplotlib
Jay_2725 天前
cloudstudio腾讯云:matplotlib 设置中文字体
腾讯云·matplotlib
Gyoku Mint1 个月前
机器学习×第二卷:概念下篇——她不再只是模仿,而是开始决定怎么靠近你
人工智能·python·算法·机器学习·pandas·ai编程·matplotlib