Python绘制折线图之可视化神器pyecharts(一)

目录

折线图介绍

折线图模板系列

双折线图(气温最高最低温度趋势显示)

面积折线图(紧贴Y轴)

简单折线图(无动态和数据标签)

连接空白数据折线图

对数轴折线图示例

折线图堆叠(适合多个折线图展示)

二维曲线折线图(两个数据)

多维度折线图(颜色对比)

阶梯折线图

js高渲染折线图

每文一语


折线图介绍

折线图和柱状图一样是我们日常可视化最多的一个图例,当然它的优势和适用场景相信大家肯定不陌生,要想快速的得出趋势,抓住趋势二字,就会很快的想到要用折线图来表示了。折线图是通过直线将这些点按照某种顺序连接起来形成的图,适用于数据在一个有序的因变量上的变化,它的特点是反应事物随类别而变化的趋势,可以清晰展现数据的增减趋势、增减的速率、增减的规律、峰值等特征。

优点

  • 能很好的展现沿某个维度的变化趋势
  • 能比较多组数据在同一个维度上的趋势
  • 适合展现较大数据集

缺点:每张图上不适合展示太多折线

折线图模板系列

双折线图(气温最高最低温度趋势显示)

双折线图在一张图里面显示,肯定有一个相同的维度,然后有两个不同的数据集。比如一天的温度有最高的和最低的温度,我们就可以用这个来作为展示了。


  
  1. import pyecharts.options as opts
  2. from pyecharts.charts import Line
  3. week_name_list = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
  4. high_temperature = [11, 11, 15, 13, 12, 13, 10]
  5. low_temperature = [1, -2, 2, 5, 3, 2, 0]
  6. (
  7. Line(init_opts=opts.InitOpts(width="1000px", height="600px"))
  8. .add_xaxis(xaxis_data=week_name_list)
  9. .add_yaxis(
  10. series_name="最高气温",
  11. y_axis=high_temperature,
  12. # 显示最大值和最小值
  13. # markpoint_opts=opts.MarkPointOpts(
  14. # data=[
  15. # opts.MarkPointItem(type_="max", name="最大值"),
  16. # opts.MarkPointItem(type_="min", name="最小值"),
  17. # ]
  18. # ),
  19. # 显示平均值
  20. # markline_opts=opts.MarkLineOpts(
  21. # data=[opts.MarkLineItem(type_="average", name="平均值")]
  22. # ),
  23. )
  24. .add_yaxis(
  25. series_name="最低气温",
  26. y_axis=low_temperature,
  27. # 设置刻度标签
  28. # markpoint_opts=opts.MarkPointOpts(
  29. # data=[opts.MarkPointItem(value=-2, name="周最低", x=1, y=-1.5)]
  30. # ),
  31. # markline_opts=opts.MarkLineOpts(
  32. # data=[
  33. # opts.MarkLineItem(type_="average", name="平均值"),
  34. # opts.MarkLineItem(symbol="none", x="90%", y="max"),
  35. # opts.MarkLineItem(symbol="circle", type_="max", name="最高点"),
  36. # ]
  37. # ),
  38. )
  39. .set_global_opts(
  40. title_opts=opts.TitleOpts(title="未来一周气温变化", subtitle="副标题"),
  41. # tooltip_opts=opts.TooltipOpts(trigger="axis"),
  42. # toolbox_opts=opts.ToolboxOpts(is_show=True),
  43. xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
  44. )
  45. .render("最低最高温度折线图.html")
  46. )
  47. print("图表已生成!请查收!")

 

 

面积折线图(紧贴Y轴)

