绘制交互式基础图形
绘制条形图-柱形图
商家A和商家B的各类商品的销售数据,如下表所示 | |||||||
商家 | 衬衫 | 毛衣 | 领带 | 裤子 | 风衣 | 高跟鞋 | 袜子 |
商家A | 120 | 56 | 28 | 98 | 129 | 28 | 107 |
商家B | 60 | 140 | 153 | 145 | 160 | 70 | 54 |
绘制的柱形图可以直观展示商家A和商家B的销售情况,便于对比同一类商品不同商家的销售差距。
from pyecharts.charts import Bar
import pandas as pd
from pyecharts.options import global_options as opts
from pyecharts.globals import ThemeType #主题,和颜色有关
data = pd.read_excel('商家A和商家B的各类商品的销售数据.xlsx',index_col='商家')#Excel的读取方式
x_data = data.columns.tolist()#tolist:列表
y_data001 = data.loc['商家A'].tolist() #名称要一模一样,最好复制
y_data002 = data.loc['商家B'].tolist()
def bar_base():
c=(
Bar(
init_opts=opts.InitOpts(
width = "900px",
height = "500px",
theme=ThemeType.LIGHT #主题和颜色有关
)
)#大小
.add_xaxis(x_data)
.add_yaxis('商家A',y_data001)
.add_yaxis('商家B',y_data002)
.set_global_opts(title_opts=opts.TitleOpts('商家A和商家B销售情况柱形图'))
)
return c
bar_base().render()
![f116ccd720bebafb65c52673a1a245b6.png](https://i-blog.csdnimg.cn/blog_migrate/09f7ae91834df15bd52e90333517bae1.png)
绘制条形图-条形图
商家A和商家B的各类商品的销售数据,如下表所示 | |||||||
商家 | 衬衫 | 毛衣 | 领带 | 裤子 | 风衣 | 高跟鞋 | 袜子 |
商家A | 120 | 56 | 28 | 98 | 129 | 28 | 107 |
商家B | 60 | 140 | 153 | 145 | 160 | 70 | 54 |
当条目较多时,使用柱形图展示数据时将会显得较拥挤。此时,可以通过翻转X轴和Y轴来显示图形,即使用条形图展示数据。
from pyecharts.charts import Bar
import pandas as pd
from pyecharts.options import global_options as opts
data = pd.read_excel('商家A和商家B的各类商品的销售数据.xlsx',index_col='商家')#Excel的读取方式
x_data = data.columns.tolist()#tolist:列表
y_data001 = data.loc['商家A'].tolist() #名称要一模一样,最好复制
y_data002 = data.loc['商家B'].tolist()
def bar_base():
c=(
Bar(
init_opts=opts.InitOpts(
width = "900px",
height = "500px"
)
)#大小
.add_xaxis(x_data)
.add_yaxis('商家A',y_data001)
.add_yaxis('商家B',y_data002)
.reversal_axis()#x,y倒置
.set_series_opts(label_opts=opts.LabelOpts(position="right"))#标签数值的位置
.set_global_opts(title_opts=opts.TitleOpts('商家A和商家B销售情况条形图'))
)
return c
bar_base().render()
![efaaa0ca5ec39880039afc0c91553d28.png](https://i-blog.csdnimg.cn/blog_migrate/c56d4d3f9361f7b69ecc3bb976c26c7d.png)
绘制条形图-堆叠柱形图
商家A和商家B的各类商品的销售数据,如下表所示 | |||||||
商家 | 衬衫 | 毛衣 | 领带 | 裤子 | 风衣 | 高跟鞋 | 袜子 |
商家A | 120 | 56 | 28 | 98 | 129 | 28 | 107 |
商家B | 60 | 140 | 153 | 145 | 160 | 70 | 54 |
可以将柱形图堆叠起来显示,即堆叠柱形图
from pyecharts.charts import Bar
import pandas as pd
from pyecharts.options import global_options as opts
data = pd.read_excel('商家A和商家B的各类商品的销售数据.xlsx',index_col='商家')#Excel的读取方式
x_data = data.columns.tolist()#tolist:列表
y_data001 = data.loc['商家A'].tolist() #名称要一模一样,最好复制
y_data002 = data.loc['商家B'].tolist()
def bar_base():
c=(
Bar(
init_opts=opts.InitOpts(
width = "900px",
height = "500px"
)
)#大小
.add_xaxis(x_data)
.add_yaxis('商家A',y_data001,stack='stack')
.add_yaxis('商家B',y_data002,stack='stack')
.set_global_opts(title_opts=opts.TitleOpts('商家A和商家B销售情况堆叠图'))
.set_series_opts(label_opts=opts.LabelOpts(position="insideTop"))#内页顶部
)
return c
bar_base().render()
![1f79a9fee48708c1dc5931d63fb7a47d.png](https://i-blog.csdnimg.cn/blog_migrate/2b95100df802547f72b1f8343bf35b65.png)
堆叠时,无论是数字还是字符串,都要保持一致。
标签的位置:可选 | |||||
top | left | right | bottom | inside | insideLeft |
insideRight | insideTop | insideBottom | insideTopLeft | insideBottomLeft | insideTopRight |
通过设置系列配置项,可以在柱形图上显示最大值、最小值以及平均值等标注。
如使用前图的数据绘制标注最大值、最小值、平均值的柱形图
from pyecharts.charts import Bar
import pandas as pd
from pyecharts.options import global_options as opts
from pyecharts import options as opt
data = pd.read_excel('商家A和商家B的各类商品的销售数据.xlsx',index_col='商家')#Excel的读取方式
x_data = data.columns.tolist()#tolist:列表
y_data001 = data.loc['商家A'].tolist() #名称要一模一样,最好复制
y_data002 = data.loc['商家B'].tolist()
def bar_base():
c=(
Bar(
init_opts=opts.InitOpts(
width = "900px",
height = "500px"
)
)#大小
.add_xaxis(x_data)
.add_yaxis('商家A',y_data001)
.add_yaxis('商家B',y_data002)
.set_global_opts(title_opts=opts.TitleOpts('指定标记点多柱形图'))
.set_series_opts(label_opts=opt.LabelOpts(is_show=False),#ture和false标签都不显示
markpoint_opts=opt.MarkPointOpts(
data=[
opt.MarkPointItem(type_="min", name="最小值"),
opt.MarkPointItem(type_="max", name="最大值"),
#opt.MarkPointItem(type_="average", name="平均值"),
]
)
)
)
return c
bar_base().render()
![70305cc5cf179de4569008d3b02bcf9e.png](https://i-blog.csdnimg.cn/blog_migrate/6981b982428827a88e3071a46bb701f7.png)
MarkPointltem | 标记点数据项 |
MarkLineltem | 标记线数据项 |
绘制散点图
![18f1ce6cda26abff5ef0bbeb34d7f93a.png](https://i-blog.csdnimg.cn/blog_migrate/021e76bc5d07b729f8475f38801500df.png)
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Scatter
student_data = pd.read_excel('学生身高和体重数据.xlsx', header=None)
student_data.set_index([0], inplace=True)
c = (
Scatter(
init_opts=opts.InitOpts(width='700px', height='400px')
)
.add_xaxis(xaxis_data=student_data.loc['身高'].tolist())
.add_yaxis('',
y_axis=student_data.loc['体重'].tolist(),
symbol_size=20,
label_opts=opts.LabelOpts(is_show=False)
)
.set_global_opts(
title_opts=opts.TitleOpts('体重与身高关系散点图'),
xaxis_opts=opts.AxisOpts(
type_='value', splitline_opts=opts.SplitLineOpts(is_show=True),
name='身高'),
yaxis_opts=opts.AxisOpts(name='体重',
type_='value',
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=True),
),
tooltip_opts=opts.TooltipOpts(is_show=False),
)
)
c.render()
![915827501ec14f99ee724c650028771c.png](https://i-blog.csdnimg.cn/blog_migrate/b15c8e64197d3bd5e1bc7f9e47b67c92.png)
绘制折线图-折线图
商家A和商家B的各类商品的销售数据,如下表所示 | |||||||
商家 | 衬衫 | 毛衣 | 领带 | 裤子 | 风衣 | 高跟鞋 | 袜子 |
商家A | 120 | 56 | 28 | 98 | 129 | 28 | 107 |
商家B | 60 | 140 | 153 | 145 | 160 | 70 | 54 |
import pandas as pd
from pyecharts.charts import Line
from pyecharts import options as opts
from pyecharts.globals import ThemeType
data = pd.read_excel('商家A和商家B的各类商品的销售数据.xlsx',
index_col='商家')
init_opts = opts.InitOpts(width='1000px', height='450px', theme=ThemeType.LIGHT)
line = (
Line()
.add_xaxis(data.columns.tolist())
.add_yaxis('商家A', data.loc['商家A'].tolist(), is_smooth=True) # 设置曲线光滑
.add_yaxis('商家B', data.loc['商家B'].tolist())
.set_global_opts(title_opts=opts.TitleOpts(title='商家A和商家B销售情况折线图')) # 设置全局选项
)
line.render()
![90153903ab3e0232b3d18bd63e166f26.png](https://i-blog.csdnimg.cn/blog_migrate/13c34b7a07a9de7ccfe273b47170ef9c.png)
绘制折线图-面积图
![4ad8a7631856683c57dc1b692cee031c.png](https://i-blog.csdnimg.cn/blog_migrate/79ebadb137ae85ef89a2bd3561290ad3.png)
import pandas as pd
from pyecharts import options as opts
from pyecharts.globals import ThemeType
from pyecharts.charts import Line
data = pd.read_excel('商家A和商家B的各类商品的销售数据.xlsx',
index_col='商家')
init_opts = opts.InitOpts(width='1000px', height='450px', theme=ThemeType.LIGHT)
line = (
Line()
.add_xaxis(data.columns.tolist())
.add_yaxis('商家A', data.loc['商家A'].tolist(),
areastyle_opts=opts.AreaStyleOpts(opacity=0.5, color='red')) # 设置曲线光滑
.add_yaxis('商家B', data.loc['商家B'].tolist(),
areastyle_opts=opts.AreaStyleOpts(opacity=0.6, color='blue'))
.set_global_opts(title_opts=opts.TitleOpts(title='商家A和商家B销售情况面积图')) # 设置全局选项
)
line.render()
![07d17c96b20abc8441bb17537dace445.png](https://i-blog.csdnimg.cn/blog_migrate/8563c5f5150a014b2d10993786da23f2.png)
绘制箱线图
![f385abf51da4ff3fd86d79570f282adb.png](https://i-blog.csdnimg.cn/blog_migrate/c8ac946df37bae8f66da417ffd9127d1.png)
![40b599033d4d01812efe3ffea47d597f.png](https://i-blog.csdnimg.cn/blog_migrate/b13400b78195ce5e39f2c63afb132b92.png)
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Boxplot
chinese_data = pd.read_excel('语文考试成绩.xlsx')
chinese_data.set_index(['班级'], inplace=True)
box = Boxplot(init_opts=opts.InitOpts(width='800px', height='400px'))
box.add_xaxis(list(chinese_data.index))
box.add_yaxis('', box.prepare_data([chinese_data.loc['1班'].tolist(),
chinese_data.loc['2班'].tolist(),
chinese_data.loc['3班'].tolist(),
chinese_data.loc['4班'].tolist()]))
box.set_global_opts(title_opts=opts.TitleOpts(title='4个班的考试成绩箱线图'))
box.render()
![b0c6c74c93afde46f28db65734e64e7f.png](https://i-blog.csdnimg.cn/blog_migrate/75633ad843d5130245fc164c339b8c9c.png)
绘制3D散点图
![313e16b22e7b89dd22b81af6dcbee9d8.png](https://i-blog.csdnimg.cn/blog_migrate/9773bf0226abb192f9a257e3db266b1e.png)
![974c3578d1a3ca4582732f8b798d97f2.png](https://i-blog.csdnimg.cn/blog_migrate/47cc71f1eedbd06bfe686a12a0d36d17.png)
最大携氧能力 | 体重 | 运动后心率 |
55.79 | 70.47 | 150 |
35.00 | 70.34 | 144 |
42.93 | 87.65 | 162 |
28.30 | 89.80 | 129 |
40.56 | 103.02 | 143 |
import pandas as pd
import numpy as np
from pyecharts import options as opts
from pyecharts.charts import Scatter3D
player_data = pd.read_excel('运动员的最大携氧能力、体重和运动后心率数据.xlsx')
player_data = [player_data['体重(kg)'], player_data['运动后心率(次/分钟)'],
player_data['最大携氧能力(ml/min)']]
player_data = np.array(player_data).T.tolist()
s = (
Scatter3D()
.add('', player_data, xaxis3d_opts=opts.Axis3DOpts(name='体重(kg)'),
yaxis3d_opts=opts.Axis3DOpts(name='运动后心率(次/分钟)'),
zaxis3d_opts=opts.Axis3DOpts(name='最大携氧能力(ml/min)')
)
.set_global_opts(title_opts=opts.TitleOpts(
title='最大携氧能力、体重和运动后心率3D散点图'),
visualmap_opts=opts.VisualMapOpts(range_color=[
'#1710c0', '#0b9df0', '#00fea8', '#00ff0d', '#f5f811', '#f09a09',
'#fe0300']), )
)
s.render()
![8f5d938cedc1095691490ec67f0c45ed.png](https://i-blog.csdnimg.cn/blog_migrate/6f8d4133511e22ddb7da8d83db8899b2.png)
绘制饼图-饼图
![649eb053d716c1a52cade4873a960929.png](https://i-blog.csdnimg.cn/blog_migrate/74de7aca2d3a1f7cd91c99e8fe42f28e.png)
![48745f499bf90530641f4fe7c2d92f59.png](https://i-blog.csdnimg.cn/blog_migrate/c631da450d4f516f8e2be823c2b2873e.png)
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.globals import ThemeType
data =data = pd.read_excel('商家A和商家B的各类商品的销售数据.xlsx',
index_col='商家')
init_opts = opts.InitOpts(width='1000px', height='450px', theme=ThemeType.LIGHT)
pie = (
Pie()
.add('', [list(z) for z in zip(data.columns.tolist(), data.loc['商家B'].tolist())])
.set_global_opts(title_opts=opts.TitleOpts(title='商家B销售情况饼图'))
.set_series_opts(label_opts=opts.LabelOpts(formatter='{b}:{c} ({d}%)'))
)
pie.render()
![965bac517af25e289727a3843a706afa.png](https://i-blog.csdnimg.cn/blog_migrate/c2f47528a34c096dbbd7ddad6828737d.png)
绘制饼图-环形图
![649eb053d716c1a52cade4873a960929.png](https://i-blog.csdnimg.cn/blog_migrate/74de7aca2d3a1f7cd91c99e8fe42f28e.png)
![d4a71a1dac4a69fdce08d33e573aeba9.png](https://i-blog.csdnimg.cn/blog_migrate/097becc9ca2dbce03f14bdba150b90ab.png)
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.globals import ThemeType
data =data = pd.read_excel('商家A和商家B的各类商品的销售数据.xlsx',
index_col='商家')
init_opts = opts.InitOpts(width='1000px', height='450px', theme=ThemeType.LIGHT)
pie = (
Pie(init_opts=opts.InitOpts(width='810px', height='400px'))
.add('', [list(z) for z in zip(data.columns.tolist(),
data.loc['商家B'].tolist())], radius=[20, 100])
.set_global_opts(title_opts=opts.TitleOpts(title='商家B销售情况环形图'))
.set_series_opts(label_opts=opts.LabelOpts(formatter='{b}:{c} ({d}%)'))
)
pie.render()
![8acdb491a81f5d7be59a07f92d14182d.png](https://i-blog.csdnimg.cn/blog_migrate/be6e384082288fb5195c8697467cfaae.png)
绘制饼图-玫瑰图
![1f39e50291b2b7fd907dc098850442bc.png](https://i-blog.csdnimg.cn/blog_migrate/8a8a9d9e5fbf8eed8b416af5e5798160.png)
![649eb053d716c1a52cade4873a960929.png](https://i-blog.csdnimg.cn/blog_migrate/74de7aca2d3a1f7cd91c99e8fe42f28e.png)
![e1476166c0dd493cc0c956639cee8aa2.png](https://i-blog.csdnimg.cn/blog_migrate/2960447f3382c2c219931699b843a422.png)
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.globals import ThemeType
data =data = pd.read_excel('商家A和商家B的各类商品的销售数据.xlsx',
index_col='商家')
init_opts = opts.InitOpts(width='1000px', height='450px', theme=ThemeType.LIGHT)
pie = (
Pie(init_opts=opts.InitOpts(width='810px', height='400px'))
.add('', [list(z) for z in zip(data.columns.tolist(),
data.loc['商家B'].tolist())],
rosetype='radius', radius=[20, 100])
.set_global_opts(title_opts=opts.TitleOpts(title='商家B销售情况玫瑰图'))
.set_series_opts(label_opts=opts.LabelOpts(formatter='{b}:{c} ({d}%)'))
)
pie.render()
![75a0dc90f1a62ef5708e9d948530d2b6.png](https://i-blog.csdnimg.cn/blog_migrate/0cb19dabb7746bb335c6bbe0b25c19e3.png)
绘制交互高级图形
绘制层叠多图
在同一个绘图区域,绘制不同类型的图表,即层叠多图,如同时绘制散点图和折线图、条形图和折线图等。在pyecharts库中,使用overlap()方法将多个图形叠加在一个视图区。
例:某省份1~12月份的降水量、蒸发量、平均温度部分数据如表所示。详见“1~12月份的降水量、蒸发量、平均温度.xlsx”
![60fde93b97a94e23067c47a87dd3373b.png](https://i-blog.csdnimg.cn/blog_migrate/8ee7b5ca04606d2380296ef5bd07bdc4.png)
![1c86a1f1c006beafba358d3f1ec591d9.png](https://i-blog.csdnimg.cn/blog_migrate/4f6d16e2af0edd55063ca96eeff94f56.png)
#引入第三方库
import pandas as pd
from pyecharts.charts import Bar,Line
from pyecharts import options as opts
#读数据
data =pd.read_excel('1~12月份的降水量、蒸发量、平均温度数据.xlsx')
data_x =data['月份'].tolist()
data_y_zfl=data['蒸发量'].tolist()
data_y_jsl=data['降水量'].tolist()
data_y_pjwd=data['平均温度'].tolist()
def zuhe_show():
c =(
Bar()
.add_xaxis(data_x)
.add_yaxis('蒸发量',data_y_zfl)
.add_yaxis('降水量',data_y_jsl)
.set_global_opts(
title_opts=opts.TitleOpts(title='叠加条形图和折线图'),
xaxis_opts=opts.AxisOpts(
name='月份',
name_location='center',
name_gap=30
),
yaxis_opts=opts.AxisOpts(
name='蒸发量/降水量(ml)',
name_location='center',
name_gap=50,
axislabel_opts=opts.LabelOpts(formatter="{value} ml")
)
)
.extend_axis(
yaxis=opts.AxisOpts(
name='平均温度(°C)',
name_location='center',
name_gap=50,
axislabel_opts=opts.LabelOpts(formatter="{value} °C")
)
)
)
return c
def line_show():
c =(
Line()
.add_xaxis(data_x)
.add_yaxis(
'平均温度',data_y_pjwd,
yaxis_index=1
)
)
return c
zuhe_show().overlap(line_show()).render('复杂.html')
![b4b5488ad17bae0e77f586f7fec2f536.png](https://i-blog.csdnimg.cn/blog_migrate/ea0b6aaf362e8fc61e7b59ca5798673b.png)
绘制漏斗图
由漏斗图可以直观地查看各个网购环节人数的转化率。
网购环节 | 人数 |
浏览商品 | 2000 |
加入购物车 | 900 |
生成订单 | 400 |
支付订单 | 320 |
完成交易 | 300 |
from pyecharts import options as opts
import pandas as pd
from pyecharts.charts import Funnel
data = pd.read_excel('某淘宝店铺的订单转化率统计数据.xlsx')
x_data = data['网购环节'].tolist()
y_data = data['人数'].tolist()
data = [[x_data[i], y_data[i]] for i in range(len(x_data))]
def fuzao_show():
c = (
Funnel()
.add('',
data_pair=data,
label_opts=opts.LabelOpts(
position='inside',
formatter="{b}:{d}%"
),
gap=2,
tooltip_opts=opts.TooltipOpts(trigger='item'),
itemstyle_opts=opts.ItemStyleOpts( border_width=1))
.set_global_opts(
title_opts=opts.TitleOpts(title='某淘宝店铺的订单转化率漏斗图'),
legend_opts=opts.LegendOpts(pos_left='40%')
)
)
return c
# funnel.render_notebook()
fuzao_show().render("复杂.html")
![f81f19504f3a6a2a921c545fa6e79fcd.png](https://i-blog.csdnimg.cn/blog_migrate/70e77a844a67ff65ef21c794a7e13d76.png)
绘制热力图
在pyecharts库中,可以使用HeatMap类绘制热力图。
![e1147da564d35a480e39b81da9e16d00.png](https://i-blog.csdnimg.cn/blog_migrate/52d261acec09a67c447763b4fc3fe3e8.png)
![dd6eff9c787f219248485049685e9f85.png](https://i-blog.csdnimg.cn/blog_migrate/b81280c3f32df75836c7eef67c742938.png)
from pyecharts import options as opts
import pandas as pd
from pyecharts.charts import HeatMap
data = pd.read_excel('heatmap.xlsx', index_col=0)
y_data = list(data.columns)
x_data = list(data.index)
values = data.iloc[:, 0: 7].values.tolist()
value = [[i, j, values[i][j]] for i in range(len(x_data)) for j in range(len(y_data))]
def fuzao_show():
heatmap = (
HeatMap()
.add_xaxis(x_data)
.add_yaxis(
'',
y_data,
value,
label_opts=opts.LabelOpts(is_show=True, position='inside'),
)
.set_global_opts(
title_opts=opts.TitleOpts(title='网站点击量热力图'),
visualmap_opts=opts.VisualMapOpts(pos_bottom='center'),
)
)
return heatmap
fuzao_show().render('复杂.html')
![6c8d9474ad4d60008727a9a8f454fc7e.png](https://i-blog.csdnimg.cn/blog_migrate/af2d6b13bbd8891cf74690c941160c25.png)
绘制词云图
在pyecharts库中,可使用WordCloud类绘制词云图。WordCloud类的基本使用格式如下。
在绘制词云图前,需要统计各词的词频。例如,附件Wordcloud.csv统计的部分宋词词频数据,绘制的词云图。
由词云图可知,宋词中使用“东风”“人间”“何处”的次数相对较多。
from pyecharts import options as opts
import pandas as pd
from pyecharts.charts import WordCloud
data_read = pd.read_csv('worldcloud.csv', encoding='gbk')
words = list(data_read['词语'].values)
num = list(data_read['频数'].values)
data = [k for k in zip(words, num)]
data = [(i, str(j)) for i, j in data]
def fuzao_show():
c = (
WordCloud()
.add(series_name='词统计', data_pair=data, word_size_range=[10, 100])
.set_global_opts(title_opts=opts.TitleOpts(
title='部分宋词词频词云图', title_textstyle_opts=
opts.TextStyleOpts(font_size=23)),
tooltip_opts=opts.TooltipOpts(is_show=True))
)
return c
fuzao_show().render('复杂.html')
![4386dd6c5d53ac1808a8c04092bc1397.png](https://i-blog.csdnimg.cn/blog_migrate/a797146e6b133722af872dc5cd853c95.png)
绘制关系图
在pyecharts库中,可使用Graph类绘制关系图。
某公司销售部的部分员工微信好友关系数据如下表所示。
目标人物 | 其它人物 | 关系 |
周建 | [贺芳,吴大,张三,刘霞] | [夫妻,同事,同学,同学] |
黄婧 | [张三,刘霞] | [朋友,同事] |
文华 | [刘霞,吴大] | [夫妻,同事] |
由关系图可以直观地看出每个人之间的关系
from pyecharts import options as opts
from pyecharts.charts import Graph
# 绘制微信好友关系图
# 节点
nodes = [
opts.GraphNode(name='张三', symbol='circle', symbol_size=10),
opts.GraphNode(name='吴大', symbol='pin', symbol_size=10),
opts.GraphNode(name='贺芳', symbol='pin', symbol_size=10),
opts.GraphNode(name='刘霞', symbol='circle', symbol_size=10),
opts.GraphNode(name='黄婧', symbol='circle', symbol_size=10),
opts.GraphNode(name='周建', symbol='circle', symbol_size=10),
opts.GraphNode(name='文华', symbol='circle', symbol_size=10)
]
# 关系
links = [
opts.GraphLink(source='周建', target='贺芳', value='夫妻'),
opts.GraphLink(source='周建', target='吴大', value='同事'),
opts.GraphLink(source='周建', target='张三', value='同学'),
opts.GraphLink(source='黄婧', target='张三', value='朋友'),
opts.GraphLink(source='黄婧', target='刘霞', value='同事'),
opts.GraphLink(source='文华', target='刘霞', value='夫妻'),
opts.GraphLink(source='文华', target='吴大', value='同事'),
opts.GraphLink(source='周建', target='刘霞', value='同学')
]
# 绘图
def fuzao_show():
c = (
Graph()
.add(series_name='',
nodes=nodes,
is_roam=False,
is_rotate_label=True,
links=links, repulsion=4000, edge_label=opts.LabelOpts(
is_show=True, position='middle', formatter='{c}'))
.set_global_opts(title_opts=opts.TitleOpts(title='微信好友关系图'))
)
return c
fuzao_show().render('复杂.html')
![db257fffeafec929aab6626551a8bad7.png](https://i-blog.csdnimg.cn/blog_migrate/1861a549d5fd417892f12efaddac6ef7.png)
绘制桑基图
在pyecharts库中,可使用Sankey类绘制桑基图。
附件sanky.csv是某家庭一个月的生活开支明细数据,绘制桑基图。
由桑基图可以直观地看出商品的开支情况,以及各商品小类、大类的开支情形。
from pyecharts import options as opts
import pandas as pd
from pyecharts.charts import Sankey
# 绘制生活开支桑基图
# 读取csv文件
data = pd.read_csv('sanky.csv', encoding='utf-8', header=None, sep='\t')
# 生成节nodes
Nodes = []
Nodes.append({'name': '总支出'})
for i in data[0].unique():
dic = {}
dic['name'] = i
Nodes.append(dic)
# 生成links
Links = []
for i in data.values:
dic = {}
dic['source'] = i[0]
dic['target'] = i[1]
dic['value'] = i[2]
Links.append(dic)
# 可视化
def fuzao_show():
c = (
Sankey()
.add('', Nodes, Links, pos_left='10%',
linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=0.5, color='source', type_='dotted'),
label_opts=opts.LabelOpts(position='right', ),
)
.set_global_opts(title_opts=opts.TitleOpts(title='生活开支桑基图'))
)
return c
fuzao_show().render('复杂.html')
![ebb31389d39738de5000c26ae5823ada.png](https://i-blog.csdnimg.cn/blog_migrate/c1ff77f2843511e6cceac9698292974f.png)
绘制并行多图
绘制并行多图
![9dbc567b8b48413f420cf944a9703722.png](https://i-blog.csdnimg.cn/blog_migrate/8707b53bc5538aa84eb3bb8d8d7e37af.png)
import pandas as pd
from pyecharts.charts import Bar,Pie,Grid
data = pd.read_excel('商家A和商家B的各类商品的销售数据.xlsx',index_col='商家')
bar_data_x = data.columns.tolist()
bar_data_y = data.loc['商家A'].tolist()
pie_data = [list(z) for z in zip(bar_data_x,bar_data_y)]
from pyecharts import options as opts
bar=(
Bar()
.add_xaxis(bar_data_x)
.add_yaxis('',bar_data_y,bar_width='60%')
.set_global_opts(
title_opts=opts.TitleOpts('商品A销售情况柱形图')
)
)
pie=(
Pie()
.add('',pie_data,radius=[20,100],center=[900,300])
.set_global_opts(
title_opts=opts.TitleOpts('商品A销售情况饼图',pos_right='30%'),
legend_opts=opts.LegendOpts( orient="vertical",pos_right='10%')
)
.set_series_opts(
label_opts=opts.LabelOpts(formatter='{b}:{c} ({d}%)')
)
)
grid = (
Grid(init_opts=opts.InitOpts(
width="1250px",
height="600px"
)
)
.add(bar,grid_opts=(opts.GridOpts(pos_right='45%')))
.add(pie,
grid_opts=(opts.GridOpts(pos_left='50%')),
)
)
grid.render('复杂.html')
![e28820fe0e2a720fb9fa85921d9a6eed.png](https://i-blog.csdnimg.cn/blog_migrate/7b0f709159249d53e1630373e8540eb9.png)
from pyecharts.charts import Bar, Pie, Grid
from pyecharts import options as opts
import pandas as pd
data = pd.read_excel('商家A和商家B的各类商品的销售数据.xlsx',
index_col='商家')
bar = (
Bar()
.add_xaxis(data.columns.tolist())
.add_yaxis('', data.loc['商家A'].tolist())
.set_global_opts(title_opts=opts.TitleOpts(title='商品A销售情况柱形图'),
legend_opts=opts.LegendOpts(pos_left='30%'))
)
pie = (Pie()
.add('', [list(z) for z in zip(data.columns.tolist(), data.loc['商家A'].tolist())],
radius=[20, 100], center=[700, 300])
.set_global_opts(title_opts=opts.TitleOpts(
title='商品A销售情况饼图', pos_left='60%'),
legend_opts=opts.LegendOpts(orient='vertical', pos_right=0))
.set_series_opts(label_opts=opts.LabelOpts(formatter='{b}:{c} ({d}%)'))
)
grid = (Grid(init_opts=opts.InitOpts(width='950px', height='600px'))
.add(bar, grid_opts=opts.GridOpts(pos_right='50%'))
.add(pie, grid_opts=opts.GridOpts(pos_left='70%'))
)
# grid.render_notebook()
grid.render("复杂.html")
绘制顺序多图
![423b63a5a18ed03a6db1e332fe43d6bd.png](https://i-blog.csdnimg.cn/blog_migrate/bbf1378f56a1a4ee869c0dbe85543176.png)
利用商家A和商家B的销售数据绘制柱形图、玫瑰图、环形图,并通过Page类实现顺序多图。
在图形的左上角存在Save_confing按钮,此时可以通过拖放的方式调整页面显示效果,当拖放完成后,单击Save_config按钮,将会下载一个名为“chart_config.json”的配置文件,即可保存设置的布局。
#老师做的
from pyecharts.charts import Bar, Pie
from pyecharts import options as opts
from pyecharts.charts import Page
from pyecharts.globals import ThemeType
import pandas as pd
data = pd.read_excel('商家A和商家B的各类商品的销售数据.xlsx',
index_col='商家')
bar = (
Bar(init_opts=opts.InitOpts(width='800px', height='310px', theme=ThemeType.LIGHT))
.add_xaxis(data.columns.tolist())
.add_yaxis('商家A', data.loc['商家A'].tolist())
.add_yaxis('商家B', data.loc['商家B'].tolist())
.set_global_opts(title_opts=opts.TitleOpts(
title='商家A和商家B销售情况柱形图')))
pie1 = (Pie(init_opts=opts.InitOpts(
width='800px', height='310px'))
.add('', [list(z) for z in zip(
data.columns.tolist(), data.loc['商家A'].tolist())], radius=[20, 100])
.set_global_opts(title_opts=opts.TitleOpts(title='商家A销售情况环形图'),
legend_opts=opts.LegendOpts(orient='vertical', pos_left=0,
pos_bottom='40%'))
.set_series_opts(label_opts=opts.LabelOpts(formatter='{b}:{c} ({d}%)')))
pie2 = (Pie(init_opts=opts.InitOpts(
width='800px', height='310px'))
.add('', [list(z) for z in zip(data.columns.tolist(), data.loc['商家B'].tolist())],
rosetype='radius', radius=[20, 100])
.set_global_opts(title_opts=opts.TitleOpts(title='商家B销售情况玫瑰图'),
legend_opts=opts.LegendOpts(orient='vertical', pos_right=0, pos_bottom='40%'))
.set_series_opts(label_opts=opts.LabelOpts(formatter='{b}:{c} ({d}%)')))
page = (Page(page_title='Page绘制顺序多图', interval=2,
layout=Page.DraggablePageLayout).add(bar, pie1, pie2))
page.render("复杂.html")
![a3bb7cfdd7fd75352edf1a3355a2e6d3.png](https://i-blog.csdnimg.cn/blog_migrate/625736141910d72d9bec795582631a3c.png)
![4b00a80bc323bf55689908822d8b7471.png](https://i-blog.csdnimg.cn/blog_migrate/3db4b36ec550dd861d4892d16016a07f.png)
![09858fd77b7f6345d4c48aa1bd42a383.png](https://i-blog.csdnimg.cn/blog_migrate/95ed2eb65df1f9762c8709f67de8069e.png)
#自己
import pandas as pd
from pyecharts.charts import Bar,Pie,Page
from pyecharts import options as opts
from pyecharts.globals import ThemeType
data = pd.read_excel('商家A和商家B的各类商品的销售数据.xlsx',index_col='商家')
bar_data_x = data.columns.tolist()
bar_data_y1 = data.loc['商家A'].tolist()
bar_data_y2 = data.loc['商家B'].tolist()
pie_data1 = [list(z) for z in zip(bar_data_x,bar_data_y1)]
pie_data2 = [list(z) for z in zip(bar_data_x,bar_data_y2)]
bar=(
Bar(
init_opts=opts.InitOpts(width='800px',height='310px',theme= ThemeType.LIGHT)
)
.add_xaxis(bar_data_x)
.add_yaxis('商家A',bar_data_y1)
.add_yaxis('商家B',bar_data_y2)
.set_global_opts(
title_opts=opts.TitleOpts('商品A和商家B销售情况柱形图')
)
)
pie1=(
Pie(init_opts=opts.InitOpts(width='800px',height='310px'))
.add('',pie_data1,radius=[20,100])
.set_global_opts(
title_opts=opts.TitleOpts('商品A销售情况环形图'),
legend_opts=opts.LegendOpts( orient="vertical",pos_left=0,pos_bottom='40%')
)
.set_series_opts(
label_opts=opts.LabelOpts(formatter='{b}:{c} ({d}%)')
)
)
pie2=(
Pie(init_opts=opts.InitOpts(width='800px',height='310px'))
.add('',pie_data2,radius=[20,100],rosetype='radius')
.set_global_opts(
title_opts=opts.TitleOpts('商品B销售情况玫瑰图'),
legend_opts=opts.LegendOpts( orient="vertical",pos_bottom='40%',pos_left=0)
)
.set_series_opts(
label_opts=opts.LabelOpts(formatter='{b}:{c} ({d}%)')
)
)
page=(
Page(
page_title='Page绘制顺序多图',interval=2,layout=Page.DraggablePageLayout
)
.add(bar,pie1,pie2)
)
page.render('复杂.html')
绘制时间轮播多图
在pyecharts库中,可使用Timeline类绘制时间线轮播多图。
![a6993ea25765984954266519efffefbe.png](https://i-blog.csdnimg.cn/blog_migrate/9bb0b337a3eb1ae0c602b03f6c84a5a1.png)
![8078a6a6e965641905601a7a95fa6449.png](https://i-blog.csdnimg.cn/blog_migrate/2f1e177236855400230426cfc1fd0416.png)
#老师做的
from pyecharts.charts import Bar
from pyecharts import options as opts
import pandas as pd
from pyecharts.charts import Timeline
data_A = pd.read_excel('timeline.xlsx')
data_B = pd.read_excel('timeline.xlsx', sheet_name=1)
x_data = data_A.columns.tolist()[1:]
time = data_A['月份'].tolist()
timeline = Timeline()
for i in range(len(time)):
y_data_A = data_A.iloc[i, 1: 9].tolist()
y_data_A = [str(j) for j in y_data_A]
y_data_B = data_B.iloc[i, 1: 9].tolist()
y_data_B = [str(j) for j in y_data_B]
bar = (Bar()
.add_xaxis(x_data)
.add_yaxis('手机店A', y_data_A)
.add_yaxis('手机店B', y_data_B)
.set_global_opts(title_opts=opts.TitleOpts('手机店{}销售情况'.format(time[i])))
)
timeline.add(bar, time[i])
timeline.add_schema(play_interval=1000, is_auto_play=True, symbol='pin')
# timeline.render_notebook()
timeline.render("复杂.html")
![72e91f573cabc0fa967f6468964c8550.png](https://i-blog.csdnimg.cn/blog_migrate/cfd014885d0710dd7d96a0367326b622.png)