前言

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。

来自:https://github.com/chenjiandongx/cutecharts

转载:Python知识圈

今天,给大家介绍一个很酷的 Python 手绘风格可视化神包:cutecharts。

和 Matplotlib 、pyecharts 等常见的图表不同,使用这个包可以生成下面这种看起来像手绘的各种图表,在一些场景下使用效果可能会更好。

a8b3336fe555415a94ab72599cf36e8d.png

GitHub 地址:

https://github.com/chenjiandongx/cutecharts

怎么画出这些图表呢,很简单,一行命令先安装好该库:

pip install cutecharts

也可以使用源码安装的方式:

$ git clone https://github.com/chenjiandongx/cutecharts.git
$ cd cutecharts
$ pip install -r requirements.txt
$ python setup.py install

下面就介绍下每个图表如何绘制。

首先是一些图表共通的参数:

Commons

不同图表有着部分相同的方法。

__init__

Params                                          Desc
------                                          ----title: Optional[str] = None                     图表标题
width: str = "800px"                            图表宽度
height: str = "600px"                           图表高度
assets_host: Optional[str] = None               引用资源 Host

render

Params                                          Desc
------                                          ----dest: str = "render.html"                       渲染的文件路径
template_name: str = "basic_local.html"         渲染使用的模板,一般不需要修改   

render_notebook

Params                                          Desc
------                                          ----template_type: str = "basic"                    渲染使用的模板类型,一般不需要修改 

load_javascript

加载 JS 依赖,在 JupyterLab 渲染时使用。

Bar(柱状图)

cutecharts.charts.Bar

API

cutecharts.charts.Bar.set_options
Params                                          Desc
------                                          ----labels: Iterable                                X 坐标轴标签数据x_label: str = ""                               X 坐标轴名称
y_label: str = ""                               Y 坐标轴名称
y_tick_count: int = 3                           Y 轴刻度分割段数
colors: Optional[Iterable] = None               label 颜色数组
font_family: Optional[str] = None               CSS font-family
cutecharts.charts.Bar.add_series
Params                                          Desc
------                                          ----name: str                                       series 名称data: Iterable                                  series 数据列表

Demo

Bar-基本示例
from cutecharts.charts import Bar
from cutecharts.components import Page
from cutecharts.faker import Faker
def bar_base() -> Bar:    chart = Bar("Bar-基本示例")
    chart.set_options(labels=Faker.choose(), x_label="I'm xlabel", y_label="I'm ylabel")
    chart.add_series("series-A", Faker.values())
    return chart
bar_base().render()

5bcd97b21eafcd55748d8a24da0591e9.png
Bar-调整颜色
def bar_tickcount_colors():
    chart = Bar("Bar-调整颜色")
    chart.set_options(labels=Faker.choose(), y_tick_count=10, colors=Faker.colors)
    chart.add_series("series-A", Faker.values())
    return chart

80edda1d72a9ac07af2208de3fd68072.png

Line(折线图)

cutecharts.charts.Line

API

cutecharts.charts.Line.set_options
Params                                          Desc
------                                          ----labels: Iterable                                X 坐标轴标签数据x_label: str = ""                               X 坐标轴名称
y_label: str = ""                               Y 坐标轴名称
y_tick_count: int = 3                           Y 轴刻度分割段数
legend_pos: str = "upLeft"                      图例位置,有 "upLeft", "upRight", "downLeft", "downRight" 可选
colors: Optional[Iterable] = None               label 颜色数组
font_family: Optional[str] = None               CSS font-family
cutecharts.charts.Line.add_series
Params                                          Desc
------                                          ----name: str                                       series 名称data: Iterable                                  series 数据列表

Demo

Line-基本示例
from cutecharts.charts import Line
from cutecharts.components import Page
from cutecharts.faker import Faker
def line_base() -> Line:    chart = Line("Line-基本示例")
    chart.set_options(labels=Faker.choose(), x_label="I'm xlabel", y_label="I'm ylabel")
    chart.add_series("series-A", Faker.values())
    chart.add_series("series-B", Faker.values())
    return chart
line_base().render()

533870b43bebadf007ac61b8e4bc228e.png
Line-Legend 位置
def line_legend():
    chart = Line("Line-Legend 位置")
    chart.set_options(labels=Faker.choose(), legend_pos="upRight")
    chart.add_series("series-A", Faker.values())
    chart.add_series("series-B", Faker.values())
    return chart

