获取北京、上海、江苏、广东四省的2008—2012年的GDP数据
在Jupyter Notebook上实现代码如下:
import pandas as pd
data = pd.read_csv("C:/Users/dell/Desktop/gdp.csv", encoding="utf-8",usecols=[9,8,7,6,5,0])
data = data.iloc[[0,8,9,18],0:6]
data
获取北京、浙江、福建、广东四省的2008——2012年的GDP数据,并且依次按2008y到2012y的列的值大小排序
在Jupyter Notebook上实现代码如下:
import pandas as pd
for i in range(2008, 2013):
data = pd.read_csv("C:/Users/dell/Desktop/gdp.csv", encoding="utf-8",usecols=[9,8,7,6,5,0])
data = data.iloc[[0,10,12,18],0:6]
data = data.sort_values( [str(i) + 'y'] ,ascending = False)
print(data)
其中,设置折线图效果如下:
线图Y轴log效果,设置每个省使用的点用不同的形状,数据紧贴Y轴,横向zoom缩放条在左下方
import pyecharts.options as opts
from pyecharts.charts import Line
import pandas as pd
data = pd.read_csv("C:/Users/dell/Desktop/gdp.csv", encoding="utf-8",usecols=[9,8,7,6,5,0])
data = data.iloc[[0,8,9,18],0:6]
gdp0 = data.loc[0].values.tolist()
gdp8 = data.loc[8].values.tolist()
gdp9 = data.loc[9].values.tolist()
gdp18 = data.loc[18].values.tolist()
#x轴需转换成str,如果x是纯文本就会出现空白折线图
x = ['2008','2009','2010','2011','2012']
c = (
Line()
.add_xaxis(x)
.add_yaxis("北京",gdp0 , symbol="triangle" , symbol_size=12 , linestyle_opts=opts.LineStyleOpts(width=2) , label_opts=opts.LabelOpts(is_show=False), )
.add_yaxis("上海",gdp8 , linestyle_opts=opts.LineStyleOpts(width=2) , label_opts=opts.LabelOpts(is_show=False), )
.add_yaxis("江苏",gdp9 , symbol="arrow" ,symbol_size=12 , linestyle_opts=opts.LineStyleOpts(width=2) , label_opts=opts.LabelOpts(is_show=False), )
.add_yaxis("广东",gdp18 , symbol="pin" ,symbol_size=12 , linestyle_opts=opts.LineStyleOpts(width=2) , label_opts=opts.LabelOpts(is_show=False), )
.set_global_opts(
datazoom_opts=opts.DataZoomOpts(pos_left='left'),
title_opts=opts.TitleOpts(title="Line-Line_yaixs_log"),
xaxis_opts=opts.AxisOpts(name="年份",boundary_gap=False),
yaxis_opts=opts.AxisOpts(
type_="log",
name="GDP",
splitline_opts=opts.SplitLineOpts(is_show=True),
is_scale=True,
),
)
)
c.render_notebook()
Timeline_Pie
其中,设置时间线轮播多图效果如下:
Timeline饼图,radius类型,轮播年份
from pyecharts import options as opts
from pyecharts.charts import Pie, Timeline
import pandas as pd
tl = Timeline()
for i in range(2008, 2013):
data = pd.read_csv("C:/Users/dell/Desktop/gdp.csv", encoding="utf-8",usecols=[9,8,7,6,5,0])
data = data.iloc[[0,8,9,18],0:6]
city = data['province'].values.tolist()
time_gdp = data[str(i) + 'y'].values.tolist()
pie = (
Pie()
.add(
"",
[list(z) for z in zip(city,time_gdp)],
rosetype="radius",
radius=["30%", "55%"],
)
.set_global_opts(title_opts=opts.TitleOpts("{}年".format(i)))
)
tl.add(pie, "{}年".format(i))
tl.render_notebook()
Timeline_Bar
Timeline横向柱状图,按GDP排序,值大的在下,轮播轴纵向在左侧
from pyecharts import options as opts
from pyecharts.charts import Bar, Timeline
import pandas as pd
tl = Timeline()
#北京、福建、浙江、广东四省的2008——2012年的GDP
for i in range(2008, 2013):
data = pd.read_csv("C:/Users/dell/Desktop/gdp.csv", encoding="utf-8",usecols=[9,8,7,6,5,0])
data = data.iloc[[0,10,12,18],0:6]
data = data.sort_values( [str(i) + 'y'] ,ascending = False)
time_gdp = data[str(i) + 'y'].values.tolist()
city = data['province'].values.tolist()
bar = (
Bar()
.add_xaxis(city)
.add_yaxis("GDP", time_gdp, label_opts=opts.LabelOpts(position="right"),)
.reversal_axis()
.set_global_opts(
title_opts=opts.TitleOpts("Timeline-Bar-Reversal (时间: {} 年)".format(i)),
)
)
#轮播组件的设置
tl.add_schema(orient="vertical",is_timeline_show=True,pos_top='24px',pos_left='30px',pos_right='96%',is_inverse=True)
tl.add(bar, "{}".format(i))
tl.render_notebook()
其中,设置图形效果如下:
Streamlit任意控件,展示2008–2012年的GDP
在pycharm运行:
import streamlit as st
from pyecharts import options as opts
from pyecharts.charts import Bar
import pandas as pd
st.header("streamlit控件年份+图表")
time = st.select_slider("时间",[2008,2009,2010,2011,2012])
for i in range(2008,2013):
data = pd.read_csv("C:/Users/dell/Desktop/gdp.csv", encoding="utf-8",usecols=[0,9,8,7,6,5])
data = data.iloc[[0,8,9,18],0:6]
data = data.sort_values( [str(i) + 'y'] ,ascending = True)
time_gdp = data[str(i) + 'y'].values.tolist()
city = data['province'].values.tolist()
c = (
Bar()
.add_xaxis(city)
.add_yaxis("gdp", time_gdp)
.reversal_axis()
.set_series_opts(label_opts=opts.LabelOpts(position="right"))
.set_global_opts(title_opts=opts.TitleOpts(title="{}年".format(time)))
)
#饼图
# c = (
# Pie()
# .add("".format(time), [list(z) for z in zip(city, time_gdp)])
# .set_global_opts(title_opts=opts.TitleOpts(title="{}年".format(time)))
# .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
# )
html = c.render_embed()
st.components.v1.html(html, width=1000, height=800, scrolling=False)