matplotlib基础绘图

  • 线性图
np.linespace(-3,3,10)  # -3 到 3 十个点
  • 常用的属性
# 颜色宽度风格
 plt.plot(x,y1,color='red',linewidth=1.0,linestyle='--')
 # 图例
 plt.legend(handles=[l1,l2],labels=['test1','test2'],loc='best')
# 自定义坐标轴

new_ticks = np.linspace(-2,2,11)
print(new_ticks)
plt.xticks(new_ticks)
plt.yticks([-1,0,1,2,3],
           ['level1','level2','level3','level4','level5'])
  • 常规坐标轴
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-1,1,100)
y1 = 2*x+1

plt.plot(x,y1,color='red',linewidth=1.0,linestyle='-')
# 得到当前的边框
ax = plt.gca()
## 把右边和上边的边框去掉
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')

## 把x轴刻度对应到下面和左边

ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')

#设置bottom对应到0点
#设置left对应到0点
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))
x0 = 0.5
y0 = 2*x0 + 1

plt.scatter(x0,y0,s=50,color='b')
plt.show()

猜你喜欢

转载自blog.csdn.net/weixin_38331049/article/details/88927297