4e8b0c87d8604956b6978e8ad045d238.png
Line-调整颜色
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

83b11fac0d53ad711186f6d117d5b7b2.png

Pie(饼图)

cutecharts.charts.Pie

API

cutecharts.charts.Pie.set_options
Params                                          Desc
------                                          ----labels: Iterable                                数据标签列表inner_radius: float = 0.5                       Pie 图半径
legend_pos: str = "upLeft"                      图例位置,有 "upLeft", "upRight", "downLeft", "downRight" 可选
colors: Optional[Iterable] = None               label 颜色数组
font_family: Optional[str] = None               CSS font-family
cutecharts.charts.Pie.add_series
Params                                          Desc
------                                       ----data: Iterable                                  series 数据列表

Demo

Pie-基本示例
from cutecharts.charts import Pie
from cutecharts.components import Page
from cutecharts.faker import Faker
def pie_base() -> Pie:    chart = Pie("Pie-基本示例")
    chart.set_options(labels=Faker.choose())    chart.add_series(Faker.values())    return chart
pie_base().render()

e46af8f793a67b01f774ad5620423b7c.png
Pie-Legend
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

296ae53e8a72f3fdecda83b35624b8e4.png
Pie-Radius
def pie_radius():
    chart = Pie("Pie-Radius")
    chart.set_options(        labels=Faker.choose(),        inner_radius=0,
    )    chart.add_series(Faker.values())    return chart

0a475e8bcc3f3f6f82b0963f9999cda5.png

Radar(雷达图)

cutecharts.charts.Radar

API

cutecharts.charts.Radar.set_options
Params                                          Desc
------                                          ----labels: Iterable                                数据标签列表is_show_label: bool = True                      是否显示标签
is_show_legend: bool = True                     是否显示图例
tick_count: int = 3                             坐标系分割刻度
legend_pos: str = "upLeft"                      图例位置,有 "upLeft", "upRight", "downLeft", "downRight" 可选
colors: Optional[Iterable] = None               label 颜色数组
font_family: Optional[str] = None               CSS font-family
cutecharts.charts.Radar.add_series
Params                                          Desc
------                                          ----name: str                                       series 名称data: Iterable                                  series 数据列表

Demo

Radar-基本示例
from cutecharts.charts import Radar
from cutecharts.components import Page
from cutecharts.faker import Faker
def radar_base() -> Radar:    chart = Radar("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()

c7998cc26567ed092cd096f63001be46.png
Radar-颜色调整
def radar_legend_colors():
    chart = Radar("Radar-颜色调整")
    chart.set_options(labels=Faker.choose(), colors=Faker.colors, legend_pos="upRight")
    chart.add_series("series-A", Faker.values())
    chart.add_series("series-B", Faker.values())
    return chart

1b97c43a7a8c584b8a36ac2e0d0269fb.png

Scatter(散点图)

cutecharts.charts.Scatter

API

cutecharts.charts.Scatter.set_options
Params                                          Desc
------                                          ----x_label: str = ""                               X 坐标轴名称
y_label: str = ""                               Y 坐标轴名称
x_tick_count: int = 3                           X 轴刻度分割段数
y_tick_count: int = 3                           Y 轴刻度分割段数
is_show_line: bool = False                      是否将散点连成线
dot_size: int = 1                               散点大小
time_format: Optional[str] = None               日期格式
legend_pos: str = "upLeft"                      图例位置,有 "upLeft", "upRight", "downLeft", "downRight" 可选
colors: Optional[Iterable] = None               label 颜色数组
font_family: Optional[str] = None               CSS font-family
cutecharts.charts.Scatter.add_series
Params                                          Desc
------                                          ----
name: str                                       series 名称
data: Iterable                                  series 数据列表,[(x1, y1), (x2, y2)]

Demo

Scatter-基本示例
from cutecharts.charts import Scatter
from cutecharts.components import Page
from cutecharts.faker import Faker
def scatter_base() -> Scatter:    chart = Scatter("Scatter-基本示例")
    chart.set_options(x_label="I'm xlabel", y_label="I'm ylabel")
    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()

9b1a9b0c1df747446398d02f79554146.png
Scatter-散点大小
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

3f439a2b90946dc8439c25fca6155d5e.png
Scatter-散点连成线
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

15a56efd65662ec4f005ebb9bca31776.png
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