文章目录
- 🏳️🌈 1. 数据
- 🏳️🌈 2. 绘图
- 2.1 绘制南丁格尔玫瑰图
- 2.2 设置cloor_bar
- 2.3 添加文字
- 2.4 添加注释
- 2.5 添加标题
- 2.6 词云
- 🏳️🌈 3. 更多可视化项目源码+数据
大家好,我是 👉 【Python当打之年(点击跳转)】
卡塔尔世界杯八强已经产生:巴西、葡萄牙、摩洛哥、克罗地亚、法国、阿根廷、荷兰、英格兰 ,本期利用python分析一下网友对各队的支持程度,看看大家觉得今年世界杯哪个队最有可能夺冠 ,希望对大家有所帮助。
- 上半区:巴西、克罗地亚、阿根廷、荷兰
- 下半区:葡萄牙、摩洛哥、法国、英格兰
🏳️🌈 1. 数据
话题:
数据获取部分大家可以参考之前的文章,这里不再赘述。
🏳️🌈 2. 绘图
2.1 绘制南丁格尔玫瑰图
def get_pie(regions, region_count,size, colors):
num= len(size)
width = 2 * np.pi / num
rad = np.cumsum([width] * num)
plt.figure(figsize=(8, 8),dpi=200)
ax = plt.subplot(projection='polar')
ax.set_ylim(-2, np.ceil(max(size) + 1))
ax.set_theta_zero_location('N',-6.0)
ax.set_theta_direction(1)
ax.grid(False)
ax.spines['polar'].set_visible(False)
ax.set_yticks([])
ax.set_thetagrids([])
ax.bar(rad, size, width=width, alpha=1)
2.2 设置cloor_bar
可以自定义颜色:
colors = [(0.68359375, 0.02734375, 0.3203125),(0.78125, 0.05078125, 0.2578125),(0.875, 0.0390625, 0.1796875),(0.81640625, 0.06640625, 0.0625),
(0.8515625, 0.1484375, 0.08203125),(0.90625, 0.203125, 0.13671875),(0.89453125, 0.2890625, 0.0703125),(0.84375, 0.2421875, 0.03125),
(0.9140625, 0.26953125, 0.05078125),(0.85546875, 0.31640625, 0.125),(0.85546875, 0.3671875, 0.1171875),(0.94921875, 0.48046875, 0.28125),
(0.9375, 0.51171875, 0.1484375), (0.93359375, 0.59765625, 0.0625),(0.93359375, 0.62890625, 0.14453125),(0.86328125, 0.5859375, 0.15234375),
(0.86328125, 0.71875, 0.16015625), (0.86328125, 0.8203125, 0.16015625),(0.76171875, 0.8671875, 0.16015625),(0.53125, 0.85546875, 0.15625),
(0.4765625, 0.94140625, 0.0703125), (0.21484375, 0.91015625, 0.0625),(0.15234375, 0.88671875, 0.08203125),(0.11328125, 0.87890625, 0.19921875),
(0.11328125, 0.8125, 0.1796875), (0.1875, 0.76953125, 0.2109375),(0.2109375, 0.78125, 0.38671875),(0.1484375, 0.76953125, 0.30859375)
]
2.3 添加文字
font_dict = {'size':size[i]/2.5, 'color':'white','weight':'heavy','style':'normal'}
ax.text(rad[i], size[i]-1.2, f'{regions[i]}', rotation=rad[i] * 180 / np.pi -5,fontdict=font_dict,
rotation_mode='anchor', ha="center", va="top" )
font_dict = {'family':'Times New Roman', 'size':size[i]/1.5, 'color':'white','weight':'heavy','style':'normal'}
ax.text(rad[i], size[i]-3.8+i*0.15, f'{region_count[i]}', rotation=rad[i] * 180 / np.pi -5,fontdict=font_dict,
rotation_mode='anchor', ha="center", va="top" )
2.4 添加注释
axins1.text(x_pos, 0.96, '十六强', fontfamily='Microsoft YaHei', fontsize=6,color='#212121',fontweight='heavy',
horizontalalignment='left',verticalalignment='center', bbox=bbox_props)
axins1.text(x_pos, 0.89, s16, fontsize=5,color=s_color,fontweight='heavy', horizontalalignment='left',verticalalignment='center')
axins1.text(x_pos, 0.81, '八强', fontsize=6,color='#212121',fontweight='heavy',
horizontalalignment='left',verticalalignment='center', bbox=bbox_props)
axins1.text(x_pos, 0.765, s8, fontsize=5,color=s_color,fontweight='heavy', horizontalalignment='left',verticalalignment='center')
axins1.text(x_pos, 0.71, '预测四强', fontsize=6,color='#212121',fontweight='heavy',
horizontalalignment='left',verticalalignment='center', bbox=bbox_props)
axins1.text(x_pos, 0.675, s4, fontsize=5,color=s_color,fontweight='heavy', horizontalalignment='left',verticalalignment='center')
axins1.text(x_pos, 0.63, '预测决赛圈', fontsize=6,color='#212121',fontweight='heavy',
horizontalalignment='left',verticalalignment='center', bbox=bbox_props)
axins1.text(x_pos, 0.59, s2, fontsize=6,color=s_color,fontweight='heavy', horizontalalignment='left',verticalalignment='center')
2.5 添加标题
font_dict = {'family':'FZYaoti','weight':'heavy', 'size':12, 'color':'#880E4F','style':'normal'}
bbox_props = dict(boxstyle="round", fc="#EF6C00", ec="#EF6C00", alpha=0.6)
axins1.text(-0.4, 1.1, '卡塔尔世界杯哪个队最有可能夺冠', fontdict=font_dict,horizontalalignment='left',verticalalignment='center')
font_dict = {'family':'FZYaoti','size':6, 'color':'#3C3B6E','style':'normal'}
axins1.text(-0.27, 1.06, '数据来源:知乎 / 截止时间:2022/12/07 08:00:00',fontdict=font_dict,horizontalalignment='left',verticalalignment='center')
完成效果如下图:
2.6 词云
stylecloud.gen_stylecloud(
text=ciyun_words,
icon_name='fas fa-futbol',
font_path=r'STXINWEI.TTF',
palette='cartocolors.qualitative.Bold_5'
background_color='black',
max_font_size=100,
max_words=2000,
output_name=out_names
)
🏳️🌈 3. 更多可视化项目源码+数据
【资源 | Python可视化系列文章资源(源码+数据)】
以上就是本期为大家整理的全部内容了,赶快练习起来吧,原创不易,喜欢的朋友可以点赞、收藏也可以分享(注明出处)让更多人知道。