matplotlib

import numpy as np
import matplotlib.pyplot as plt

1.一些常规操作

x = np.linspace(-3,3,50)
y1 = 2*x+1
y2 = x*x

plt.figure(num=1)
l1,=plt.plot(x,y2,label='up')
l2,=plt.plot(x,y1,color='red',linewidth=1.0,linestyle='--',label='down')
#其实也可以直接写plt.plot,但是为了后面演示添加legend,就增加了l1 l2 ;l1,l2后面那个逗号一定要加
plt.xlim(-1,2)
plt.ylim(-2,3)
plt.xlabel('i am x')
plt.ylabel('i am y')

new_xticks = np.linspace(-1,2,5)
new_yticks = [r'$really\ bad$',r'$bad\ \alpha$',r'$normal$',r'$good$',r'$really\ good$']
plt.xticks(new_xticks)
plt.yticks([-2,-1.8,-1,1.22,3],new_yticks)

#移动坐标轴
#gca abbr for get current axis
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom') #设置x坐标轴文字显示在轴下方
ax.yaxis.set_ticks_position('left')#设置y坐标轴文字显示在轴左边
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))
# data表示定位的方式是具体数值
# 此外还可以填'axes',后面的数值表示坐标轴在x(或y)的范围的百分之多少
# 比如ax.spines['bottom'].set_position(('axes',0.8))就表示把x轴设置在y整体范围的80%处

#设置legend
plt.legend(handles=[l2,],labels=['aaa'],loc='lower right')
#只给l2设置图例,如果不指定,就从第一条线开始添加图例


#设置标注
# method1
x0 = 0.5
y0 = 2*x0+1
plt.scatter(x0,y0,s=50,color='b')
plt.plot([x0,x0],[0,y0],'k--',lw=2.5) #画出那根黑色的虚线
plt.annotate(r'$2x+1=3$',
             xycoords='data',#用data定位标注的点,即下一行xy是多少,那么标注的点就算是那个
             xy=(x0,y0), #标注的是哪个点
             textcoords='offset points', #标注语句的定位方式:offset points:Offset (in points) from the *xy* value;offset pixels:Offset (in pixels) from the *xy* value
             xytext=(30,-30), #标注语句相对于标注点的偏移量
             fontsize=16, #标注语句的字号
             arrowprops=dict(arrowstyle='->',connectionstyle='arc3,rad=.2')) #标注点与标注语句之间连接线的样式

# method2
plt.text(-1,2,'a simple pic',fontdict={
   'size':16,'color':'purple'})


plt.show()

在这里插入图片描述

2.设置坐标轴标签的可见程度

x = np.linspace(-3, 3, 50)
y = 0.1*x

plt.figure()
plt.plot(x, y, linewidth=10, zorder=1)      # set zorder for ordering the plot in plt 2.0.2 or higher
plt.ylim(-2, 2)
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值