推荐一个 Python 手绘图形库(附代码)

Python大数据分析

共 9771字,需浏览 20分钟

 ·

2020-11-22 16:50

来源:Python与算法社区
今天,给大家介绍一个很酷的 Python 手绘风格可视化神包:cutecharts。
GitHub地址:
https://github.com/chenjiandongx/cutecharts

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


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 Barfrom cutecharts.components import Pagefrom 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()


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


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 Linefrom cutecharts.components import Pagefrom 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 chartline_base().render()


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


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

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 Piefrom cutecharts.components import Pagefrom 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()


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


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

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 Radarfrom cutecharts.components import Pagefrom 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()


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

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 Scatterfrom cutecharts.components import Pagefrom 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()


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


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


· 往期精选 ·
1

Python代码转Latex公式,这个开源库用一行代码帮你搞定

2

纯Python绘制满满艺术感的山脊地图

3

如何使用sklearn优雅地进行数据挖掘?




Python大数据分析

data creates value

扫码关注我们

浏览 45
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报