Python数据可视化:matplotlib使用入门

一、准备工作

命令控制行安装matplotlib

pip install matplotlib

代码中引用

import matplotlib.pyplot as plt
import numpy as np

二、代码用例

1、样例数据生成

x = np.linspace(0.05,20,2000) #Get 2000 numbers evenly in the range from 0.05 to 20
x = np.random.randn(200) #Get 200 numbers randomly from the standard normal distribution
y = np.sin(x) #Get the value of the sin funtion based on x

2、趋势图

#x: the data of the x axis
#y: the data of the y axis
#ls:line style,"-"、":"、"-."、"None"
#lw:line width
#label:this line's name
plt.plot(x,y,ls="-",lw=1,label="plot figure1") #set the parameters of the new line
plt.legend() #set the plot location (default)
plt.show() #Show the plot

#趋势图实例:

x = np.linspace(0.05,20,2000) #Get 2000 numbers evenly from range 0.05 to 20
y = np.sin(x)
z = np.cos(x)
plt.plot(x,y,ls="-",lw=1,label="sin(x)") 
plt.plot(x,z,ls=":",lw=1,label="cos(x)") #set the parameters of the second line
plt.legend() 
plt.show() 

图1 双曲线图样例

3、散点图

plt.scatter(x,y,label="scatter figure1")
plt.legend()
plt.show()

#散点图实例:

x1 = np.linspace(5,500,200) 
x2 = np.linspace(600,1500,50) 
x3 = np.linspace(2000,3000,100)
y1 = np.random.randn(200)
y2 = np.random.randn(50)
y3 = np.random.randn(100)
#c: the color of the point 
plt.scatter(x1,y1,c="r",label="scatter figure1")
plt.scatter(x2,y2,c="g",label="scatter figure2")
plt.scatter(x3,y3,c="b",label="scatter figure3")
plt.legend()
plt.show()

图2 三类型散点图样例 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值