plot Matplotlib简单绘图
#导入库
import numpy as np
import matplotlib.pyplot as plt
#两个list的取值范围必须一样
a = [1, 2, 3]
b = [4, 5, 6]
#生成可视化的图(横轴,纵轴),返回一个对象(2D线)
#用plt.show()方法可以直接绘图
plt.plot(a, b)
'''
得到
[<matplotlib.lines.Line2D at 0x113265a90>]
'''
#不调用plt.show()的方法,前加一个魔法函数
%matplotlib inline
#直接得到图像
plt.plot(a, b)
得到
[<matplotlib.lines.Line2D at 0x1133bb8d0>]
#另一个魔法函数,得到一个语法执行时间
%timeit np.arange(10)
'''
得到
The slowest run took 22.76 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 616 ns per loop
'''
#plt.plot格式,横轴,纵轴,线风格,b--代表蓝色虚线
plt.plot(a, b, '--')
得到
[<matplotlib.lines.Line2D at 0x113dda5c0>]
#增加两个list
c = [10,8,6]
d = [1,8,3]
#绘图参数,第一组横轴纵轴红色--线,第二组横轴纵轴蓝色*线代表点
plt.plot(a,b, 'r--', c,d, 'b*')
得到
[<matplotlib.lines.Line2D at 0x11412c780>,
<matplotlib.lines.Line2D at 0x11412c978>]
#得到一组arrange
t = np.arange(0.0, 2.0, 0.1)
t.size
'''
得到
20
'''
#以t*π为取值的sin曲线
s = np.sin(t*np.pi)
s.size
'''
得到
20
'''
#绘制第一条红色--线,用横轴t纵轴s得到sin曲线,命名aaaa
plt.plot(t,s,'r--',label='aaaa')
#绘制第二条蓝色--线,用横轴t*2纵轴s得到曲线,命名bbbb
plt.plot(t*2, s, 'b--', label='bbbb')
#定义横轴名称
plt.xlabel('this is x')
#定义纵轴名称
plt.ylabel('this is y')
#定义图形名称
plt.title('this is a demo')
#显示图例
plt.legend()
得到
<matplotlib.legend.Legend at 0x11526ec18>
subplot Matplotlib简单绘图
#导入库
import numpy as np
import matplotlib.pyplot as plt
#生成等差间隔数据linspace,以0.1为间距
x = np.linspace(0.0, 5.0)
#x*π为参数的sin值
y1 = np.sin(np.pi*x)
#x*π*2为参数的sin值
y2 = np.sin(np.pi*x*2)
#绘制第一条蓝色--线,用横轴x纵轴y1得到sin曲线,命名sin(pi*x)
plt.plot(x, y1, 'b--', label='sin(pi*x)')
#纵轴命名y1 value
plt.ylabel('y1 value')
#绘制第二条红色--线,用横轴x纵轴y2得到sin曲线,命名sin(pi*2x)
plt.plot(x, y2, 'r--', label='sin(pi*2x)')
#纵轴命名y2 value
plt.ylabel('y2 value')
#横轴命名x value
plt.xlabel('x value')
#图形命名this is x-y value
plt.title('this is x-y value')
#显示图例
plt.legend()
'''
得到
<matplotlib.legend.Legend at 0x10f110860>
'''
#显示图形,以第二次命名的横轴名为准
plt.show()
得到
#子图,生成两行两列的子图一,(行,列,第几个图)可以不加逗号
plt.subplot(2, 2, 1)
#定义曲线
plt.plot(x, y1, 'b--')
#定义纵轴名称
plt.ylabel('y1')
#生成两行两列的子图二
plt.subplot(2, 2, 2)
#定义曲线
plt.plot(x, y2, 'r--')
#定义纵轴名称
plt.ylabel('y2')
#定义横轴名称
plt.xlabel('x')
#生成两行两列的子图三
plt.subplot(2, 2, 3)
#定义曲线
plt.plot(x, y1, 'r*')
#生成两行两列的子图四
plt.subplot(2, 2, 4)
#定义曲线
plt.plot(x, y1, 'b*')
'''
得到
[<matplotlib.lines.Line2D at 0x112eeb390>]
'''
#显示图像
plt.show()
得到
#子图,plt.subplots()得到一个元组
#a[0]代表一块画布画到哪,a[1]代表画图数值plt.subplots(行,列)
#figure此例代表四块空白画布
figure, ax = plt.subplots(2, 2)
#ax此例代表函数曲线,包含ax[0][0]/ax[0][1]/ax[1][0]/ax[1][1]
ax[0][0].plot(x, y1)
ax[0][1].plot(x, y2)
plt.show()
#四行放在一个只显示四个框,四个不放在一起出现很多
得到
Series Pandas绘图
#导入库
import numpy as np
import pandas as pd
from pandas import Series
import matplotlib.pyplot as plt
#创建两个Series,cumsum数据累加1,2,3☞1,3,6
s1 = Series(np.random.randn(1000)).cumsum()
s2 = Series(np.random.randn(1000)).cumsum()
#使用Series绘图
#kind曲线类型(默认line,bar柱状图),grid代表是否显示方格,label曲线名称命名S1,title图形名称命名This is Serie
s1.plot(kind='line',grid=True, label='S1', title='This is Series')
#曲线名称为S2
s2.plot(label='S2')
'''
得到
<matplotlib.axes._subplots.AxesSubplot at 0x110230eb8>
'''
#显示图例
plt.legend()
plt.show()
得到
#子图数据,两行一列
fig, ax = plt.subplots(2,1)
ax
'''
得到
array([<matplotlib.axes._subplots.AxesSubplot object at 0x10fd43eb8>,
<matplotlib.axes._subplots.AxesSubplot object at 0x10fb2cda0>], dtype=object)
'''
#第一张子图,是s1数据
ax[0].plot(s1)
'''
得到
[<matplotlib.lines.Line2D at 0x1109d1940>]
'''
#第二张子图,是s2数据
ax[1].plot(s2)
'''
得到
[<matplotlib.lines.Line2D at 0x10fb76668>]
'''
#图形显示
plt.show()
得到
#子图可以带定义
fig, ax = plt.subplots(2,1)
#1到10的切片数据
s1[0:10].plot(ax=ax[0], label='S1', kind='bar')
s2.plot(ax=ax[1], label='S2')
'''
得到
<matplotlib.axes._subplots.AxesSubplot at 0x11176b438>
'''
plt.show()
得到
DataFrame Pandas绘图
#导入库
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas import Series, DataFrame
#定义DataFrame
df = DataFrame(
np.random.randint(1,10,40).reshape(10,4),
columns=['A','B','C','D']
)
#绘制图形,柱状图
df.plot(kind='bar')
'''
得到
<matplotlib.axes._subplots.AxesSubplot at 0x10ec8af28>
'''
plt.show()
得到
#绘制横柱状图
df.plot(kind='barh')
plt.show()
得到
#stacked代表堆叠
df.plot(kind='bar', stacked=True)
'''
得到
<matplotlib.axes._subplots.AxesSubplot at 0x10f0b4278>
'''
plt.show()
得到
#kind='area'是填充线形图
df.plot(kind='area')
'''
得到
<matplotlib.axes._subplots.AxesSubplot at 0x10f2ceef0>
'''
plt.show()
得到
#按行画图,iloc第几行取值
a = df.iloc[5]
#是一个Series
type(a)
'''
得到
pandas.core.series.Series
'''
df.iloc[5].plot()
'''
得到
<matplotlib.axes._subplots.AxesSubplot at 0x10f292588>
'''
plt.show()
得到
a
'''
得到
A 9
B 2
C 3
D 2
Name: 5, dtype: int64
'''
#所有的Index绘图
for i in df.index:
df.iloc[i].plot(label=str(i))
plt.legend()
plt.show()
得到
#绘制A列图
df['A'].plot()
'''
得到
<matplotlib.axes._subplots.AxesSubplot at 0x10f2ff8d0>
'''
plt.show()
得到
#转置绘图
df.T.plot()
plt.show()
得到
直方图和密度图
#导入库
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas import Series, DataFrame
#直方图代表数据的分布
#创建一个Series
s = Series(np.random.randn(1000))
#绘制直方图,参数rwidth宽度
plt.hist(s, rwidth=0.9)
'''
得到
(array([ 4., 22., 65., 148., 228., 245., 160., 84., 37., 7.]),
array([-3.30986998, -2.67775932, -2.04564866, -1.413538 , -0.78142734,
-0.14931668, 0.48279398, 1.11490464, 1.74701531, 2.37912597,
3.01123663]),
<a list of 10 Patch objects>)
'''
plt.show()
得到
#另一个例子
a = np.arange(10)
'''
得到
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
'''
#绘制直方图
plt.hist(a,rwidth=0.9)
plt.show()
得到
#另一个例子,re是一个元组
re = plt.hist(s, rwidth=0.9)
#长度3
len(re)
'''
得到
3
'''
#第一个是频率
re[0]
'''
得到
array([ 4., 22., 65., 148., 228., 245., 160., 84., 37., 7.])
'''
#第二个是取值间隔
re[1]
'''
得到
array([-3.30986998, -2.67775932, -2.04564866, -1.413538 , -0.78142734,
-0.14931668, 0.48279398, 1.11490464, 1.74701531, 2.37912597,
3.01123663])
'''
#第三个是图表
re[2]
'''
得到
<a list of 10 Patch objects>
'''
plt.show()
得到
#直方图的其它参数,rwidth宽度,bins取值数量默认10,color颜色
plt.hist(s, rwidth=0.9,bins=20, color='r')
plt.show()
得到
#密度图同直方图,代表出现的概率
#kind='kde'表示密度图
s.plot(kind='kde')
'''
得到
<matplotlib.axes._subplots.AxesSubplot at 0x117bf20b8>
'''
plt.show()
得到