还记得二重积分吗,面积代表什么?有时候我们就想要看谁围出来的面积大,这个在物理的实际运用中比较常见,下面来看看效果吧。


  
  1. import pyecharts.options as opts
  2. from pyecharts.charts import Line
  3. from pyecharts.faker import Faker
  4. from pyecharts.globals import ThemeType
  5. c = (
  6. Line({"theme": ThemeType.MACARONS})
  7. .add_xaxis(Faker.choose())
  8. .add_yaxis("商家A", Faker.values(), is_smooth=True)
  9. .add_yaxis("商家B", Faker.values(), is_smooth=True)
  10. .set_series_opts(
  11. areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
  12. label_opts=opts.LabelOpts(is_show=False),
  13. )
  14. .set_global_opts(
  15. title_opts=opts.TitleOpts(title="标题"),
  16. xaxis_opts=opts.AxisOpts(
  17. axistick_opts=opts.AxisTickOpts(is_align_with_label=True),
  18. is_scale=False,
  19. boundary_gap=False,
  20. name='类别',
  21. name_location='middle',
  22. name_gap=30, # 标签与轴线之间的距离,默认为20,最好不要设置20
  23. name_textstyle_opts=opts.TextStyleOpts(
  24. font_family='Times New Roman',
  25. font_size=16 # 标签字体大小
  26. )),
  27. yaxis_opts=opts.AxisOpts(
  28. name='数量',
  29. name_location='middle',
  30. name_gap=30,
  31. name_textstyle_opts=opts.TextStyleOpts(
  32. font_family='Times New Roman',
  33. font_size=16
  34. # font_weight='bolder',
  35. )),
  36. # toolbox_opts=opts.ToolboxOpts() # 工具选项
  37. )
  38. .render("面积折线图-紧贴Y轴.html")
  39. )
  40. print("请查收!")

 

简单折线图(无动态和数据标签)

此模板和Excel里面的可视化差不多,没有一点功能元素,虽然它是最简洁的,但是我们可以通过这个进行改动,在上面创作的画作。


  
  1. import pyecharts.options as opts
  2. from pyecharts.charts import Line
  3. from pyecharts.globals import ThemeType
  4. x_data = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
  5. y_data = [820, 932, 901, 934, 1290, 1330, 1320]
  6. (
  7. Line({"theme": ThemeType.MACARONS})
  8. .set_global_opts(
  9. tooltip_opts=opts.TooltipOpts(is_show=False),
  10. xaxis_opts=opts.AxisOpts(
  11. name='类别',
  12. name_location='middle',
  13. name_gap=30, # 标签与轴线之间的距离,默认为20,最好不要设置20
  14. name_textstyle_opts=opts.TextStyleOpts(
  15. font_family='Times New Roman',
  16. font_size=16 # 标签字体大小
  17. )),
  18. yaxis_opts=opts.AxisOpts(
  19. type_="value",
  20. axistick_opts=opts.AxisTickOpts(is_show=True),
  21. splitline_opts=opts.SplitLineOpts(is_show=True),
  22. name='数量',
  23. name_location='middle',
  24. name_gap=30,
  25. name_textstyle_opts=opts.TextStyleOpts(
  26. font_family='Times New Roman',
  27. font_size=16
  28. # font_weight='bolder',
  29. )),
  30. )
  31. .add_xaxis(xaxis_data=x_data)
  32. .add_yaxis(
  33. series_name="",
  34. y_axis=y_data,
  35. symbol="emptyCircle",
  36. is_symbol_show=True,
  37. label_opts=opts.LabelOpts(is_show=False),
  38. )
  39. .render("简单折线图.html")
  40. )

 

 

连接空白数据折线图

有时候我们在处理数据的时候,发现有些类别的数据缺失了,这个时候我们想要它可以自动连接起来,那么这个模板就可以用到了。


  
  1. import pyecharts.options as opts
  2. from pyecharts.charts import Line
  3. from pyecharts.faker import Faker
  4. from pyecharts.globals import ThemeType
  5. y = Faker.values()
  6. y[3], y[5] = None, None
  7. c = (
  8. Line({"theme": ThemeType.WONDERLAND})
  9. .add_xaxis(Faker.choose())
  10. .add_yaxis("商家A", y, is_connect_nones=True)
  11. .set_global_opts(title_opts=opts.TitleOpts(title="标题"),
  12. xaxis_opts=opts.AxisOpts(
  13. name='类别',
  14. name_location='middle',
  15. name_gap=30, # 标签与轴线之间的距离,默认为20,最好不要设置20
  16. name_textstyle_opts=opts.TextStyleOpts(
  17. font_family='Times New Roman',
  18. font_size=16 # 标签字体大小
  19. )),
  20. yaxis_opts=opts.AxisOpts(
  21. name='数量',
  22. name_location='middle',
  23. name_gap=30,
  24. name_textstyle_opts=opts.TextStyleOpts(
  25. font_family='Times New Roman',
  26. font_size=16
  27. # font_weight='bolder',
  28. )), )
  29. # toolbox_opts=opts.ToolboxOpts() # 工具选项)
  30. .render("数据缺失折线图.html")
  31. )

 

