python-matplotlib学习(1)

1 import matplotlib.pyplot as plt
2 import numpy as np
3 
4 x=np.linspace(-1,1,50)
5 y=2*x+1
6 plt.plot(x,y)
7 plt.show()

2:绘制y=x*x 的图像

1 import matplotlib.pyplot as plt
2 import numpy as np
3 
4 x=np.linspace(-1,1,50)
5 #y=2*x+1
6 y=x**2
7 plt.plot(x,y)
8 plt.show()

3:figure 的认识

 1 import matplotlib.pyplot as plt
 2 import numpy as np
 3 
 4 x=np.linspace(-1,1,50)
 5 y1=2*x+1
 6 y2=x**2
 7 
 8 plt.figure("第一个figure")
 9 plt.plot(x,y1)
10 
11 plt.figure() #看看默认的名字
12 plt.plot(x,y2)
13 plt.plot(x,y1,color='red',linewidth=2.0,linestyle='--') # 绘制两条线 颜色为红色,宽度为2,虚线
14 
15 
16 plt.show()

 1 import matplotlib.pyplot as plt
 2 import numpy as np
 3 
 4 x=np.linspace(-3,3,50)
 5 y1=2*x+1
 6 y2=x**2
 7 
 8 plt.figure() #看看默认的名字
 9 plt.plot(x,y2)
10 plt.plot(x,y1,color='red',linewidth=2.0,linestyle='--') # 绘制两条线 颜色为红色,宽度为2,虚线
11 plt.xlim(-1,2) #x轴取值范围
12 plt.ylim(-2,3) #y轴取值范围
13 
14 plt.xlabel("x轴")
15 plt.ylabel("y轴") #描述
16 
17 plt.show()
View Code

中文乱码出现解决方法

找到matplot安装路径下的matplotlibrc 文件用文本编辑工具打开并修改 
#font.family         : sans-serif 去掉 #

#font.sans-serif 这一行 去点 # 并添加微软雅黑字体 

这是网上查到的解决办法我试了一下然并卵啊。

不了了之了暂时,等找到更好的方法后再说吧

替换坐标轴度量值

 1 import matplotlib.pyplot as plt
 2 import numpy as np
 3 
 4 x=np.linspace(-3,3,50)
 5 y1=2*x+1
 6 y2=x**2
 7 
 8 plt.figure("figure能不能用中文") #看看默认的名字
 9 plt.plot(x,y2)
10 plt.plot(x,y1,color='red',linewidth=2.0,linestyle='--') # 绘制两条线 颜色为红色,宽度为2,虚线
11 
12 plt.xlim(-1,2) #x轴取值范围
13 plt.ylim(-2,3) #y轴取值范围
14 
15 
16 plt.xlabel("I am x")
17 plt.ylabel("I am y") #描述
18 #plt.legend(prop=font)
19 
20 new_ticks=np.linspace(-1,2,5)
21 print(new_ticks)
22 plt.xticks(new_ticks)
23 plt.yticks([-2,-1.8,-1,1.22,3],[r"$really\ bad$",r"$bad$","$normal$","$good$","$really\ good$"])
24 
25 plt.show()

 1 import matplotlib.pyplot as plt
 2 import numpy as np
 3 
 4 x=np.linspace(-3,3,50)
 5 y1=2*x+1
 6 y2=x**2
 7 
 8 plt.figure("figure能不能用中文") #看看默认的名字
 9 plt.plot(x,y2)
10 plt.plot(x,y1,color='red',linewidth=2.0,linestyle='--') # 绘制两条线 颜色为红色,宽度为2,虚线
11 
12 plt.xlim(-1,2) #x轴取值范围
13 plt.ylim(-2,3) #y轴取值范围
14 
15 
16 plt.xlabel("I am x")
17 plt.ylabel("I am y") #描述
18 #plt.legend(prop=font)
19 
20 new_ticks=np.linspace(-1,2,5)
21 print(new_ticks)
22 plt.xticks(new_ticks)
23 plt.yticks([-2,-1.8,-1,1.22,3],[r"$really\ bad$",r"$bad$","$normal$","$good$","$really\ good$"])
24 
25 #gca ='get current axis'
26 
27 ax=plt.gca()
28 ax.spines['right'].set_color('none') #右侧轴隐藏
29 ax.spines['top'].set_color('none') #上侧轴隐藏
30 ax.xaxis.set_ticks_position('bottom')
31 ax.yaxis.set_ticks_position('left')
32 ax.spines["bottom"].set_position(('data',0)) # 设置x轴位置
33 ax.spines["left"].set_position(('data',0))
34 
35 plt.show()
View Code

 1 import matplotlib.pyplot as plt
 2 import numpy as np
 3 
 4 x=np.linspace(-5,5,100)
 5 y1=2*x+1
 6 y2=x**2
 7 
 8 plt.xlim(-5,5) #x轴取值范围
 9 plt.ylim(-5,25) #y轴取值范围
10 plt.xlabel("I am x")
11 plt.ylabel("I am y") #描述
12 
13 l1,=plt.plot(x,y1,label="y=2x+1")
14 l2,=plt.plot(x,y2,color='red',linewidth=1.0,linestyle='--',label="y=x*x")# 逗号为必须
15 plt.legend(handles=[l1,l2,],labels=["aaa","bbb"],loc='best')
16 
17 plt.show()
View Code

 

转载于:https://www.cnblogs.com/shuiliuhualuo/p/6520539.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值