Python 绘图还在用 Matplotlib?发现一款手绘可视化神器!
(给Python开发者加星标,提升Python技能)
转自:高级农民工
今天,给大家介绍一个很酷的 Python 手绘风格可视化神包: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 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
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()
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
推荐阅读
(点击标题可跳转阅读)
觉得本文对你有帮助?请分享给更多人
关注「Python开发者」加星标,提升Python技能
好文章,我在看❤️