对数轴折线图示例

此图例未必用的上,当然也可以作为一个模板分享于此。

 


  
  1. import pyecharts.options as opts
  2. from pyecharts.charts import Line
  3. x_data = ["一", "二", "三", "四", "五", "六", "七", "八", "九"]
  4. y_data_3 = [1, 3, 9, 27, 81, 247, 741, 2223, 6669]
  5. y_data_2 = [1, 2, 4, 8, 16, 32, 64, 128, 256]
  6. y_data_05 = [1 / 2, 1 / 4, 1 / 8, 1 / 16, 1 / 32, 1 / 64, 1 / 128, 1 / 256, 1 / 512]
  7. (
  8. Line(init_opts=opts.InitOpts(width="1200px", height="600px"))
  9. .add_xaxis(xaxis_data=x_data)
  10. .add_yaxis(
  11. series_name="1/2的指数",
  12. y_axis=y_data_05,
  13. linestyle_opts=opts.LineStyleOpts(width=2),
  14. )
  15. .add_yaxis(
  16. series_name="2的指数", y_axis=y_data_2, linestyle_opts=opts.LineStyleOpts(width=2)
  17. )
  18. .add_yaxis(
  19. series_name="3的指数", y_axis=y_data_3, linestyle_opts=opts.LineStyleOpts(width=2)
  20. )
  21. .set_global_opts(
  22. title_opts=opts.TitleOpts(title="对数轴示例", pos_left="center"),
  23. tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{a} <br/>{b} : {c}"),
  24. legend_opts=opts.LegendOpts(pos_left="left"),
  25. xaxis_opts=opts.AxisOpts(type_="category", name="x"),
  26. yaxis_opts=opts.AxisOpts(
  27. type_="log",
  28. name="y",
  29. splitline_opts=opts.SplitLineOpts(is_show=True),
  30. is_scale=True,
  31. ),
  32. )
  33. .render("对数轴折线图.html")
  34. )

 

折线图堆叠(适合多个折线图展示)

