导入包
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
import matplotlib.pyplot as plt
%matplotlib inline
s1=Series(data=[2,4,6,8,0])
s1
0 2
1 4
2 6
3 8
4 0
dtype: int64
折线图,x轴为行索引,y轴为数值
s1.plot()
dic={
'A':[1,2,3,4,5],
'B':[3,4,5,6,7],
'C':[5,8,4,6,2]
}
df=DataFrame(data=dic)
df
A B C
0 1 3 5
1 2 4 8
2 3 5 4
3 4 6 6
4 5 7 2
df.plot()
s1
0 2
1 4
2 6
3 8
4 0
dtype: int64
柱状图
s1.plot(kind='bar')
df.plot(kind='bar')
df
A B C
0 1 3 5
1 2 4 8
2 3 5 4
3 4 6 6
4 5 7 2
直方图
df.plot(kind='hist')
生成点
x=[1,2,3,4,5]
y=[6,7,8,9,0]
plt.plot(x,y,'bo')
使用arange函数生成抛物线y=x**2
q=np.arange(-2,2,0.01)
w=q**2
plt.plot(q,w)
绘制正弦曲线y=sin(x)
x=np.linspace(-2,2,100)
y=np.sin(x)
plt.plot(x,y)
plt.plot(x,y)
plt.plot(x,y+3)
将多个曲线图放到一个table表中
a1=plt.subplot(2,2,1)
a1.plot(x,y)
a2=plt.subplot(2,2,2)
a2.plot(x+1,y+1)
a3=plt.subplot(2,2,3)
a3.plot(x+1,y+1)
a4=plt.subplot(2,2,4)
a4.plot(x+1,y+1)
设置网格
plt.plot(x,y)
plt.grid(color='r')
plt.axis()或者plt.xlim(),plt.ylim()
plt.plot(x,y)
plt.axis([-4,4,-2,2])
[-4, 4, -2, 2]
关闭坐标轴
plt.plot(x,y)
plt.axis([-4,4,-2,2])
plt.axis('off')
(-4.0, 4.0, -2.0, 2.0)
设置画布比例
plt.figure(figsize=(5,5))
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x1d1ff6c7588>]
画圆
a=np.linspace(-1,1,100)
b=(1-a**2)**0.5
plt.figure(figsize=(8,8))
plt.plot(a,b)
plt.plot(a,-b)
设置坐标轴名称,标题
plt.xlabel('aaa')
plt.ylabel('bbb')
plt.title('hello')
lengend的参数
q=np.random.randint(1,5,5)
w=np.sin(x)
e=np.random.randint(1,5,5)
r=np.cos(e)
plt.plot(x,y,x+1,y+1)
plt.plot(x,y)
plt.plot(x+1,y+1)
plt.legend(['x','x+1'])
ncol=表示图例几列
plt.plot(x,y)
plt.plot(x+1,y+1)
plt.legend(['x','x+1'],ncol=2)
保存图片
plt.plot(x,y)
fig=plt.figure()
fig.savefig('111.jpg')
设置线性的颜色
plt.plot(x,y,c='r')
设置透明度
plt.plot(x,y,c='r',alpha=0.2)
设置背景色
ax=plt.subplot(111)
ax.plot(x,y,c='r')
ax.set_facecolor('g')
设置线型
plt.plot(x,y,ls='')
设置线宽
plt.plot(x,y,ls='steps',lw=3)
设置点型
plt.plot(x,y,marker='3')
marker前后景色,markerfacecolor=”,markere
多参连用,只能设置颜色,点型,线型,顺序可变,
plt.plot(x,y,x+1,y+2)
修改刻度
plt.plot(x,y)
D图
直方图,密度图
x=[1,2,3,4,5,2,1]
plt.hist(x,bins=10,color='r')
条形图,柱状图(wight,)横向barh
x=[1,2,3,4,5]
y=[1,2,3,4,5]
plt.barh(x,y)
未占满饼图
a=[0.5,0.2,0.1]
plt.pie(a)
b=[5,1,8,4,9,6]
plt.pie(b)
占满饼图
b=[5,1,8,4,9,6]
plt.pie(b)
设置标签
b=[5,1,8,4,9,6]
plt.pie(b,labels=['a','b','c','d','e','f'])
设置标签
b=[5,1,8,4,9,6]
plt.pie(b,labels=['a','b','c','d','e','f'])
设置标签与圆心的距离
b=[5,1,8,4,9,6]
plt.pie(b,labels=['a','b','c','d','e','f'],labeldistance=0.5)
设置每个标签所占比列,2表示保留两位小数,f表示浮点型
b=[5,1,8,4,9,6]
plt.pie(b,labels=['a','b','c','d','e','f'],autopct='%.2f%%')
是否绘制阴影
b=[5,1,8,4,9,6]
plt.pie(b,labels=['a','b','c','d','e','f'],autopct='%.2f%%',shadow=True)
散点图
x=DataFrame(data=np.random.randint(-np.pi,np.pi,(1000,2)))
x.head()
0 1
0 2 -2
1 -3 0
2 -2 2
3 -1 -3
4 -3 0
plt.scatter(x[0],x[1])
x=np.random.randn(1000)
y=np.random.randn(1000)
plt.scatter(x,y)
设置形状
plt.scatter(x,y,marker='d')
设置随机颜色值
plt.scatter(x,y,c='rbgy')