推荐一个 Python 手绘图形库
共 9623字,需浏览 20分钟
·
2020-09-30 17:07
https://github.com/chenjiandongx/cutecharts
今天,给大家介绍一个很酷的 Python 手绘风格可视化神包:cutecharts。
![](https://filescdn.proginn.com/449bed27fe81c2e884e23999abbb8c34/8f4121e74cc73aba0523e13bbf91b1ea.webp)
GitHub 地址:
怎么画出这些图表呢,很简单,一行命令先安装好该库:
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()
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 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()
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 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()
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
![img](https://filescdn.proginn.com/0c22f98ad42e5a41a1e86b1168f69cd3/8be2f41b9ddbdfa3527bb1923d977939.webp)
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()
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 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()
![img](https://filescdn.proginn.com/0638a1e7d26808cb437c8eace748d27e/92e2d25defe2f1fe4697c1bf2a0f5222.webp)
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
![img](https://filescdn.proginn.com/b826016c204bb5974e9653fa88a68648/d9ebd88154cf230c5515812e6a2e8f92.webp)
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