多个折线图展示要注意的是,数据量不能过于的接近,不然密密麻麻的折线,反而让人看起来不舒服。


  
  1. import pyecharts.options as opts
  2. from pyecharts.charts import Line
  3. from pyecharts.globals import ThemeType
  4. x_data = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
  5. y_data = [820, 932, 901, 934, 1290, 1330, 1320]
  6. (
  7. Line({"theme": ThemeType.MACARONS})
  8. .add_xaxis(xaxis_data=x_data)
  9. .add_yaxis(
  10. series_name="邮件营销",
  11. stack="总量",
  12. y_axis=[120, 132, 101, 134, 90, 230, 210],
  13. label_opts=opts.LabelOpts(is_show=False),
  14. )
  15. .add_yaxis(
  16. series_name="联盟广告",
  17. stack="总量",
  18. y_axis=[220, 182, 191, 234, 290, 330, 310],
  19. label_opts=opts.LabelOpts(is_show=False),
  20. )
  21. .add_yaxis(
  22. series_name="视频广告",
  23. stack="总量",
  24. y_axis=[150, 232, 201, 154, 190, 330, 410],
  25. label_opts=opts.LabelOpts(is_show=False),
  26. )
  27. .add_yaxis(
  28. series_name="直接访问",
  29. stack="总量",
  30. y_axis=[320, 332, 301, 334, 390, 330, 320],
  31. label_opts=opts.LabelOpts(is_show=False),
  32. )
  33. .add_yaxis(
  34. series_name="搜索引擎",
  35. stack="总量",
  36. y_axis=[820, 932, 901, 934, 1290, 1330, 1320],
  37. label_opts=opts.LabelOpts(is_show=False),
  38. )
  39. .set_global_opts(
  40. title_opts=opts.TitleOpts(title="折线图堆叠"),
  41. tooltip_opts=opts.TooltipOpts(trigger="axis"),
  42. yaxis_opts=opts.AxisOpts(
  43. type_="value",
  44. axistick_opts=opts.AxisTickOpts(is_show=True),
  45. splitline_opts=opts.SplitLineOpts(is_show=True),
  46. name='数量',
  47. name_location='middle',
  48. name_gap=40,
  49. name_textstyle_opts=opts.TextStyleOpts(
  50. font_family='Times New Roman',
  51. font_size=16
  52. # font_weight='bolder',
  53. )),
  54. xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False,
  55. name='类别',
  56. name_location='middle',
  57. name_gap=30, # 标签与轴线之间的距离,默认为20,最好不要设置20
  58. name_textstyle_opts=opts.TextStyleOpts(
  59. font_family='Times New Roman',
  60. font_size=16 # 标签字体大小
  61. )),
  62. )
  63. .render("折线图堆叠.html")
  64. )

 

二维曲线折线图(两个数据)

有时候需要在一个图里面进行对比,那么我们应该如何呈现一个丝滑般的曲线折线图呢?看看这个


  
  1. import pyecharts.options as opts
  2. from pyecharts.charts import Line
  3. from pyecharts.faker import Faker
  4. c = (
  5. Line()
  6. .add_xaxis(Faker.choose())
  7. .add_yaxis("商家A", Faker.values(), is_smooth=True) # 如果不想变成曲线就删除即可
  8. .add_yaxis("商家B", Faker.values(), is_smooth=True)
  9. .set_global_opts(title_opts=opts.TitleOpts(title="标题"),
  10. xaxis_opts=opts.AxisOpts(
  11. name='类别',
  12. name_location='middle',
  13. name_gap=30, # 标签与轴线之间的距离,默认为20,最好不要设置20
  14. name_textstyle_opts=opts.TextStyleOpts(
  15. font_family='Times New Roman',
  16. font_size=16 # 标签字体大小
  17. )),
  18. yaxis_opts=opts.AxisOpts(
  19. name='数量',
  20. name_location='middle',
  21. name_gap=30,
  22. name_textstyle_opts=opts.TextStyleOpts(
  23. font_family='Times New Roman',
  24. font_size=16
  25. # font_weight='bolder',
  26. )),
  27. # toolbox_opts=opts.ToolboxOpts() # 工具选项
  28. )
  29. .render("二维折线图.html")
  30. )

 

 

多维度折线图(颜色对比)

次模板的最大的好处就是可以移动鼠标智能显示数据


  
  1. import pyecharts.options as opts
  2. from pyecharts.charts import Line
  3. # 将在 v1.1.0 中更改
  4. from pyecharts.commons.utils import JsCode
  5. js_formatter = """function (params) {
  6. console.log(params);
  7. return '降水量 ' + params.value + (params.seriesData.length ? ':' + params.seriesData[0].data : '');
  8. }"""
  9. (
  10. Line(init_opts=opts.InitOpts(width="1200px", height="600px"))
  11. .add_xaxis(
  12. xaxis_data=[
  13. "2016-1",
  14. "2016-2",
  15. "2016-3",
  16. "2016-4",
  17. "2016-5",
  18. "2016-6",
  19. "2016-7",
  20. "2016-8",
  21. "2016-9",
  22. "2016-10",
  23. "2016-11",
  24. "2016-12",
  25. ]
  26. )
  27. .extend_axis(
  28. xaxis_data=[
  29. "2015-1",
  30. "2015-2",
  31. "2015-3",
  32. "2015-4",
  33. "2015-5",
  34. "2015-6",
  35. "2015-7",
  36. "2015-8",
  37. "2015-9",
  38. "2015-10",
  39. "2015-11",
  40. "2015-12",
  41. ],
  42. xaxis=opts.AxisOpts(
  43. type_="category",
  44. axistick_opts=opts.AxisTickOpts(is_align_with_label=True),
  45. axisline_opts=opts.AxisLineOpts(
  46. is_on_zero=False, linestyle_opts=opts.LineStyleOpts(color="#6e9ef1")
  47. ),
  48. axispointer_opts=opts.AxisPointerOpts(
  49. is_show=True, label=opts.LabelOpts(formatter=JsCode(js_formatter))
  50. ),
  51. ),
  52. )
  53. .add_yaxis(
  54. series_name="2015 降水量",
  55. is_smooth=True,
  56. symbol="emptyCircle",
  57. is_symbol_show=False,
  58. # xaxis_index=1,
  59. color="#d14a61",
  60. y_axis=[2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3],
  61. label_opts=opts.LabelOpts(is_show=False),
  62. linestyle_opts=opts.LineStyleOpts(width=2),
  63. )
  64. .add_yaxis(
  65. series_name="2016 降水量",
  66. is_smooth=True,
  67. symbol="emptyCircle",
  68. is_symbol_show=False,
  69. color="#6e9ef1",
  70. y_axis=[3.9, 5.9, 11.1, 18.7, 48.3, 69.2, 231.6, 46.6, 55.4, 18.4, 10.3, 0.7],
  71. label_opts=opts.LabelOpts(is_show=False),
  72. linestyle_opts=opts.LineStyleOpts(width=2),
  73. )
  74. .set_global_opts(
  75. legend_opts=opts.LegendOpts(),
  76. tooltip_opts=opts.TooltipOpts(trigger="none", axis_pointer_type="cross"),
  77. xaxis_opts=opts.AxisOpts(
  78. type_="category",
  79. axistick_opts=opts.AxisTickOpts(is_align_with_label=True),
  80. axisline_opts=opts.AxisLineOpts(
  81. is_on_zero=False, linestyle_opts=opts.LineStyleOpts(color="#d14a61")
  82. ),
  83. axispointer_opts=opts.AxisPointerOpts(
  84. is_show=True, label=opts.LabelOpts(formatter=JsCode(js_formatter))
  85. ),
  86. ),
  87. yaxis_opts=opts.AxisOpts(
  88. type_="value",
  89. splitline_opts=opts.SplitLineOpts(
  90. is_show=True, linestyle_opts=opts.LineStyleOpts(opacity=1)
  91. ),
  92. ),
  93. )
  94. .render("多维颜色多维折线图.html")
  95. )

 

阶梯折线图


  
  1. import pyecharts.options as opts
  2. from pyecharts.charts import Line
  3. from pyecharts.faker import Faker
  4. from pyecharts.globals import ThemeType
  5. c = (
  6. Line({"theme": ThemeType.MACARONS})
  7. .add_xaxis(Faker.choose())
  8. .add_yaxis("商家A", Faker.values(), is_step=True)
  9. .set_global_opts(title_opts=opts.TitleOpts(title="标题"),
  10. xaxis_opts=opts.AxisOpts(
  11. name='类别',
  12. name_location='middle',
  13. name_gap=30, # 标签与轴线之间的距离,默认为20,最好不要设置20
  14. name_textstyle_opts=opts.TextStyleOpts(
  15. font_family='Times New Roman',
  16. font_size=16 # 标签字体大小
  17. )),
  18. yaxis_opts=opts.AxisOpts(
  19. name='数量',
  20. name_location='middle',
  21. name_gap=30,
  22. name_textstyle_opts=opts.TextStyleOpts(
  23. font_family='Times New Roman',
  24. font_size=16
  25. # font_weight='bolder',
  26. )),
  27. # toolbox_opts=opts.ToolboxOpts() # 工具选项
  28. )
  29. .render("阶梯折线图.html")
  30. )

 

 

