python之matplotlib各种图表详细讲解

#相应包导入
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
%matplotlib inline
  1. 散点图(scatter) 对比两组数据之间的关系
  • s:标记大小
  • marker:标记样式
  • color:颜色
  • alpha:透明度(0,1)
height = [161,170,182,175,173,165]
weight = [50,58,80,70,69,55]
plt.scatter(height, weight,s=100, marker='^', c='blue', alpha=0.5)
  1. 折线图(plot)
  • color:颜色
  • linestyle:线型("-","–","-.")
  • linewidth:线宽
  • marker:标记样式
  • markerfacecolor:标记颜色
  • markersize:标记大小
x = np.linspace(-10, 10, 5)
y = x ** 2
plt.plot(x, y,c='red',linestyle='-.',linewidth=1,marker='o',markerfacecolor='blue')
plt.show()
  1. 条形图(bar)
  • width:长条形宽度(横向为height),默认值0.8
  • bottom:坐标原点y起始值
  • color:颜色
y = [20, 10, 30, 25, 15]
x = range(len(y))
bar_width = 10
bar_width = 10
plt.bar(x, y, width = 0.8,color='blue', bottom=100)
# 横向条形图
plt.barh(x, y, height=0.8)
  1. 直方图(hist)
  • bins:直方图的长条形数目,可选项,默认为10
  • normed:是否将得到的直方图向量归一化,显示频率
  • color:长条形的颜色
  • edgecolor:长条形边框颜色
  • alpha:透明度
mu = 100  # mean of distribution
sigma = 20  # standard deviation of distribution
x = mu + sigma * np.random.randn(2000)
plt.hist(x, bins=15,color='red',edgecolor='blue',normed=True)
# 两组数据的热点直方图
# x = np.random.randn(1000)+2
# y = np.random.randn(1000)+3
# plt.hist2d(x, y, bins=40)
  1. 饼图(pie)
  • size:各部分大小
  • explode:各部分突出
  • label:设置各部分标签
  • labeldistance:设置标签文本距圆心位置,1.1表示1.1倍半径
  • autopct:设置圆里面文本
  • shadow:设置是否有阴影
  • startangle:起始角度,默认从0开始逆时针旋转
  • pctdistance:设置圆内文本距圆心距离
  • l_test:圆内部文本
  • p_test:圆外部文本
labels = 'SH', 'BJ', 'SZ', 'GD'
size = [20, 10, 30, 25]
color = ['red','yellow','blue','green']
explode = (0, 0, 0.05, 0)
plt.axis(aspect=2)
plt.pie(size, explode=explode, labels=labels, colors=color, autopct='%.1f%%', shadow=True)
  1. 箱线图(boxplot)
  • sym:异常点形状
  • whis:上下四分位线
np.random.seed(10)
data = np.random.normal(size=10, loc=0.0, scale=1.0)
plt.boxplot(data, sym='o', whis=1.5)
# 同时显示多组数据的箱线图
data = np.random.normal(size=(100, 4), loc=0.0, scale=1.0)
labels = ['A','B','C','D']
plt.boxplot(data, labels=labels,sym='^')
  1. 子图(snbplot)
  • 221:2行2列第1个
x = np.arange(1, 100)
plt.subplot(221)
plt.plot(x, x)
plt.subplot(222)
plt.plot(x, -x)
plt.subplot(223)
plt.plot(x, x * x)
plt.subplot(224)
plt.plot(x, np.log(x))
  1. 多图(snbplot)
fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
ax1.plot([1, 2, 3], [3, 2, 1])
fig2 = plt.figure()
ax2 = fig2.add_subplot(111)
ax2.plot([1, 2, 3], [1, 2, 3])
  1. 网格(grid)
  • 设置网格显示
y = np.arange(1, 5)
plt.plot(y, y * 2)
plt.grid(True, color='g', linestyle='--', linewidth='2')
  1. 图例(legend)
  • loc 图例的位置
  • ncol 图例的列数
x = np.arange(1, 11, 1)
y = x * x
plt.plot(x, x * 2, label='Normal')
plt.plot(x, x * 3, label='Fast')
plt.plot(x, x * 4, label='Faster')
plt.legend(loc=0, ncol=3)
# 方法二
plt.plot(x, x * 2)
plt.plot(x, x * 3)
plt.plot(x, x * 4)
plt.legend(['Normal', 'Fast', 'Faster'])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值