大家好,今天給大家帶來繪制“手繪風格”可視化作品的小技巧,主要涉及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')

Example01 of matplotlib.xkcd()
- 樣例二:
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')

Example02 of matplotlib.xkcd()
- 樣例三:
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')

Example03 of matplotlib.xkcd()
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()

Example02 of cutecharts
- 樣例三:
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()

Example03 of cutecharts
這里這是基本的圖表繪制,實作定制化的屬性引數也都沒有介紹,小伙伴們可去官網查閱(由于沒詳細的官方檔案,大家可參考樣例和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()

Example01 of roughviz
- 樣例二:
from roughviz.charts.donut import Donut
donut = Donut(data={"labels": ['a', 'b'], "values": [10, 20]}, title="Donut-roughviz基本示例02", title_fontsize=3)
donut.show()

Example02 of roughviz
總結
本期推文,小編給大家匯總了“手繪風格”的可視化圖表繪制方法,喜歡的歡迎收藏、點贊、關注,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/423350.html
標籤:AI