js高渲染折线图

里面的渲染效果相当好看,可以适用于炫酷的展示,数据集可以展示也可以不展示,在相应的位置更改参数即可。


  
  1. import pyecharts.options as opts
  2. from pyecharts.charts import Line
  3. from pyecharts.commons.utils import JsCode
  4. x_data = ["14", "15", "16", "17", "18", "19", "20", "21", "22", "23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40"]
  5. y_data = [393, 438, 485, 631, 689, 824, 987, 1000, 1100, 1200,1500,1000,1700,1900,2000,500,1200,1300,1500,1800,1500,1900,1700,1000,1900,1800,2100,1600,2200,2300]
  6. background_color_js = (
  7. "new echarts.graphic.LinearGradient(0, 0, 0, 1, "
  8. "[{offset: 0, color: '#c86589'}, {offset: 1, color: '#06a7ff'}], false)"
  9. )
  10. area_color_js = (
  11. "new echarts.graphic.LinearGradient(0, 0, 0, 1, "
  12. "[{offset: 0, color: '#eb64fb'}, {offset: 1, color: '#3fbbff0d'}], false)"
  13. )
  14. c = (
  15. Line(init_opts=opts.InitOpts(bg_color=JsCode(background_color_js)))
  16. .add_xaxis(xaxis_data=x_data)
  17. .add_yaxis(
  18. series_name="注册总量",
  19. y_axis=y_data,
  20. is_smooth=True,
  21. is_symbol_show=True,
  22. symbol="circle",
  23. symbol_size=6,
  24. linestyle_opts=opts.LineStyleOpts(color="#fff"),
  25. label_opts=opts.LabelOpts(is_show=True, position="top", color="white"),
  26. itemstyle_opts=opts.ItemStyleOpts(
  27. color="red", border_color="#fff", border_width=3
  28. ),
  29. tooltip_opts=opts.TooltipOpts(is_show=False),
  30. areastyle_opts=opts.AreaStyleOpts(color=JsCode(area_color_js), opacity=1),
  31. )
  32. .set_global_opts(
  33. title_opts=opts.TitleOpts(
  34. title="OCTOBER 2015",
  35. pos_bottom="5%",
  36. pos_left="center",
  37. title_textstyle_opts=opts.TextStyleOpts(color="#fff", font_size=16),
  38. ),
  39. xaxis_opts=opts.AxisOpts(
  40. type_="category",
  41. boundary_gap=False,
  42. axislabel_opts=opts.LabelOpts(margin=30, color="#ffffff63"),
  43. axisline_opts=opts.AxisLineOpts(is_show=False),
  44. axistick_opts=opts.AxisTickOpts(
  45. is_show=True,
  46. length=25,
  47. linestyle_opts=opts.LineStyleOpts(color="#ffffff1f"),
  48. ),
  49. splitline_opts=opts.SplitLineOpts(
  50. is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")
  51. ),
  52. ),
  53. yaxis_opts=opts.AxisOpts(
  54. type_="value",
  55. position="right",
  56. axislabel_opts=opts.LabelOpts(margin=20, color="#ffffff63"),
  57. axisline_opts=opts.AxisLineOpts(
  58. linestyle_opts=opts.LineStyleOpts(width=2, color="#fff")
  59. ),
  60. axistick_opts=opts.AxisTickOpts(
  61. is_show=True,
  62. length=15,
  63. linestyle_opts=opts.LineStyleOpts(color="#ffffff1f"),
  64. ),
  65. splitline_opts=opts.SplitLineOpts(
  66. is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")
  67. ),
  68. ),
  69. legend_opts=opts.LegendOpts(is_show=False),
  70. )
  71. .render("高渲染.html")
  72. )

 

所有图表均可配置,无论是字体的大小,还是颜色,还是背景都可以自己配置哟!下期文章我们继续探索折线图的魅力哟!

 

每文一语

万物皆对象,一切可配置!

文章来源: blog.csdn.net,作者:王小王-123,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_47723732/article/details/113868461

(完)