数据可视化——matplotlib
散点图
散点图可以用来判断数据的相关性
import numpy as np
import matplotlib.pyplot as plt
height=[161,170,182,175,173,165]
weight=[50,58,80,70,69,55]
# s代表点的大小,c代表颜色, marker代表点的形状, alpha代表透明度
plt.scatter(height,weight, s = 20, c = 'b', marker = 'o', alpha = 0.5)
plt.show()
折线图
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
x=np.linspace(-10,10,100)
y=x**2
plt.plot(x,y,linestyle = '-', color = 'green', marker = '<')
plt.show()
# plot_date函数可以自动识别date数据并将其转换成相应的时间日期
plt.plot_date(date,close)
plt.show()
条形图
import numpy as np
import matplotlib.pyplot as plt
y=[20,10,30,25,15]
index = np.arange(5)
p1 = plt.bar(index, height=y,width=0.5,bottom=100,color='red')
plt.show()
# orientation = 'horizontal'意味着画水平的条形图,width, index等需要灵活变换
p2 = plt.bar(bottom=index, width=y,height=0.5,orientation='horizontal')
plt.show()
# barh也是画水平条形图
p3=plt.barh(bottom=index,width=y,height=0.5)
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')
# index+bar_width画两条并列的条形图
plt.bar(index+bar_width,sales_SH,bar_width,color='r')
plt.show()
# bottom=sales_BJ画出层叠图
plt.bar(index,sales_BJ,bar_width,color='b')
plt.bar(index,sales_SH,bar_width,color='r',bottom=sales_BJ)
plt.show()
直方图
import numpy as np
import matplotlib.pyplot as plt
mu = 100 # mean of distribution
sigma = 20 # standard deviation of distribution
x = mu + sigma * np.random.randn