点击上方“Python高校”,马上关注
真爱,请置顶或星标
来源:
https://github.com/chenjiandongx/cutecharts
今天,给大家介绍一个很酷的 Python 手绘风格可视化神包:cutecharts。
和 Matplotlib 、pyecharts 等常见的图表不同,使用这个包可以生成下面这种看起来像手绘的各种图表,在一些场景下使用效果可能会更好。
![](https://i-blog.csdnimg.cn/blog_migrate/d80ff3d6cb3d58782e7ce1069f895a1e.jpeg)
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()
![img](https://i-blog.csdnimg.cn/blog_migrate/61f0676326b881af930535839681a2b8.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
![img](https://i-blog.csdnimg.cn/blog_migrate/8dbe03b794463478ed9d44dcd88ea354.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()
![img](https://i-blog.csdnimg.cn/blog_migrate/7e8c75c508ccc4d344f9a2c5230e45e8.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
![img](https://i-blog.csdnimg.cn/blog_migrate/014c908a733b0cad2964330f78dd8932.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
![img](https://i-blog.csdnimg.cn/blog_migrate/4727223db3e040075e86da592dc6a2ff.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()
![img](https://i-blog.csdnimg.cn/blog_migrate/3ebfc72dfc04ffc63eb244c7a891f466.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
![img](https://i-blog.csdnimg.cn/blog_migrate/1aa275a24a9f78759894ea18a13625f3.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
![img](https://i-blog.csdnimg.cn/blog_migrate/bc4d3c72876c9426f2ad042e3af6f474.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()
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://i-blog.csdnimg.cn/blog_migrate/1daa5df5b6a0d5b6f1eb3f31e62921c4.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
![img](https://i-blog.csdnimg.cn/blog_migrate/2f7084f3537106e14e15ece3a145de10.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
![img](https://i-blog.csdnimg.cn/blog_migrate/eef13d382b6fa686961cf74ede420d3e.png)
觉得不错就赶紧去尝尝鲜,点个赞再走!
历史阅读:
Python 开发者必知的 11 个 Python GUI 库
基于TensorFlow 2.0的中文深度学习开源书来了!GitHub趋势日榜第一,斩获2K+星
微软官方上线了Python 教程,7个章节就把Python说通了
我给曾经暗恋的初中女同学,用Python实现了她飞机上刷抖音
被女朋友三番五次拉黑后,我用 Python 写了个“舔狗”必备神器
最全 14 张思维导图:教你构建 Python 编程的核心知识体系!
这里除了干货一无所有
人生苦短,我选在看