今天无意间刷到了一个跟Python可视化相关的头条,就点进去看看,这是一位作者编写的手绘风格的绘图工具,对可视化工作一直都是比较感兴趣的,所以这里就想直接上手使用一下。
GitHub地址在这里。首页截图如下:
最下方也给出了安装方式,应该是以Python3为基础编写的,所以Python2应该是安装不了的,所以这里我本地使用的话也是基于Python3来进行安装的,安装截图如下:
成功安装后就可以使用了,这里我借助的是jupyter-notebook来进行实践的,原因是这个库本身也是会有与web交互的地方的,所以我觉得jupyter可能更合适。下面是具体的实践:
【柱状图】
【折线图】
【柱状图+折线图】
当前,也是可以一张图里面同时绘制柱状图和折线图或者其他类型的图像的,这里简单举例:
from cutecharts.charts import Bar, Line
from cutecharts.faker import Faker
from cutecharts.components import Page
def bar_base() -> Bar:
chart = Bar("手绘风格的柱状图")
chart.set_options(labels=Faker.choose(), x_label="类别", y_label="数量")
chart.add_series("series-A", Faker.values())
return chart
def line_base() -> Line:
chart = Line("手绘风格的折线统计图")
chart.set_options(labels=Faker.choose(), x_label="种类", y_label="数量")
chart.add_series("series-A", Faker.values())
chart.add_series("series-B", Faker.values())
return chart
page = Page()
page.add(bar_base(),line_base())
page.render('both.html')
由于截图比较大,这里采用保存本地文件的形式,结果如下:
这里也可以对线条或者图形的颜色进行调整,如下:
【饼图】
可以修改字体,如下:
也可以绘制正常类型的饼图【非环状】:
【雷达图】
【散点图】
也可以调整散点的大小程度。
也可以将散点连成线,这个我觉得平时使用到的场景比较少。
最后贴出来完整的实现代码:
# -*- coding: utf-8 -*-
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:light
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.4'
# jupytext_version: 1.1.6
# kernelspec:
# display_name: python36
# language: python
# name: python36
# ---
# +
from cutecharts.charts import Bar, Line
from cutecharts.faker import Faker
from cutecharts.components import Page
def bar_base() -> Bar:
chart = Bar("手绘风格的柱状图")
chart.set_options(labels=Faker.choose(), x_label="类别", y_label="数量")
chart.add_series("series-A", Faker.values())
return chart
page = Page()
page.add(bar_base())
page.render_notebook()
# +
from cutecharts.charts import Bar, Line
from cutecharts.faker import Faker
from cutecharts.components import Page
def line_base() -> Line:
chart = Line("手绘风格的折线统计图")
chart.set_options(labels=Faker.choose(), x_label="种类", y_label="数量")
chart.add_series("series-A", Faker.values())
chart.add_series("series-B", Faker.values())
return chart
page = Page()
page.add(line_base())
page.render_notebook()
# +
from cutecharts.charts import Bar, Line
from cutecharts.faker import Faker
from cutecharts.components import Page
def bar_base() -> Bar:
chart = Bar("手绘风格的柱状图")
chart.set_options(labels=Faker.choose(), x_label="类别", y_label="数量")
chart.add_series("series-A", Faker.values())
return chart
def line_base() -> Line:
chart = Line("手绘风格的折线统计图")
chart.set_options(labels=Faker.choose(), x_label="种类", y_label="数量")
chart.add_series("series-A", Faker.values())
chart.add_series("series-B", Faker.values())
return chart
page = Page()
page.add(bar_base(),line_base())
page.render('both.html')
# +
def line_tickcount_colors():
chart = Line("Line-调整颜色")
chart.set_options(labels=Faker.choose(), colors=Faker.colors, y_tick_count=8)
chart.add_series("series-A", Faker.values())
chart.add_series("series-B", Faker.values())
return chart
line_tickcount_colors().render_notebook()
# +
from cutecharts.charts import Pie
from cutecharts.components import Page
from cutecharts.faker import Faker
def pie_base() -> Pie:
chart = Pie("手绘风格饼图")
chart.set_options(labels=Faker.choose())
chart.add_series(Faker.values())
return chart
pie_base().render_notebook()
# -
def pie_legend_font():
chart = Pie("Pie-Legend")
chart.set_options(
labels=Faker.choose(),
legend_pos="downLeft",
font_family='"Times New Roman",Georgia,Serif;',
)
chart.add_series(Faker.values())
return chart
pie_legend_font().render_notebook()
def pie_radius():
chart = Pie("Pie-Radius")
chart.set_options(
labels=Faker.choose(),
inner_radius=0,
)
chart.add_series(Faker.values())
return chart
pie_radius().render_notebook()
# +
from cutecharts.charts import Radar
from cutecharts.components import Page
from cutecharts.faker import Faker
def radar_base() -> Radar:
chart = Radar("手绘风格雷达图")
chart.set_options(labels=Faker.choose())
chart.add_series("series-A", Faker.values())
chart.add_series("series-B", Faker.values())
return chart
radar_base().render_notebook()
# +
from cutecharts.charts import Scatter
from cutecharts.components import Page
from cutecharts.faker import Faker
def scatter_base() -> Scatter:
chart = Scatter("手绘风格散点图")
chart.set_options(x_label="时间", y_label="数量")
chart.add_series("series-A", [(z[0], z[1]) for z in zip(Faker.values(), Faker.values())])
chart.add_series("series-B", [(z[0], z[1]) for z in zip(Faker.values(), Faker.values())])
return chart
scatter_base().render_notebook()
# -
def scatter_dotsize_tickcount():
chart = Scatter("Scatter-散点大小")
chart.set_options(dot_size=2, y_tick_count=8)
chart.add_series("series-A", [(z[0], z[1]) for z in zip(Faker.values(), Faker.values())])
chart.add_series("series-B", [(z[0], z[1]) for z in zip(Faker.values(), Faker.values())])
return chart
scatter_dotsize_tickcount().render_notebook()
def scatter_show_line():
chart = Scatter("Scatter-散点连成线")
chart.set_options(y_tick_count=8, is_show_line=True)
chart.add_series(
"series-A", [(z[0], z[1]) for z in zip(Faker.values(), Faker.values())]
)
chart.add_series(
"series-B", [(z[0], z[1]) for z in zip(Faker.values(), Faker.values())]
)
return chart
scatter_show_line().render_notebook()