其他
干货,用 Python 绘制“手绘风格”图表的所有方法!
Python-matplotlib 手绘风格图表绘制 Python-cutecharts 手绘风格图表绘制 Python-py-roughviz 手绘风格图表绘制
Python-matplotlib 手绘风格图表绘制
使用Python进行可视化绘制,首先想到的当然是Matplotlib,“手绘风格”的图表绘制方法当然首选它。在Matplotlib中,matplotlib.pyplot.xkcd() 绘图函数就可以进行手绘风图表的绘制,下面小编通过具体样例进行展示:
样例一:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
with plt.xkcd():
fig, ax = plt.subplots(figsize=(6.5,4),dpi=100)
ax = df.plot.bar(color=["#BC3C28","#0972B5"],ec="black",rot=15,ax=ax)
ax.set_ylim((0, 100))
ax.legend(frameon=False)
ax.set_title("EXAMPLE01 OF MATPLOTLIB.XKCD()",pad=20)
ax.text(.8,-.22,'Visualization by DataCharm',transform = ax.transAxes,
ha='center', va='center',fontsize = 10,color='black')
样例二:
df = pd.DataFrame({
'x': [1, 2, 2.5, 3, 3.5, 4, 5],
'y': [4, 4, 4.5, 5, 5.5, 6, 6],
})
with plt.xkcd():
fig, ax = plt.subplots(figsize=(6.5,4),dpi=100)
ax = df.plot.kde(color=["#BC3C28","#0972B5"],ax=ax)
ax.set_ylim((0, 0.4))
ax.legend(frameon=False)
ax.set_title("EXAMPLE02 OF MATPLOTLIB.XKCD()",pad=20)
ax.text(.8,-.22,'Visualization by DataCharm',transform = ax.transAxes,
ha='center', va='center',fontsize = 10,color='black')
样例三:
with plt.xkcd():
fig, ax = plt.subplots(figsize=(6.5,4),dpi=100)
ax.spines["right"].set_color('none')
ax.spines["top"].set_color('none')
ax.set_xticks([])
ax.set_yticks([])
ax.set_ylim([-30, 10])
data = np.ones(100)
data[70:] -= np.arange(30)
ax.annotate(
'THE DAY I REALIZED\nI COULD COOK BACON\nWHENEVER I WANTED',
xy=(70, 1), arrowprops=dict(arrowstyle='->'), xytext=(15, -10))
ax.plot(data,color="#BC3C28")
ax.set_xlabel('time')
ax.set_ylabel('my overall health')
ax.set_title("EXAMPLE03 OF MATPLOTLIB.XKCD()")
ax.text(.8,-.15,'Visualization by DataCharm',transform = ax.transAxes,
ha='center', va='center',fontsize = 10,color='black')
Python-cutecharts 手绘风格图表绘制
介绍完使用matplotlib绘制后,小编再介绍一个专门绘制“手绘风格”图表的Python可视化库-cutecharts。这个包可能有的小伙伴也有了解过,如果熟悉pyecharts的同学肯定会更加快速上手的。官网如下:https://github.com/cutecharts/cutecharts.py 。这里小编就直接列举几个例子,感兴趣的同学可自行探索哈~
样例一:
from cutecharts.charts import Bar
from cutecharts.components import Page
from cutecharts.faker import Faker
def bar_base() -> Bar:
chart = Bar("Bar-cutecharts基本示例01")
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_notebook()
注:render_notebook()方法可使绘图结果在jupyter notebook 中显示。
样例二:
from cutecharts.charts import Line
from cutecharts.components import Page
from cutecharts.faker import Faker
def line_base() -> Line:
chart = Line("Line-cutecharts基本示例02")
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_notebook()
样例三:
from cutecharts.charts import Pie
from cutecharts.components import Page
from cutecharts.faker import Faker
def pie_base() -> Pie:
chart = Pie("Pie-cutecharts基本示例03")
chart.set_options(labels=Faker.choose(),legend_pos="upRight")
chart.add_series(Faker.values())
return chart
pie_base().render_notebook()
这里这是基本的图表绘制,实现定制化的属性参数也都没有介绍,小伙伴们可去官网查阅(由于没详细的官方文档,大家可参考样例和pyecharts的文档)
Python-py-roughviz 手绘风格图表绘制
这个和cutecharts包一样,都是基于roughViz.js转换编码绘制的,官网为:https://github.com/charlesdong1991/py-roughviz 。由于所支持的图表类型不是很多且各个图标设置的参数也不够完善,这里小编直接给出两个样例,感兴趣的小伙伴可自行探索哈~
样例一:
from roughviz.charts.bar import Bar
data = {
"labels": ["North", "South", "East", "West"],
"values": [10, 5, 8, 3]
}
bar = Bar(data=data, title="Bar-roughviz基本示例01", title_fontsize=3)
bar.set_options(xlabel="Region", ylabel="Number", color="orange")
bar.show()
样例二:
from roughviz.charts.donut import Donut
donut = Donut(data={"labels": ['a', 'b'], "values": [10, 20]}, title="Donut-roughviz基本示例02", title_fontsize=3)
donut.show()
- EOF -
看完本文有收获?请转发分享给更多人
推荐关注「数据分析与开发」,提升数据技能
点赞和在看就是最大的支持❤️