Matplotlib python 数据可视化

Matplotlib python 数据可视化


视频链接

1.如何安装matplotlib

系统是基于ubuntu18.04+python3

首先安装pip3

$ sudo apt install python3-pip
$ pip3 install numpy
$ pip3 install matplotlib

2.基本用法

2.1 画函数

画y=2*x+1的曲线在区间[-1,1]上;

'''
@Description: 
@version: 
@Author: sunshine
@Github: https://subshine.github.io/
@Email: 2182216077@ncepu.edu.cn
@Date: 2020-08-05 11:11:12
@LastEditTime: 2020-08-05 11:16:56
'''
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-1, 1, 50)
## y = 2 * x + 1
y=x**2
plt.plot(x, y)
plt.show()

在这里插入图片描述

在这里插入图片描述

2.2画不同的函数在一张图上

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-1, 1, 50)
y1 = 2 * x + 1
y2 = x ** 2
plt.figure()
plt.plot(x, y1)

plt.figure(num=3, figsize=(8, 5),)#注意这里有,不然不显示第二个曲线
plt.plot(x, y2)
plt.plot(x,y1,color='red',linewidth=1.0,linestyle='--')

plt.show()

在这里插入图片描述

2.2 设置figure图像

'''
@Description: 
@version: 
@Author: sunshine
@Github: https://subshine.github.io/
@Email: 2182216077@ncepu.edu.cn
@Date: 2020-08-05 11:11:12
@LastEditTime: 2020-08-06 22:23:53
'''
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3, 3, 50)
y1 = 2 * x + 1
y2 = x ** 2
plt.figure()
plt.plot(x, y2)
plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')
plt.xlim((-1, 2))#设置x轴的范围
plt.ylim((-2, 3))#设置y轴的范围
plt.xlabel('I am x')
plt.ylabel('I am y')
#set new sticks
new_ticks = np.linspace(-1,2,5)
print(new_ticks)
plt.xticks(new_ticks)
#set tick labels
plt.yticks([-2,-1.8,-1,-1.22,3],[r'$really\ bad$',r'$bad$',r'$normal$',r'$good$',r'$really\ good$'])
plt.show()

在这里插入图片描述

2.3 坐标轴的设置

'''
@Description: 
@version: 
@Author: sunshine
@Github: https://subshine.github.io/
@Email: 2182216077@ncepu.edu.cn
@Date: 2020-08-05 11:11:12
@LastEditTime: 2020-08-06 22:54:26
'''

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3, 3, 50)
y1 = 2 * x + 1
y2 = x ** 2
plt.figure()
plt.plot(x, y2)
plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')
plt.xlim((-1, 2))#
plt.ylim((-2, 3))#
plt.xlabel('I am x')
plt.ylabel('I am y')
#set new sticks
new_ticks = np.linspace(-1,2,5)
print(new_ticks)
plt.xticks(new_ticks)
#set tick labels
plt.yticks([-2,-1.8,-1,-1.22,3],[r'$really\ bad$',r'$bad$',r'$normal$',r'$good$',r'$really\ good$'])
#gca='get current axis'
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))

plt.show()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

纷繁中淡定

你的鼓励是我装逼的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值