matplotlib绘图工具简介

matplotlib绘图工具简介

Matplotlib是一个综合库,用于在Python中创建静态,动画和交互式可视化。我们可以将通过numpy分析得到的数据结果通过matplotlib描绘出来.

配置画布

画布中图形具有的字体颜色,背景颜色等可以在会图前,设置一些初始值,后面只需要对部分属性进行一些微调.

import numpy as np
import matplotlib.pyplot as plt
import pylab

plt.rcParams['font.sans-serif'] = 'SimHei' # 配置画布字体颜色
plt.rcParams['axes.unicode_minus'] = False # 允许显示特殊字符
plt.rcParams['axes.facecolor'] = 'black' # 子图形背景颜色
plt.rcParams['ytick.color'] = 'red' # 子图形y轴字体颜色
plt.rcParams['xtick.color'] = 'red' # 画布x轴颜色
plt.rcParams['axes.edgecolor'] = "green" # 子图形边框颜色

散点图

散点图是指在回归分析中,数据点在直角坐标系平面上的分布图,散点图表示因变量随自变量而变化的大致趋势,据此可以选择合适的函数[对数据点进行拟合.

plt.figure(figsize=(8, 8),facecolor='black')# 构建画布
x = np.arange(0, 12)
y = 2x + 1
plt.scater(x, y, c='black') # 描绘散点图 c:散点的颜色属性
# 设置特殊的颜色格式可以用下面代码
plt.scater(x, y, c=y, cmap='rainbow')
# 特殊颜色样式如下:
# "Accent', 'Accent_r', 'Blues', 'Blues_r', 'BrBG', 'BrBG_r', 'BuGn', " \
# "'BuGn_r', 'BuPu', 'BuPu_r', 'CMRmap', 'CMRmap_r', 'Dark2', 'Dark2_r', " \
# "'GnBu', 'GnBu_r', 'Greens', 'Greens_r', 'Greys', 'Greys_r', 'OrRd', 'OrRd_r'," \
# " 'Oranges', 'Oranges_r', 'PRGn', 'PRGn_r', 'Paired', 'Paired_r', " \
# "'Pastel1', 'Pastel1_r', 'Pastel2', 'Pastel2_r', 'PiYG', 'PiYG_r'," \
# " 'PuBu', 'PuBuGn', 'PuBuGn_r', 'PuBu_r', 'PuOr', 'PuOr_r', 'PuRd'," \
# " 'PuRd_r', 'Purples', 'Purples_r', 'RdBu', 'RdBu_r', 'RdGy', 'RdGy_r', " \
# "'RdPu', 'RdPu_r', 'RdYlBu', 'RdYlBu_r', 'RdYlGn', 'RdYlGn_r', 'Reds', 'Reds_r', " \
# "'Set1', 'Set1_r', 'Set2', 'Set2_r', 'Set3', 'Set3_r', 'Spectral', 'Spectral_r', 'Wistia'," \
# " 'Wistia_r', 'YlGn', 'YlGnBu', 'YlGnBu_r', 'YlGn_r', 'YlOrBr', 'YlOrBr_r', 'YlOrRd', 'YlOrRd_r'," \
# " 'afmhot', 'afmhot_r', 'autumn', 'autumn_r', 'binary', 'binary_r', 'bone', 'bone_r', " \
# "'brg', 'brg_r', 'bwr', 'bwr_r', 'cividis', 'cividis_r', 'cool', 'cool_r', 'coolwarm', " \
# "'coolwarm_r', 'copper', 'copper_r', 'cubehelix', 'cubehelix_r', 'flag', 'flag_r', 'gist_earth', " \
# "'gist_earth_r', 'gist_gray', 'gist_gray_r', 'gist_heat', 'gist_heat_r', 'gist_ncar', 'gist_ncar_r', " \
# "'gist_rainbow', 'gist_rainbow_r', 'gist_stern', 'gist_stern_r', 'gist_yarg', 'gist_yarg_r', 'gnuplot', " \
# "'gnuplot2', 'gnuplot2_r', 'gnuplot_r', 'gray', 'gray_r', 'hot', 'hot_r', 'hsv', 'hsv_r', " \
# "'inferno', 'inferno_r', 'jet', 'jet_r', 'magma', 'magma_r', 'nipy_spectral', 'nipy_spectral_r', " \
# "'ocean', 'ocean_r', 'pink', 'pink_r', 'plasma', 'plasma_r', 'prism', 'prism_r', 'rainbow'," \
# " 'rainbow_r', 'seismic', 'seismic_r', 'spring', 'spring_r', 'summer', 'summer_r', 'tab10', 'tab10_r', " \
# "'tab20', 'tab20_r', 'tab20b', 'tab20b_r', 'tab20c', 'tab20c_r', 'terrain', 'terrain_r', " \
# "'turbo', 'turbo_r', 'twilight', 'twilight_r', 'twilight_shifted', 'twilight_shifted_r', " \
# "'viridis', 'viridis_r', 'winter', 'winter_r"
plt.xlabel('x轴名')
plt.ylabel('y轴名')
plt.title('标题')
plt.show() # 展示图形

柱状图

柱状图是直方图的前身,柱状图适合做多个条件下指标的比较.

x = [1, 2, 3, 4]
y = [100, 200, 300, 400]
plt.bar(x,y) # 绘制柱状图
plt.xticks(x,['条件1', '条件2', '条件3', '条件4'])# 将x位置换成对应条件
for x, y in zip(x, y): # 将每一个柱子的最大值添加到柱子顶部
    plt.text(x, y+2 ,y)

直方图

直方图将柱状图进行了拓展,是将条件从一个具体的值,转变为一个条件范围,想轴的宽度表示条件范围大小.

x = np.random.randn(1000000)
plt.hist(x, bins=1000, color='') # 绘制直方图.bins是将大范围切成的小范围数.
plt.show()

折线图

折线图可以很好的反映一个指标在一个条件范围内的变化趋势.折线图不同于散点图的地方在于,折线图的所有点都被一条线串联起来了.描画清楚明确的折线图能够很好的反映指标的变化规律.

x = np.arange(1, 100)
y = 2*x +1
plt.plot(x,y) 3 # 绘制折线图
plt.xticks(x,x) # 设置想轴显示内容
plt.show()

饼图

man_num = data1[:-2, 2].sum()
women_num = data1[:-2, 3].sum()
num_list = (man_num, women_num) # 展示的数据列表
plt.pie(num_list, autopct='%1.0f%%') # 绘制饼状图,其中autopct设置的是饼状图上显示的内容.
plt.legend(['男生', '女生'], facecolor='gray') # 设置图例
plt.title('男女人数占比图', color='white') # 设置标题

雷达图

x = np.linspace(0, 2 * np.pi, 10, endpoint=False) # 等分角度,等同于设置极坐标的坐标数
x = np.concatenate((x, [x[0]])) # 闭合图形
y1 = [2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
plt.polar(x, y1) # 绘画饼图
plt.xticks(x) # 设置轴显示的内容
plt.fill(x, y1, 'y', alpha=0.2) # 填充图形
plt.show()
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值