提交 5507acaa 编写于 作者: 秦英杰

fix:图形处理

上级 c20d6f4c
此差异已折叠。
......@@ -14,10 +14,10 @@ line.add_yaxis("GDP", [30, 20, 10])
# 设置全局配置项set_global_opts来设置,
line.set_global_opts(
title_opts=TitleOpts(title="GDP展示", pos_left="center", pos_bottom="1%"),
legend_opts=LegendOpts(is_show=True),
toolbox_opts=ToolboxOpts(is_show=True),
visualmap_opts=VisualMapOpts(is_show=True),
title_opts=TitleOpts(title="GDP展示", pos_left="center", pos_bottom="1%"), # title设置
legend_opts=LegendOpts(is_show=True), # 图例
toolbox_opts=ToolboxOpts(is_show=True), # 工具箱
visualmap_opts=VisualMapOpts(is_show=True), # 视觉映射
)
# 通过render方法,将代码生成为图像
......
from pyecharts.charts import Bar, Timeline
from pyecharts import options as opts
from pyecharts.globals import ThemeType
# 读取数据
f = open("/Users/qinyingjie/Documents/python-workspace/python-demo/data/动态柱状图数据/1960-2019全球GDP数据.csv",
"r",
encoding="GB2312")
data_lines = f.readlines()
# 关闭文件
f.close()
# 删除第一条数据
data_lines.pop(0)
# 将数据转换为字典存储,格式为:
# { 年份: [ [国家, gdp], [国家,gdp], ...... ], 年份: [ [国家, gdp], [国家,gdp], ...... ], ...... }
data_dict = {}
for line in data_lines:
year = int(line.split(",")[0]) # 年份
country = line.split(",")[1] # 国家
gdp = float(line.split(",")[2]) # gdp数据
try:
data_dict[year].append([country, gdp])
except KeyError:
data_dict[year] = []
data_dict[year].append([country, gdp])
# 创建时间线对象
timeline = Timeline({"theme": ThemeType.LIGHT})
# 排序年份
sorted_year_list = sorted(data_dict.keys())
for year in sorted_year_list:
data_dict[year].sort(key=lambda element: element[1], reverse=True)
# 取出本年份前8名的国家
year_data = data_dict[year][0:8]
x_data = []
y_data = []
for country_gdp in year_data:
x_data.append(country_gdp[0]) # x轴添加国家
y_data.append(int(country_gdp[1] / 100000000)) # y轴添加gdp数据
# 构建柱状图
bar = Bar()
x_data.reverse()
y_data.reverse()
colors = ["#ff4757", "#ff9f43", "#f4e925", "#90ed7d", "#58d68d", "#3dd9c7", "#3d99c7", "#9e6ffe", "#c051ec"]
bar.add_xaxis(x_data)
bar.add_yaxis(
"GDP(亿)",
y_data,
label_opts=opts.LabelOpts(position="right", font_size="15px"),
# itemstyle_opts=opts.ItemStyleOpts(color=colors[:len(y_data)]),
)
# for i in range(len(x_data)):
# bar.add_xaxis([x_data[i]])
# bar.add_yaxis(
# "GDP(亿)",
# [y_data[i]],
# label_opts=opts.LabelOpts(position="right"),
# itemstyle_opts=opts.ItemStyleOpts(color=colors[i % len(colors)])
# )
# 反转x轴和y轴
bar.reversal_axis()
# 设置每一年的图表的标题
bar.set_global_opts(
title_opts=opts.TitleOpts(title=f"{year}年全球前8GDP数据")
)
timeline.add(bar, str(year))
# 设置时间线自动播放
timeline.add_schema(
play_interval=1000,
is_timeline_show=True,
is_auto_play=True,
is_loop_play=False
)
# 绘图
timeline.render("1960-2019全球GDP前8国家.html")
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Awesome-pyecharts</title>
<script type="text/javascript" src="https://assets.pyecharts.org/assets/v5/echarts.min.js"></script>
</head>
<body >
<div id="e93ec85d48b54bf3a84f485e3ded8ac4" class="chart-container" style="width:900px; height:500px; "></div>
<script>
var chart_e93ec85d48b54bf3a84f485e3ded8ac4 = echarts.init(
document.getElementById('e93ec85d48b54bf3a84f485e3ded8ac4'), 'white', {renderer: 'canvas'});
var option_e93ec85d48b54bf3a84f485e3ded8ac4 = {
"animation": true,
"animationThreshold": 2000,
"animationDuration": 1000,
"animationEasing": "cubicOut",
"animationDelay": 0,
"animationDurationUpdate": 300,
"animationEasingUpdate": "cubicOut",
"animationDelayUpdate": 0,
"aria": {
"enabled": false
},
"color": [
"#5470c6",
"#91cc75",
"#fac858",
"#ee6666",
"#73c0de",
"#3ba272",
"#fc8452",
"#9a60b4",
"#ea7ccc"
],
"series": [
{
"type": "line",
"name": "GDP",
"connectNulls": false,
"xAxisIndex": 0,
"symbolSize": 4,
"showSymbol": true,
"smooth": false,
"clip": true,
"step": false,
"data": [
[
"\u4e2d\u56fd",
30
],
[
"\u7f8e\u56fd",
20
],
[
"\u82f1\u56fd",
10
]
],
"hoverAnimation": true,
"label": {
"show": true,
"margin": 8
},
"logBase": 10,
"seriesLayoutBy": "column",
"lineStyle": {
"show": true,
"width": 1,
"opacity": 1,
"curveness": 0,
"type": "solid"
},
"areaStyle": {
"opacity": 0
},
"zlevel": 0,
"z": 0
}
],
"legend": [
{
"data": [
"GDP"
],
"selected": {},
"show": true,
"padding": 5,
"itemGap": 10,
"itemWidth": 25,
"itemHeight": 14,
"backgroundColor": "transparent",
"borderColor": "#ccc",
"borderWidth": 1,
"borderRadius": 0,
"pageButtonItemGap": 5,
"pageButtonPosition": "end",
"pageFormatter": "{current}/{total}",
"pageIconColor": "#2f4554",
"pageIconInactiveColor": "#aaa",
"pageIconSize": 15,
"animationDurationUpdate": 800,
"selector": false,
"selectorPosition": "auto",
"selectorItemGap": 7,
"selectorButtonGap": 10
}
],
"tooltip": {
"show": true,
"trigger": "item",
"triggerOn": "mousemove|click",
"axisPointer": {
"type": "line"
},
"showContent": true,
"alwaysShowContent": false,
"showDelay": 0,
"hideDelay": 100,
"enterable": false,
"confine": false,
"appendToBody": false,
"transitionDuration": 0.4,
"textStyle": {
"fontSize": 14
},
"borderWidth": 0,
"padding": 5,
"order": "seriesAsc"
},
"xAxis": [
{
"show": true,
"scale": false,
"nameLocation": "end",
"nameGap": 15,
"gridIndex": 0,
"inverse": false,
"offset": 0,
"splitNumber": 5,
"minInterval": 0,
"splitLine": {
"show": true,
"lineStyle": {
"show": true,
"width": 1,
"opacity": 1,
"curveness": 0,
"type": "solid"
}
},
"data": [
"\u4e2d\u56fd",
"\u7f8e\u56fd",
"\u82f1\u56fd"
]
}
],
"yAxis": [
{
"show": true,
"scale": false,
"nameLocation": "end",
"nameGap": 15,
"gridIndex": 0,
"inverse": false,
"offset": 0,
"splitNumber": 5,
"minInterval": 0,
"splitLine": {
"show": true,
"lineStyle": {
"show": true,
"width": 1,
"opacity": 1,
"curveness": 0,
"type": "solid"
}
}
}
],
"title": [
{
"show": true,
"text": "GDP\u5c55\u793a",
"target": "blank",
"subtarget": "blank",
"left": "center",
"bottom": "1%",
"padding": 5,
"itemGap": 10,
"textAlign": "auto",
"textVerticalAlign": "auto",
"triggerEvent": false
}
],
"toolbox": {
"show": true,
"orient": "horizontal",
"itemSize": 15,
"itemGap": 10,
"left": "80%",
"feature": {
"saveAsImage": {
"type": "png",
"backgroundColor": "auto",
"connectedBackgroundColor": "#fff",
"show": true,
"title": "\u4fdd\u5b58\u4e3a\u56fe\u7247",
"pixelRatio": 1
},
"restore": {
"show": true,
"title": "\u8fd8\u539f"
},
"dataView": {
"show": true,
"title": "\u6570\u636e\u89c6\u56fe",
"readOnly": false,
"lang": [
"\u6570\u636e\u89c6\u56fe",
"\u5173\u95ed",
"\u5237\u65b0"
],
"backgroundColor": "#fff",
"textareaColor": "#fff",
"textareaBorderColor": "#333",
"textColor": "#000",
"buttonColor": "#c23531",
"buttonTextColor": "#fff"
},
"dataZoom": {
"show": true,
"title": {
"zoom": "\u533a\u57df\u7f29\u653e",
"back": "\u533a\u57df\u7f29\u653e\u8fd8\u539f"
},
"icon": {},
"filterMode": "filter"
},
"magicType": {
"show": true,
"type": [
"line",
"bar",
"stack",
"tiled"
],
"title": {
"line": "\u5207\u6362\u4e3a\u6298\u7ebf\u56fe",
"bar": "\u5207\u6362\u4e3a\u67f1\u72b6\u56fe",
"stack": "\u5207\u6362\u4e3a\u5806\u53e0",
"tiled": "\u5207\u6362\u4e3a\u5e73\u94fa"
},
"icon": {}
}
}
},
"visualMap": {
"show": true,
"type": "continuous",
"min": 0,
"max": 100,
"inRange": {
"color": [
"#50a3ba",
"#eac763",
"#d94e5d"
]
},
"calculable": true,
"inverse": false,
"splitNumber": 5,
"hoverLink": true,
"orient": "vertical",
"padding": 5,
"showLabel": true,
"itemWidth": 20,
"itemHeight": 140,
"borderWidth": 0
}
};
chart_e93ec85d48b54bf3a84f485e3ded8ac4.setOption(option_e93ec85d48b54bf3a84f485e3ded8ac4);
</script>
</body>
</html>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册