Matplotlib | 高阶绘图案例【2】- 读哪些专业最容易后悔?

大家好,我是 👉【Python当打之年(点击跳转)】

本期是 Matplotlib高阶绘图案例系列 的第 2 期, Matplotlib系列和Pyecharts系列都会不间断更新,希望对大家有所帮助,如有疑问或者需要改进的地方可以联系小编。

上期Matplotlib | 高阶绘图案例【1】

先看看效果:

在这里插入图片描述

🏳️‍🌈 1. 导入模块

import numpy as np
import matplotlib.pyplot as plt
import funcy
import warnings
warnings.filterwarnings('ignore')
plt.rcParams['font.family'] = ['Microsoft YaHei']

示例数据:

majors = ['营销', '金融', '心理学', '新闻学', '电视', '日语', '汉语言', '物理', '机械', '文学', '生物', '计算机', '化学', '数学', '设计', '英语', '会计', '教育', '工程', '医学']
datas = [18.7, 21.6, 22.9, 23.5, 24.8, 26.2, 27.1, 27.1, 29.6, 31.3, 32.6, 33.5, 36.3, 40.8, 54.3, 55.8, 70.1, 70.3, 70.7, 100]

🏳️‍🌈 2. 绘图

2.1 绘制图布,设置极坐标系

fig, ax = plt.subplots(figsize=(12, 12),dpi=100)
ax1 = fig.add_axes([0.22, 0.2, 0.6, 0.6], polar=True)

在这里插入图片描述

2.2 绘制上半扇形区域

theta_group = np.linspace(0, 1, 21)*np.pi
# 绘制上半扇形区域
for idx, group in enumerate(funcy.pairwise(theta_group)):
    ax1.fill_between(group, 1.4, 4, facecolor='#fafbff' if idx % 2 == 0 else '#e3effd')

在这里插入图片描述

2.3 绘制上半扇形区域中间虚线

# 绘制虚线
for idx, group in enumerate(funcy.pairwise(theta_group)):
    theta = (group[0] + group[1]) / 2
    ax1.plot([theta, theta], [1.4, 3.5], linestyle='dashed', color='#9fa0a0', linewidth=0.25)

在这里插入图片描述

2.4 绘制柱状图

for idx, group in enumerate(funcy.pairwise(theta_group)):
    theta = (group[0] + group[1]) / 2
    ax1.plot([theta, theta], [1.4, 3.5], linestyle='dashed', color='#9fa0a0', linewidth=0.25)
    # 柱状图
    ax1.bar(theta, 0.025*datas[idx], width=[np.pi / 21], bottom=1.4,
            facecolor='#6785f2' if idx % 2 == 0 else '#7171fe', edgecolor='white', linewidth=0.1, alpha=0.95, zorder=9
           )

在这里插入图片描述

2.5 绘制扇形区域白色边界

# 绘制扇形区域白色边界
for theta in theta_group:
    ax1.plot([theta, theta], [1, 4], color='w', linewidth=0.5)

在这里插入图片描述

2.6 添加文本指数

# 文本
for idx, group in enumerate(funcy.pairwise(theta_group)): # 文字角度
    angle = ((group[0] + group[1]) * 0.5 / np.pi) * 180 - (0 if idx < (len(majors) / 2) else 180)
    # 专业
    ax1.annotate(majors[idx], xy=[(group[0]+group[1]) / 2, 4.4], va='center', ha='center', zorder=10, rotation=angle,fontsize=10)
    # 指数
    ax1.annotate(datas[idx],
                 xy=[(group[0]+group[1]) / 2, 3.79],
                 va='center', ha='center', zorder=10,
                 rotation=angle,
                 fontsize=10)

在这里插入图片描述

2.7 绘制外圈虚线

# 绘制外圈虚线
ax1.plot([0, 0], [1.4, 4.8], linestyle='--', color='#B0BEC5', linewidth=0.25)
ax1.plot([np.pi, np.pi], [1.4, 4.8], linestyle='--', color='#B0BEC5', linewidth=0.25)
ax1.plot(np.linspace(0, 1, 1000)*np.pi, [4.8]*1000, linestyle='dashed', color='#B0BEC5', linewidth=0.75)

在这里插入图片描述

2.8 添加标题

ax1.annotate('在网友看来\n读哪些专业', xy=[np.pi/2, 0.9],
             va='center',ha='center', zorder=10,
             fontsize=12,fontproperties='Microsoft Yahei', fontweight='bold'
            )

ax1.annotate('最容易后悔?', xy=[np.pi/2, 0.4],
             va='center',ha='center', ma='center',zorder=10,
             fontsize=16,color='#D32F2F', fontproperties='Microsoft Yahei', fontweight='bold',
            )

在这里插入图片描述

🏳️‍🌈 3. 在线运行地址

在线运行地址(全部代码):
https://www.heywhale.com/mw/project/64e84ba23852baaea3bdaa07

🏳️‍🌈 4. 可视化项目源码+数据

点击跳转:【全部可视化项目源码+数据】


以上就是本期为大家整理的全部内容了,赶快练习起来吧,原创不易,喜欢的朋友可以点赞、收藏也可以分享注明出处)让更多人知道。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Python当打之年

您的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值