1. 概述
pyplot是替代matlab的一个很好的选择,不过默认的曲线绘图不够美观,这里对一些细节参数进行了修改,以供各位参考。
2. 绘图效果对比
![]() | ![]() |
注:图中曲线数据来源是对文献
Hu Chengzhi, Tang Dawei, Lv Jizu, et al. Molecular dynamics simulation of frictional properties of Couette flow with striped superhydrophobic surfaces under different loads.. 2019, 21(32):17786-17791.
的复现与学习,感兴趣的同学可以阅读原文。
3. 修改前后代码
注:以下示例程序中不包含数据,不可直接运行。
3.1 导入库
import matplotlib.pyplot as plt
import matplotlib as mp
3.2 修改前
plt.figure()
plt.plot(d_plot1[:, 0]-8, d_plot1[:, 1], label='Smooth',color='r',marker="*")
plt.plot(d_plot2[:, 0]-8, d_plot2[:, 1], label='Rect',color='b',marker="o")
plt.xlabel(xlabel='y (Å)')
plt.ylabel(ylabel='Number ratio')
plt.legend()
plt.xlim((0, 100))
plt.show()
3.3 修改后
plt.figure(figsize=(8, 5)) # 打开指定大小的窗口
plt.axes([0.16, 0.16, 0.75, 0.75]) # 偏移绘图区域,从而完整显示坐标轴名称
plt.plot(d_plot1[:, 0]-8, d_plot1[:, 1], label='Smooth',color='r', linewidth=3, marker="*", markersize= 12)
plt.plot(d_plot2[:, 0]-8, d_plot2[:, 1], label='Rect',color='b', linewidth=3, marker="o", markersize= 8)
# 图例名称 颜色 曲线线宽 标记符号 标记符号尺寸
plt.xticks(fontsize= 13, fontfamily= "Times New Roman") # x 轴刻度字体大小,字体类型
plt.yticks(fontsize= 13, fontfamily= "Times New Roman") # y 轴刻度字体大小,字体类型
plt.tick_params(direction= "in", width= 2.0, length= 5.0) # 刻度显示在绘图区域内侧,宽度2.0,高度5.0
plt.axhline(y=plt.ylim()[0], xmin= 0.0, color= "k", linewidth= 3.0) # 加粗显示 x 轴
plt.axvline(x=plt.xlim()[0], ymin= 0.0, color= "k", linewidth= 4.0) # 加粗显示 y 轴
plt.xlabel(xlabel='y (Å)', fontsize= 18, fontfamily= "Times New Roman") # x轴名称及字号字体
plt.ylabel(ylabel='Number ratio', fontsize= 18, fontfamily= "Times New Roman") # y轴名称及字号字体
# plt.ylabel(ylabel='Friction force(eV/A)', fontsize= 18, fontfamily= "Times New Roman")
legend_font= mp.font_manager.FontProperties(family="Times New Roman", size= 12)# 定义字体
plt.legend(prop= legend_font,loc='upper left', bbox_to_anchor=(0.02, 0.98)) # 图例字体
# plt.savefig('temp.png') # 保存图片
plt.xlim((0, 100)) # 设置显示范围
plt.show() # 显示曲线
本文介绍了如何使用matplotlib.pyplot改进曲线图的外观,通过代码示例展示了修改前后的对比,适用于希望提升Python图表质量的读者。



1万+

被折叠的 条评论
为什么被折叠?



