import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0,10,100)
y = np.sin(x)
#默认绘制样式,plot第三个参数可以修改绘制的样式,第四个参数控制颜色color
#plt.plot(x,y)
plt.plot(x,y,'--',color='red')
plt.plot(x,np.cos(x))
#生产图片
fig = plt.figure()
plt.plot(x,y,'.')
fig.savefig('1.png');
#在不同的图片(2行1列)上绘制曲线
plt.subplot(2,1,1)
plt.plot(x,np.sin(x),'--')
plt.subplot(2,1,2)
plt.plot(x,np.cos(x),'--')
#添加图例
plt.plot(x,y,'--',label='sin(x)')
plt.plot(x,np.cos(x),label='cos(x)')
plt.legend()
#设置图例的位置
plt.plot(x,y,'--',label='sin(x)')
plt.plot(x,np.cos(x),label='cos(x)')
plt.legend(loc="upper right")
#plot定制的参数非常多
x = np.linspace(0,10,20)
y = np.sin(x)
plt.plot(x,y,'-p',color='orange',markersize=16,linewidth=4,markeredgecolor='gray',markeredgewidth=1)
#限制y轴的取值范围
plt.ylim(-0.5,1.2)
#绘制散点图
x = np.linspace(0,10,20)
y = np.sin(x)
plt.scatter(x,y,s=20,c='blue')
#随机生产散点图
#设置样式-经典样式
plt.style.use('classic')
x = np.random.random(100)
y = np.random.random(100)
colors = np.random.rand(100)
sizes = 1000 * np.random.random(100)
plt.scatter(x,y,c = colors ,s=sizes,alpha=0.4)
#绘制颜色条
plt.colorbar()
import pandas as pd
#pandas本身自带绘图
#线性图
data = pd.DataFrame(np.random.rand(100,4).cumsum(0),columns=['A','B','C','D'])
data.plot()
#柱状图
data = pd.DataFrame(np.random.rand(100,4).cumsum(0),columns=['A','B','C','D'])
#data.plot.bar()
#等同于
data.plot(kind='bar')
#直方图
data = pd.DataFrame(np.random.rand(100,4),columns=['A','B','C','D'])
data.hist()
plt.show()
aaa