matplotlib 详解1-基本图形

来源:互联网 发布:linux查看重启日志 编辑:程序博客网 时间:2024/06/08 01:17

1.  初始

importmatplotlib.pyplot as plt

plt.plot([1,2,3],[3,2,1])

plt.show()


2 . 饼图

labels= 'A','B','C','D'

fracs = [15,20,35,30]

plt.axes(aspect = 1) # X Y轴变成1:1

explode =[0,0.05,0,0]

plt.pie(x=fracs,labels=labels,autopct='%.0f%%',explode = explode, shadow = True)

#autopct ='%.0f%%'是将百分比(0表示取零位小数)显示出来,explode= explode 是将饼图部分凸显出来。#shadow 显示阴影。

plt.show



3. 箱体图

importnumpy as np

np.random.seed(100)

 

data =np.random.normal(size =1000, loc =0,scale =1)#产生1000个随机数,平均数为0,scale = 方差

plt.boxplot(data,sym  ='o',whis =1.0)#whis = 上边缘高度。sym:  异常值符号

plt.show()



importnumpy as np

np.random.seed(100)

 

data =np.random.normal(size =(1000,4),loc=1, scale =2)

labels = ['A','B','D','C']

plt.boxplot(data,labels = labels,sym  ='x',whis =1.0)

plt.show()


4 散点图

height =[161,170,182,175,173,165]

weight = [50,58,80,70,69,55]

 

plt.scatter(height, weight)

plt.show()


N =1000

x = np.random.randn(N)

y = x+np.random.randn(N)*0.7

plt.scatter(x,y)

plt.show()



下面代码因无文件未有验证:

open, close = np.loadtxt('000001.csv',delimiter= ',', skiprows = 1, usecols = (1,4),unpack = True)

change = close - open

yeasterday = change[:-1]

today = change [1:0]

plt.scatter(yesterday, today, s=100,c ='r',marker ='o', alpha = 0.5)

plt.show


 5. 柱状图

N = 5

y = [20,10,30,25,15]

index = np.arange(N)

#pl = plt.bar(left = index, height = y, color ='red', width =0.5)

pl = plt.bar(left = 0, bottom = index, width = y, color ='red',height=0.5, orientation = 'horizontal')

plt.show()




index = np.arange(4)

sales_BJ  = [52,55,63,53]

sales_SH = [44,66,55,41]

bar_width = 0.3

 

plt.bar(index, sales_BJ, bar_width, color ='b')

#plt.bar(index+bar_width, sales_SH, bar_width, color ='r')

plt.bar(index, sales_SH,bar_width, color = 'r', bottom = sales_BJ)

plt.show



折线图

x = np.linspace(-10,10,1000)

y= x**2

plt.plot(x,y)

plt.show




以下代码因缺乏文件未能执行

date,open,close = np.loadtxt('00001.csv',delimiter = ',',

converters={0:mdates.strpdate2num('%m/%d/%Y')},

skiprows=1, usecols =(0,1,4),unpack = True)

plt.plot(date,open)

plt.plot_date(date,open,linestyle=‘-’,color =’red’, marker =’o’)

 

plt.show

六、折方图

mu =100

sigma =20

x = mu + sigma*np.random.randn(20000)

plt.hist(x,bins=100, color ='red',normed =True)

plt.show()


双折方图

x=np.random.randn(1000)+2

y=np.random.randn(1000)+3

plt.hist2d(x,y,bins=40)

plt.show()





原创粉丝点击