Skip to content

Instantly share code, notes, and snippets.

@2niuhe
Last active June 28, 2020 16:19
Show Gist options
  • Save 2niuhe/da7691c87d3a63224f3a3da50000665c to your computer and use it in GitHub Desktop.
Save 2niuhe/da7691c87d3a63224f3a3da50000665c to your computer and use it in GitHub Desktop.
用matplotlib来画二维图,采用中文标签。
#conding:utf-8
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.figure(num=1,figsize=(8,5))
plt.plot([0,1],[3,3],color='red',linewidth=1.0,linestyle='-')
plt.plot([1,3],[3,1],color='red',linewidth=1.0,linestyle='-')
plt.plot([3,4],[1,1],color='red',linewidth=1.0,linestyle='-')
# x,y轴的取值范围
plt.xlim(0, 5)
plt.ylim(0,4)
# x,y轴的标签
plt.xlabel(u'时间')
plt.ylabel(u'速度')
# x,y轴的单位间隔
# new_ticks = np.linspace(0,6,5)
plt.xticks([1,3],[r'$t_d$',r'$t_g$'])
plt.yticks([1,3],[r'$v_g$',r'$v_s$'])
# gca = 'get current axis'
# spines设置figure的边框
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
xf1 = [0,1]
xf2 = [1,3]
plt.fill_between(xf1,3,1,color='blue',alpha=0.25)
plt.fill_between(xf2,[3,1],1,color='blue',alpha=0.25)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment