【Python】Matplotlib绘图01_pyplot基本图表绘制:折线图、直方图、散点图、箱线图、堆积图等

本人依据上课学习内容,将matplotlib内容浓缩为代码块
以下是第一部分的学习内容:
1.柱形图 bar
2.条形图 barh
3.折线图 plot
4.堆积面积图 stackplot
5.直方图 hist
6.饼图/圆环图 pie
7.散点图/气泡图 scatter
8.箱线图 boxplot
9.雷达图 polar
10.误差棒图 errorbar

本文于2021/12/13首发于csdn,如有错误和不足请指出。

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# 如果不输入两个变量,会默认将一个变量当做y轴,
# 并自动生成与该变量数据长度一致的递增x轴数列

plt.figure()
# 显示中文
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False


'''1.柱形图 bar'''
# (1)一组柱形的柱形图
x = np.arange(5)
y1 = np.array([10, 8, 7, 11, 13])
# 柱形的宽度与标签
bar_width = 0.3
label = ['a', 'b', 'c', 'd', 'e']
# 绘制柱形图
plt.bar(x, y1, tick_label=label, color='g', width=bar_width)
plt.show()

# (2)两组柱形的柱形图 + width
y2 = np.array([9, 6, 5, 10, 12])
# 根据多组数据绘制柱形图
plt.bar(x, y1, tick_label=label, width=bar_width)
plt.bar(x + bar_width, y2, width=bar_width)
plt.show()

# (3)堆积柱形图 bottom
plt.bar(x, y1, tick_label=label, width=bar_width)
plt.bar(x, y2, bottom=y1, width=bar_width)
plt.show()

# (4)有误差棒的柱形图 yerr
# 偏差数据,数据会在这个数据内+-
error = [2, 1, 2.5, 2, 1.5]
# 绘制带有误差棒的柱形图
plt.bar(x, y1, tick_label=label, width=bar_width)
plt.bar(x, y1, bottom=y1, width=bar_width, yerr=error)
plt.show()

'''2.条形图 barh'''
# (1)一组条形的条形图
y = np.arange(5)
x1 = np.array([10, 8, 7, 11, 13])
# 条形的高度(纵宽)与标签
bar_height = 0.3
label = ['a', 'b', 'c', 'd', 'e']
# 绘制条形图 y控制y轴,x1是x轴数据,标签是y轴标签
plt.barh(y, x1, tick_label=label, height=bar_height)
plt.show()

# (2)绘制有两组条形的条形图 height
x2 = np.array([9, 6, 5, 10, 12])
plt.barh(y, x1, tick_label=label, height=bar_height)
plt.barh(y + bar_height, x2, height=bar_height)
plt.show()

# (3)绘制堆积条形图 left
plt.barh(y, x1, tick_label=label, height=bar_height)
plt.barh(y, x2, left=x1, height=bar_height)
plt.show()

# (4)有误差棒的条形图 xerr
plt.barh(y, x1, tick_label=label, height=bar_height)
plt.barh(y, x2, left=x1, height=bar_height, xerr=error)
plt.show()

'''3.折线图 plot'''
x = np.arange(5)
y1 = np.array([10, 8, 7, 11, 13])
y2 = np.array([9, 6, 5, 10, 12])
plt.plot(x, y1, label=label)
plt.plot(x, y2)
# plt.plot(x, y1, x, y2)
plt.show()

'''4.堆积面积图 stackplot'''
# (1)绘制有三个填充区域堆叠的堆积面积图
x = np.arange(6)
y1 = np.array([1, 4, 3, 5, 6, 7])
y2 = np.array([[1, 3, 4, 2, 7, 6]])
# 可以输入二维数组
# y2 = np.array([[1, 3, 4, 2, 7, 6], [3, 4, 3, 6, 5, 5]])
y3 = np.array([3, 4, 3, 6, 5, 5])
# 标签
labels = ['a', 'b', 'c']
# 绘制堆积面积图
plt.stackplot(x, y1, y2, y3, labels=labels)
plt.show()

'''5.直方图 hist'''
# 准备50个随机测试数据
scores = np.random.randint(0, 100, 50)
# x为x轴连续数据,y相当于x轴数据的频数/频率统计
# bins矩形条个数(分组数)
# range数据范围,默认为(x.min,x.max)
# cumulative是否累积频数/频率
# histtype直方图类型:
# 'bar'为默认值,传统的直方图;'barstacked'堆积直方图;
# 'step'未填充的线条直方图;'stepfilled'填充的线条直方图。
plt.hist(scores, bins=8, histtype='step')
plt.show()

'''6.饼图/圆环图 pie'''
data = np.array([20, 50, 10, 15, 30, 55])
pie_labels = np.array(['A', 'B', 'C', 'D', 'E', 'F'])
explode = [0.1, 0, 0, 0, 0, 0]
# explode:表示扇形或楔形离开圆心的距离。每个扇形距离的列表,需要写全
# labels:表示扇形或楔形对应的标签文本
# autopct:表示控制扇形或楔形的数值显示的字符串,可通过格式字符串指定小数点后的位数,%3.1表示小数点后面精确到1位,总长度3位数,包括小数点
# radius:表示扇形或楔形围成的圆形半径
plt.pie(data, radius=1.5, explode=explode,
        labels=pie_labels, autopct='%3.1f%%')

# wedgeprops:可用来画圆环图,表示控制扇形或楔形属性的字典。例如,通过wedgeprops={'width':0.7}将楔形的宽度设为0.7
# pctdistance:表示扇形或楔形对应的数值标签距离圆心的比例,默认为0.6
# shadow:表示是否显示阴影。
# startangle:表示起始绘制角度,默认从 x轴的正方向 逆时针绘制。
plt.pie(data, radius=1.5, wedgeprops={'width': 0.7},
           labels=pie_labels, autopct='%3.1f%%',
           pctdistance=0.75, startangle=90, shadow=True)
plt.show()

'''7.散点图/气泡图 scatter'''
# 散点图
num = 50
x = np.random.rand(num)
y = np.random.rand(num)
# c散点颜色,marker散点样式
plt.scatter(x, y, c='b', marker='*')
plt.show()

# 气泡图
# 随机生成num个数,area是一个ndarray
# area = (30 * np.random.rand(num))**2
# s散点大小,可以自定也可以根据y来改变,alpha透明度
plt.scatter(x, y, s=y*100, alpha=0.7)
plt.show()

'''8.箱线图 boxplot'''
data = np.random.randn(100)
# widths:表示箱体的宽度,默认为0.5。
# meanline:是否用横跨箱体的线条标出中位数,默认不使用。
# showcaps:表示是否显示箱体顶部和底部的横线,默认显示。
# showbox:表示是否显示箱形图的箱体,默认显示。
# showfliers:表示是否显示异常值,默认显示。
# patch_artist:表示是否填充箱体的颜色,默认不填充。

# 不显示异常值的箱线图showfliers=False
plt.boxplot(data, meanline=True, widths=0.3, patch_artist=True,
            showfliers=False)

# sym:表示异常值对应的符号,默认为空心圆圈。
# vert:表示是否将箱形图垂直摆放,默认为垂直摆放。
# whis:表示箱形图上下限与上下四分位的距离,默认为1.5倍的四分位差。
# positions:表示箱体的位置。
# labels:表示箱形图的标签。

# 横放的箱线图vert=False
plt.boxplot(data, labels=['2018年'], meanline=True, patch_artist=True,
            vert=False)
plt.show()

'''9.雷达图 polar'''
# 霍兰德职业兴趣测试
# 维度
dim_num = 6
# 六个人的样本(六维数据)
data = np.array([[0.40, 0.32, 0.35, 0.30, 0.30, 0.88],
                 [0.85, 0.35, 0.30, 0.40, 0.40, 0.30],
                 [0.43, 0.89, 0.30, 0.28, 0.22, 0.30],
                 [0.30, 0.25, 0.48, 0.85, 0.45, 0.40],
                 [0.20, 0.38, 0.87, 0.45, 0.32, 0.28],
                 [0.34, 0.31, 0.38, 0.40, 0.92, 0.28]])
# 设置六维射线与极径的夹角。
angles = np.linspace(0, 2 * np.pi, dim_num, endpoint=False)
# 因为是一个圆,最后要绕回来,所以合并上起始位置
angles = np.concatenate((angles, [angles[0]]))
data = np.concatenate((data, [data[0]]))
# 维度标签
radar_labels = ['研究型(I)', '艺术型(A)', '社会型(S)',
                '企业型(E)', '传统型(C)', '现实型(R)']
radar_labels = np.concatenate((radar_labels, [radar_labels[0]]))

# 绘制雷达图
plt.polar(angles, data)

# 设置极坐标的标签
plt.thetagrids(angles * 180/np.pi, labels=radar_labels)
# 填充多边形
plt.fill(angles, data, alpha=0.25)
plt.show()

'''
10.误差棒图 errorbar,这种算作是单独画误差棒,
没有基底图如柱形图等,会自动用plot连接
'''
x = np.arange(5)
y = (25, 32, 34, 20, 25)
y_offset = (3, 5, 2, 3, 3)
# capsize:表示误差棒边界横杆的大小
# capthick:表示误差棒边界横杆的厚度。
plt.errorbar(x, y, yerr=y_offset, capsize=3, capthick=2)
plt.show()
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值