Matplotlib 直方、散点、热图、饼图、误差图 (基础)

本文展示了如何使用Python的sklearn和matplotlib库对加州房价数据进行分析,包括绘制价格分布直方图、散点图、热力图、饼图以及带有误差线的图表
摘要由CSDN通过智能技术生成
from sklearn.datasets import fetch_california_housing
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
%matplotlib inline

housing = fetch_california_housing()
print(housing)

x_axis = housing.data
y_axis = housing.target

style.use('ggplot')
plt.figure(figsize=(7,7))
plt.hist(y_axis, bins=50)
plt.xlabel('price in 1000s USD')
plt.ylabel('number of houses')
plt.show()

 

# plot scatter plot
style.use('ggplot')
plt.figure(figsize=(7,7))

plt.scatter(housing.data[:,2],housing.target)
plt.ylabel('price in 1000s')
plt.xlabel('number of houses')
plt.show()

 

# heatmaps
import matplotlib.pyplot as plt
import seaborn as sns

%matplotlib inline

flightd = sns.load_dataset('flights')
flightd.head()

 

flightd = flightd.pivot('month','year','passengers')
flightd

 

sns.heatmap(flightd)

 

 

# piechart
import matplotlib.pyplot as plt
%matplotlib inline
jobd = ['40','20','17','8','5','10']
# for i in jobd:
    # print(type(i))
labels = ['IT', 'Finance', 'Marketing', 'Admin', 'HR', 'Operations']
# explode the ist slice 
explode = (0.05,0,0,0,0,0)
plt.pie(jobd, labels=labels, explode=explode)
plt.show()

# error bars
import matplotlib.pyplot as plt
x = range(5)
y = (25, 32, 34, 20, 25)
y_offset = (3, 5, 2, 3, 3)
plt.errorbar(x, y, yerr=y_offset, capsize=3, capthick=2)
plt.show()

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值