matplotlib库的简单介绍

一、numpy库的常用语句

import numpy as np
x = np.random.randint(1, 100, 20)
np.savetxt('x.yui', x, fmt='%d', delimiter=',')
y = np.loadtxt('x.yui', delimiter=',')
y.sort()
y.max()
y.min()
y.mean()
y.var()

二、散点图

import matplotlib.pyplot as plt
import numpy as np
N = 1000
x = np.random.randn(N)
y = np.random.randn(N)
plt.scatter(x, y, s=50, c='r', alpha=0.7, marker='1')
#s为点的面积大小,c为颜色,alpha为透明度,marker为点的形状
N = 1000
x = np.random.randn(N)
y = x+np.random.randn(N)*0.3
plt.scatter(x, y, s=50, c='xkcd:sky blue', alpha=0.7, marker='*')

三、折线图

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.dates as mdates
x = np.linspace(-10,10,5)#-10到10的100个等差数列
y = x**2
plt.plot(x, y)
plt.show()
##############下面读入股票数据画图####################
def convert_date(date_bytes):
    return mdates.strpdate2num(' %Y/%m/%d')(date_bytes.decode('ascii'))
#########%Y前有个空格,因为文档中存在空格,如果没空格会报错,所以要仔细查看文档日期
date, high, low = np.loadtxt('C:/Users/Administrator/Desktop/300411.csv', delimiter=',', converters={
   0: convert_date},
                             skiprows=2, usecols=(0, 2, 3), unpack=True)
plt.plot_date(date, high, '-', c='r')##横坐标为日期的特殊画图函数
plt.plot_date(date, low, linestyle='--', c='g',marker='*')
plt.show()

四、条形图

import matplotlib.pyplot as plt
import numpy as np

N = 5
y =[20, 10, 30, 25, 15]
index = np.arange(N)
pl = plt.bar(x=index, height=y,align='edge', color='red',width=0.5)
##水平条形图,有些参数不同于上个函数
plh = plt.barh(y=index, width=y, align='center', color='blue',height=0.5)
plt.show()
############对比图#########
index=np.arange(4)
sales_BJ = [52,56,78,57]
sales_SH = [67,89,75,45]
bar_width=0.4
plt.bar(index, sales_BJ, bar_width, align='edge', color='b')
plt.bar(index+bar_width,sales_SH,bar_width, align='edge', color='r')
plt.show()

plt.barh(y=index, width=sales_BJ,height= bar_width, align='edge', color='b')
plt.barh(y=index+bar_width,width=sales_SH, height=bar_width, align='edge', color='r')
plt.show()
###########层叠图
plt.bar(index,sales_BJ, bar_width, align='edge', color='b')
plt.bar(index,sales_SH, bar_width,color='r', align='edge', bottom=sales_BJ)
##注意bottom的使用
plt.show()
###########层叠图
plt.barh(index,sales_BJ, bar_width, align='edge', color='b')
plt.barh(index,width=sales_SH,height= bar_width,color='r', align='edge',left=sales_BJ)
####注意left的使用
plt.show()

五、直方图
表示数据分布

###########直方图##########
import matplotlib.pyplot as plt
import numpy as np

mu = 100 #均值
sigma = 20 #标准差
x = mu + sigma * np.random.randn(2000
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值