python数据分析06--matplotlib绘图

1 matplotlib

# 导包
import numpy as np 
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

# %matplotlib具体作用是当你调用matplotlib.pyplot的绘图函数plot()进行绘图的时候,或者生成一个figure画布的时候,可以直接在你的python console里面生成图像。

1.1 plt.plot( ) 绘制线性图

# 直线,两个参数x和y
x = np.array([1,2,3,4,5])
y = 2*x+1
plt.plot(x,y)

# 曲线
x = x
y = x**3
plt.plot(x,y)

# 多条线
plt.plot(x,y,x**2,y)
plt.plot(x=2,y+2)

# 设置坐标比例,画布小了
plt.figure(figsize=(1,2))
plt.plot(x,y)

# 设置线段的标识,label
plt.plot(x,y,label='temp')	# 一条线叫temp
plt.plot(x+1,y-1,label='dist')	# 一条叫dist
plt.lefend()	# loc=5,ncol=2	,标识的位置,可以调整

# 设置轴向的标识
plt.plot(x,y)
plt.xlabel('temp')	# x轴名称
plt.ylabel('dist')	# y轴名称
plt.title('temp&dist')	# 标题title

# 图片保存,分三步
fig = plt.figure()

plt.plot(x,y)
plt.xlabel('temp')
plt.ylabel('dist')
plt.title('temp&dist')

fig.savefig('路径名称')

# 其他样式的指令,比如调整线颜色c,线格式linstyle
plt.plot(x,y,c='red',alpha=0.7,linestyle=':')

plt.bar( ) 柱状图

plt.bar(索引,数据值,条形宽度)

plt.hist( ) 直方图,只需一组参数

密度图

x = [1,2,2,3,4,5]
plt.hist(x, bins=, normed=, color=, orientation)

# bins:可以是bin数量的整数值,默认为10。
# normed:如果为True,直方图的值将进行归一化处理,形成概率密度;默认为false。
# color:指定直方图颜色。如果指定了多个数据集合,例如DataFrame对象,颜色序列将会设置为相同的顺序。
# orientation:通过设置orientation为horizontal创建水平直方图。默认值为vertical。

plt.pie( ) 饼图,只需一组参数

涉及到展示部分与整体的比例,用饼图;展示各部分的大小,用条形图。

plt.pie([0.2,0.4,0.1])	# 不完整饼图

arr = [11,22,33,44]
plt.pie(arr,labels=['a','b','c','d'])	# 用abcd为各部分命名
plt.pie(arr,labels=['a','b','c','d'],labeldistance=0.3)	# abcd距离圆心0.3
plt.pie(arr,labels=['a','b','c','d'],labeldistance=0.3,autopct='%.6f%%')	# autopct参数,显示各部分所占比例在名称后面
plt.pie(arr,labels=['a','b','c','d'],labeldistance=0.3,shadow=True,explode=[0.2,0.3,0.2,0.4])	# 效果分裂

plt.scatter( ) 散点图

展现变化趋势

x = np.random.randint(0,100,size=(100,))
y = np.random.randint(0,100,size=(100,))
plt.scatter(x,y,c='r')	# c颜色

boxplot( ) 箱型图

显示一组数据分散情况的统计图,Q1最小值、Q2下四分位数(有小到大25%)、Q3中位数、Q4、Q5最大值
可以检测异常值

# subplot绘制子图,放到大表中,下面完成了图的排列
# 2行2列排布,位于第一个位置
ax1 = plt.subplot(2,2,1)
ax1.plot(x,y)

ax2 = plt.subplot(2,2,2)
ax2.plot(x,y)

ax3 = plt.subplot(2,2,3)
ax3.plot(x,y)

ax4 = plt.subplot(2,2,4)
ax4.plot(x,y)

# 思考:如果是3个图或者5个图,怎么实现绘制?
all_data=[np.random.normal(0,std,100) for std in range(1,4)]	# 
figure,axes=plt.subplots() #得到画板、轴
axes.boxplot(all_data,patch_artist=True) #描点上色
plt.show() #展示

# 2
d = [1,1,1,2,3,4,4,5,5,5,5,555,666,888,3]
a = plt.boxplot(d)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值