{ "cells": [ { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
RigonYearValue
0WORLD195046.5
1WORLD195147.1
2WORLD195248.2
3WORLD195348.8
4WORLD195449.6
............
67WORLD201772.3
68WORLD201872.6
69WORLD201972.8
70WORLD202072.0
71WORLD202171.0
\n", "

72 rows × 3 columns

\n", "
" ], "text/plain": [ " Rigon Year Value\n", "0 WORLD 1950 46.5\n", "1 WORLD 1951 47.1\n", "2 WORLD 1952 48.2\n", "3 WORLD 1953 48.8\n", "4 WORLD 1954 49.6\n", ".. ... ... ...\n", "67 WORLD 2017 72.3\n", "68 WORLD 2018 72.6\n", "69 WORLD 2019 72.8\n", "70 WORLD 2020 72.0\n", "71 WORLD 2021 71.0\n", "\n", "[72 rows x 3 columns]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "LE=pd.read_csv(\"LE.csv\")\n", "LE" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'C:\\\\Users\\\\1\\\\Desktop\\\\01.html'" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "y = LE[\"Value\"]\n", "x= LE[\"Year\"]\n", "\n", "import pyecharts.options as opts\n", "from pyecharts.charts import Line\n", "\n", "# 将在 v1.1.0 中更改\n", "from pyecharts.commons.utils import JsCode\n", "\n", "\n", "\n", "js_formatter = \"\"\"function (params) {\n", " console.log(params);\n", " return 'Life expectancy ' + params.value + (params.seriesData.length ? ':' + params.seriesData[0].data : '');\n", " }\"\"\"\n", "\n", "(\n", " Line()\n", " .add_xaxis(\n", " xaxis_data=x\n", " )\n", " .extend_axis(\n", " xaxis_data=x,\n", " xaxis=opts.AxisOpts(\n", " type_=\"category\",\n", " axistick_opts=opts.AxisTickOpts(is_align_with_label=True),\n", " axisline_opts=opts.AxisLineOpts(\n", " is_on_zero=False, linestyle_opts=opts.LineStyleOpts(color=\"#6e9ef1\")\n", " ),\n", " axispointer_opts=opts.AxisPointerOpts(\n", " is_show=True, label=opts.LabelOpts(formatter=JsCode(js_formatter))\n", " ),\n", " ),\n", " )\n", " \n", " .add_yaxis(\n", " series_name=\"Life expectancy\",\n", " is_smooth=True,\n", " symbol=\"emptyCircle\",\n", " is_symbol_show=False,\n", " color=\"#6e9ef1\",\n", " y_axis=y,\n", " label_opts=opts.LabelOpts(is_show=False),\n", " linestyle_opts=opts.LineStyleOpts(width=2),\n", " )\n", " .set_global_opts(\n", " title_opts=opts.TitleOpts( title=\"Change in world average life expectancy, 1950-2021\",pos_left=\"center\",pos_top=\"bottom\"),\n", " legend_opts=opts.LegendOpts(),\n", " tooltip_opts=opts.TooltipOpts(trigger=\"none\", axis_pointer_type=\"cross\"),\n", " xaxis_opts=opts.AxisOpts(\n", " \n", " type_=\"category\",\n", " axistick_opts=opts.AxisTickOpts(is_align_with_label=True),\n", " axisline_opts=opts.AxisLineOpts(\n", " is_on_zero=False, linestyle_opts=opts.LineStyleOpts(color=\"#d14a61\")\n", " ),\n", " axispointer_opts=opts.AxisPointerOpts(\n", " is_show=True, label=opts.LabelOpts(formatter=JsCode(js_formatter))\n", " ),\n", " ),\n", " yaxis_opts=opts.AxisOpts(\n", " min_=40,\n", " max_=80,\n", " type_=\"value\",\n", " splitline_opts=opts.SplitLineOpts(\n", " is_show=True, linestyle_opts=opts.LineStyleOpts(opacity=1)\n", " ),\n", " ),\n", " )\n", " .render(\"01.html\")\n", ")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'C:\\\\Users\\\\1\\\\Desktop\\\\02.html'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "CA=pd.read_csv(\"CA.csv\")\n", "x=CA[\"Year1\"]\n", "y1=CA[\"Live Expectancy1\"]\n", "y2=CA[\"Live Expectancy2\"]\n", "\n", "import pyecharts.options as opts\n", "from pyecharts.charts import Line\n", "\n", "# 将在 v1.1.0 中更改\n", "from pyecharts.commons.utils import JsCode\n", "\n", "\"\"\"\n", "Gallery 使用 pyecharts 1.0.0\n", "参考地址: https://echarts.apache.org/examples/editor.html?c=multiple-x-axis\n", "\n", "目前无法实现的功能:\n", "\n", "1、暂无\n", "\"\"\"\n", "\n", "js_formatter = \"\"\"function (params) {\n", " console.log(params);\n", " return 'live expectancy ' + params.value + (params.seriesData.length ? ':' + params.seriesData[0].data : '');\n", " }\"\"\"\n", "\n", "(\n", " Line()\n", " .add_xaxis(\n", " x\n", " )\n", "\n", " .add_yaxis(\n", " series_name=\"China\",\n", " is_smooth=True,\n", " symbol=\"emptyCircle\",\n", " is_symbol_show=False,\n", " # xaxis_index=1,\n", " color=\"#d14a61\",\n", " y_axis=y1,\n", " label_opts=opts.LabelOpts(is_show=False),\n", " linestyle_opts=opts.LineStyleOpts(width=2),\n", " )\n", " .add_yaxis(\n", " series_name=\"America\",\n", " is_smooth=True,\n", " symbol=\"emptyCircle\",\n", " is_symbol_show=False,\n", " color=\"#6e9ef1\",\n", " y_axis=y2,\n", " label_opts=opts.LabelOpts(is_show=False),\n", " linestyle_opts=opts.LineStyleOpts(width=2),\n", " )\n", " .set_global_opts(\n", " title_opts=opts.TitleOpts(title=\"Comparison of average life expectancy between China and America, 2006-2021\",pos_left=\"center\",pos_top=\"bottom\"),\n", " legend_opts=opts.LegendOpts(),\n", " tooltip_opts=opts.TooltipOpts(trigger=\"none\", axis_pointer_type=\"cross\"),\n", " xaxis_opts=opts.AxisOpts(\n", " type_=\"category\",\n", " axistick_opts=opts.AxisTickOpts(is_align_with_label=True),\n", " axisline_opts=opts.AxisLineOpts(\n", " is_on_zero=False, linestyle_opts=opts.LineStyleOpts(color=\"#d14a61\")\n", " ),\n", " axispointer_opts=opts.AxisPointerOpts(\n", " is_show=True, label=opts.LabelOpts(formatter=JsCode(js_formatter))\n", " ),\n", " ),\n", " yaxis_opts=opts.AxisOpts(\n", " min_=70,\n", " max_=80,\n", " type_=\"value\",\n", " splitline_opts=opts.SplitLineOpts(\n", " is_show=True, linestyle_opts=opts.LineStyleOpts(opacity=1)\n", " ),\n", " ),\n", " )\n", " .render(\"02.html\")\n", ")\n" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'C:\\\\Users\\\\1\\\\Desktop\\\\03.html'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "data_frame=pd.read_excel(\"sj1.xls\")\n", "data_frame\n", "from pyecharts import options as opts\n", "import matplotlib as plt\n", "from pyecharts.charts import Scatter\n", "from pyecharts.commons.utils import JsCode\n", "\n", "x=data_frame[\"Explained by: GDP per capita\"]\n", "y=data_frame[\"Happiness score\"]\n", "y1=data_frame[\"Country\"]\n", "\n", "\n", "c = (\n", " Scatter()\n", " .add_xaxis(x)\n", " .add_yaxis(\"\", y,\n", " label_opts=opts.LabelOpts(is_show=False)\n", " \n", " )\n", " .set_global_opts(\n", " yaxis_opts=opts.AxisOpts(\n", " name='Happiness score',\n", " name_location='middle',\n", " name_gap=30,\n", " name_textstyle_opts=opts.TextStyleOpts(\n", " font_family= 'Times New Roman',\n", " font_size=20,\n", " color='black',\n", "# font_weight='bolder',\n", " )),\n", " xaxis_opts=opts.AxisOpts(\n", " name='GDP per capita',\n", " name_location='middle',\n", " name_gap=20,\n", "# x轴名称的格式配置\n", " name_textstyle_opts=opts.TextStyleOpts(\n", " font_family= 'Times New Roman',\n", " font_size=20,\n", " )),\n", " title_opts=opts.TitleOpts(title=\"Scatter plot of GDP per capita and Happiness score\",pos_left=\"center\",pos_top=\"top\"),\n", " \n", " visualmap_opts=opts.VisualMapOpts(max_=8,min_=2),\n", " )\n", " .render(\"03.html\")\n", ")\n", "\n", "c\n", "\n" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'C:\\\\Users\\\\1\\\\Desktop\\\\04.html'" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "x=data_frame[\"Explained by: Social support\"]\n", "y=data_frame[\"Happiness score\"]\n", "\n", "\n", "\n", "c = (\n", " Scatter()\n", " .add_xaxis(x)\n", " .add_yaxis(\"\", y,\n", " label_opts=opts.LabelOpts(is_show=False)\n", " \n", " )\n", " .set_global_opts(\n", " yaxis_opts=opts.AxisOpts(\n", " name='Happiness score',\n", " name_location='middle',\n", " name_gap=30,\n", " name_textstyle_opts=opts.TextStyleOpts(\n", " font_family= 'Times New Roman',\n", " font_size=20,\n", " color='black',\n", "# font_weight='bolder',\n", " )),\n", " xaxis_opts=opts.AxisOpts(\n", " name='Social support',\n", " name_location='middle',\n", " name_gap=20,\n", "# x轴名称的格式配置\n", " name_textstyle_opts=opts.TextStyleOpts(\n", " font_family= 'Times New Roman',\n", " font_size=20,\n", " )),\n", " title_opts=opts.TitleOpts(title=\"Scatter plot of Social support and Happiness score\",pos_left=\"center\",pos_top=\"top\"),\n", " \n", " visualmap_opts=opts.VisualMapOpts(max_=8,min_=2),\n", " )\n", " .render(\"04.html\")\n", ")\n", "\n", "c\n" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'C:\\\\Users\\\\1\\\\Desktop\\\\05.html'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x=data_frame[\"Explained by: Freedom to make life choices\"]\n", "y=data_frame[\"Happiness score\"]\n", "\n", "\n", "\n", "c = (\n", " Scatter()\n", " .add_xaxis(x)\n", " .add_yaxis(\"\", y,\n", " label_opts=opts.LabelOpts(is_show=False)\n", " \n", " )\n", " .set_global_opts(\n", " yaxis_opts=opts.AxisOpts(\n", " name='Happiness score',\n", " name_location='middle',\n", " name_gap=30,\n", " name_textstyle_opts=opts.TextStyleOpts(\n", " font_family= 'Times New Roman',\n", " font_size=20,\n", " color='black',\n", "# font_weight='bolder',\n", " )),\n", " xaxis_opts=opts.AxisOpts(\n", " name='Freedom to make life choices',\n", " name_location='middle',\n", " name_gap=20,\n", "# x轴名称的格式配置\n", " name_textstyle_opts=opts.TextStyleOpts(\n", " font_family= 'Times New Roman',\n", " font_size=20,\n", " )),\n", " title_opts=opts.TitleOpts(title=\"Scatter plot of Social support Freedom to make life choices and Happiness score\",pos_left=\"center\",pos_top=\"top\"),\n", " \n", " visualmap_opts=opts.VisualMapOpts(max_=8,min_=2),\n", " )\n", " .render(\"05.html\")\n", ")\n", "\n", "c" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "y1=CA[\"Life Ladder1\"]\n", "y2=CA[\"Life Ladder2\"]\n", "x1=CA[\"Live Expectancy1\"]\n", "\n", "\n", "c = (\n", " Scatter()\n", " .add_xaxis(x1)\n", " .add_yaxis(\"China\", y1,label_opts=opts.LabelOpts(is_show=False))\n", " .add_yaxis(\"America\",y2,label_opts=opts.LabelOpts(is_show=False))\n", " .set_global_opts(\n", " yaxis_opts=opts.AxisOpts(\n", " name='Happiness score',\n", " name_location='middle',\n", " name_gap=30,\n", " name_textstyle_opts=opts.TextStyleOpts(\n", " font_family= 'Times New Roman',\n", " font_size=20,\n", " color='black'),\n", " max_=7.8,\n", " min_=4,),\n", " xaxis_opts=opts.AxisOpts(\n", " name='Live Expectancy',\n", " name_location='middle',\n", " name_gap=30,\n", " name_textstyle_opts=opts.TextStyleOpts(\n", " font_family= 'Times New Roman',\n", " font_size=20,\n", " color='black'),\n", "\n", "max_=80,\n", "\n", "min_=74,\n", "\n", "),\n", " title_opts=opts.TitleOpts(title=\"The relationship between live expectancy and Happy Score\",pos_left=\"center\",pos_top=\"bottom\"),\n", " visualmap_opts=opts.VisualMapOpts(type_=\"size\", max_=8, min_=4),\n", " )\n", " .render(\"06.html\")\n", ")\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }