matplotlib绘图常用命令汇总


import numpy as np

import matplotlib
import matplotlib.pyplot as plt

# 关于matplotlib绘图Times New Roman字体问题
# https://blog.csdn.net/Limonor/article/details/106392436
# 解决字体不一致问题, 设置全局字体为Times New Roman
plt.rc('font', family='Times New Roman')
# 解决Times New Roman字体加粗问题
del matplotlib.font_manager.weight_dict['roman']
matplotlib.font_manager._rebuild()


# Data
x = np.random.rand(10, 1)
y = np.random.rand(10, 1)

# 定义字体
font1 = {'family': 'Times New Roman', 'weight': 'normal', 'size': 10}
font2 = {'family': 'Times New Roman', 'style': 'italic', 'weight': 'normal', 'size': 12}

# 创建figure
fig, ax = plt.subplots(figsize=[10*0.3937008, 4*0.3937008]) # 英寸改为厘米
# fig, ax =  plt.subplots(figsize=[7.5*0.3937008, 4*0.3937008])
# fig, ax =  plt.subplots(figsize=[10*0.3937008, 4*0.3937008])
#fig, ax = plt.subplots(figsize=[15*0.3937008, 7*0.3937008])
#fig, = plt.figure(figsize=[15*0.3937008, 7*0.3937008])

ax.plot(x, y, color='r', linestyle='-', linewidth=1, marker='o', markersize=2, label='this is a ')

#plt.tight_layout(pad=3)

plt.gca().set_xlim(0, 1)
plt.gca().set_ylim(0, 1)

plt.tick_params(labelsize=10)

plt.xlabel('x', font2)
plt.ylabel('y', font2)
plt.title("This is title: {}".format(111), font1)
plt.legend(loc=1, prop=font1, frameon=False)
plt.savefig('demo.png', dpi=1000, bbox_inches='tight')
#plt.tight_layout()
#plt.show()


if __name__ == '__main__':
    pass