前言
哪个国家的世界五百强企业数量最多?今天带你用python来处理数据,并实现数据可视化,解决对现有数据产生的疑问。
本次代码是在 Jupyter Notebook 里面编写的
Python从零基础入门到实战系统教程、源码、视频、在线一对一解答,可以加群:1039649593
先导入本次代码需要的模块
代码语言:javascript复制import pandas as pd
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.commons.utils import JsCode
from pyecharts.globals import ThemeType
导入数据
代码语言:javascript复制data = pd.read_excel('2021年世界五百强排行榜.xlsx')
data.head(5)
统计世界500强企业各个国家之间的占比
代码语言:javascript复制pie = Pie(init_opts=opts.InitOpts(theme='light',
width='800px',
height='700px'))
data_x = data1['国家'].tolist()
data_y = data1['排名'].tolist()
pie.add("",
[list(z) for z in zip(data_x, data_y)],
radius=["35%", "55%"]
)
pie.set_series_opts(label_opts=opts.LabelOpts(position="insideLeft",
font_size=12,
color='rgba(0,0,0,0.5)',
font_weight='bold',
formatter='{b}:{d}%'),
itemstyle_opts={'normal': {
'opacity': 1, # 透明度
'shadowColor': 'rgba(0, 0, 0, 0.2)', # 阴影颜色
'shadowBlur': 5, # 阴影大小
'shadowOffsetY': 5, # Y轴方向阴影偏移
'shadowOffsetX': 5, # x轴方向阴影偏移
}
}
)
pie.set_global_opts(legend_opts=opts.LegendOpts(is_show=True, pos_left='right', pos_top='center', orient='vertical'),
title_opts=opts.TitleOpts(title="2021年世界各国500强企业占比", pos_top='center', pos_left='center',
title_textstyle_opts=opts.TextStyleOpts(font_size=16)),
visualmap_opts=opts.VisualMapOpts(
min_=0,
max_=200,
))
pie.render_notebook()
将国家列设置为索引
代码语言:javascript复制bar = Bar(init_opts=opts.InitOpts(theme=ThemeType.PURPLE_PASSION))
bar.add_yaxis("世界500强企业数量", data1['排名'].tolist())
bar.set_global_opts(title_opts=opts.TitleOpts(title="每日电量统计表"),toolbox_opts=opts.ToolboxOpts(),xaxis_opts=opts.AxisOpts(name_rotate=60,axislabel_opts={"rotate":45}))
bar.render_notebook()
# 绘制直方图来显示
# 线性渐变
color_js = """new echarts.graphic.LinearGradient(0, 1, 0, 0,
[{offset: 0, color: '#008B8B'}, {offset: 1, color: '#FF6347'}], false)"""
bar = Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK))
bar.add_xaxis(data1.index.tolist())
bar.add_yaxis("世界500强企业数量", data1['排名'].tolist(),itemstyle_opts=opts.ItemStyleOpts(color=JsCode(color_js)))
bar.set_global_opts(title_opts=opts.TitleOpts(title="2021年各国世界500强企业统计",subtitle='',pos_left='center',pos_top='3%'),
toolbox_opts=opts.ToolboxOpts(),
xaxis_opts=opts.AxisOpts(name_rotate=60,axislabel_opts={"rotate":45}),
legend_opts=opts.LegendOpts(is_show=True,
pos_left='80%',
pos_bottom='90%'))
bar.render_notebook()
筛选出中国的世界500强企业并进行数据可视化分析
代码语言:javascript复制# m_df = df.groupby(['行业'])['存活天数'].mean().reset_index()
data2.sort_values(by='排名',ascending=False, inplace=True)
data_x = data2['字段2'].tolist()
data_y = data2['排名'].tolist()
bar = Bar(init_opts=opts.InitOpts(theme='light',
width='1000px',
height='900px'))
bar.add_xaxis(data_x)
bar.add_yaxis('平均存活天数', [int(i) for i in data_y])
bar.set_series_opts(label_opts=opts.LabelOpts(position="insideLeft",
font_size=12,
font_weight='bold',
formatter='{b}:{c}'))
bar.set_global_opts(title_opts=opts.TitleOpts(title="2021年中国世界500强企业城市分布", pos_top='2%', pos_left='center',
title_textstyle_opts=opts.TextStyleOpts(font_size=16)),
legend_opts=opts.LegendOpts(is_show=False),
xaxis_opts=opts.AxisOpts(is_show=False, is_scale=True),
yaxis_opts=opts.AxisOpts(is_show=False),
visualmap_opts=opts.VisualMapOpts(
max_=3000,
min_=1500,
is_piecewise=False,
dimension=0,
range_color=['rgba(238,25,38,1)', 'rgba(289,112,147,0.4)'])
)
bar.reversal_axis()
bar.render_notebook()