這個Matplotlib子函數特別簡單,只有三個參數,別看參數少,但功能可不小
matplotlib.pyplot.xkcd(scale=1, #相對於不使用xkcd的風格圖,褶皺的幅度
length=100, #褶皺長度
randomness=2#褶皺的隨機性
)
matplotlib.pyplot.xkcd()使用如下,加with行代碼即可,括號中參數按個人喜好決定是否設置~
with plt.xkcd(scale=1, length=100, randomness=2):
#with是臨時使用一下,不影響其它圖使用正常樣式
繪圖代碼
。。。。。。
plt.show()
matplotlib.pyplot.xkcd()使用實例以下參考:Python可視化25|seaborn繪製矩陣圖
#支持seaborn
import seaborn as sns
iris_sns = sns.load_dataset("iris")
with plt.xkcd():
g = sns.pairplot(
iris_sns,
hue='species', #按照三種花分類
palette=['#dc2624', '#2b4750', '#45a0a2'])
sns.set(style='whitegrid')
g.fig.set_size_inches(12, 12)
sns.set(style='whitegrid', font_scale=1.5)以下參考:Python可視化29|matplotlib-餅圖(pie)
import matplotlib.pyplot as plt
with plt.xkcd(
scale=4, #相對於不使用xkcd的風格圖,褶皺的幅度
length=120, #褶皺長度
randomness=2): #褶皺的隨機性
plt.figure(dpi=150)
patches, texts, autotexts = plt.pie(
x=[1, 2, 3], #返回三個對象
labels=['A', 'B', 'C'],
colors=['#dc2624', '#2b4750', '#45a0a2'],
autopct='%.2f%%',
explode=(0.1, 0, 0))
texts[1].set_size('20') #修改B的大小
#matplotlib.patches.Wedge
patches[0].set_alpha(0.3) #A組分設置透明度
patches[2].set_hatch('|') #C組分添加網格線
patches[1].set_hatch('x')
plt.legend(
patches,
['A', 'B', 'C'], #添加圖例
title="Pie Learning",
loc="center left",
fontsize=15,
bbox_to_anchor=(1, 0, 0.5, 1))
plt.title('Lovely pie', size=20)
plt.show()with plt.xkcd():
from string import ascii_letters
plt.figure(dpi=150)
patches, texts, autotexts = plt.pie(
x=range(1, 12),
labels=list(ascii_letters[26:])[0:11],
colors=[
'#dc2624', '#2b4750', '#45a0a2', '#e87a59', '#7dcaa9', '#649E7D',
'#dc8018', '#C89F91', '#6c6d6c', '#4f6268', '#c7cccf'
],
autopct='%.2f%%',
)
plt.legend(
patches,
list(ascii_letters[26:])[0:11], #添加圖例
title="Pie Learning",
loc="center left",
bbox_to_anchor=(1, 0, 0.5, 1),
ncol=2, #控制圖例中按照兩列顯示,默認為一列顯示,
)以下參考:Python可視化|matplotlib12-垂直|水平|堆積條形圖詳解
import matplotlib.pyplot as plt
import numpy as np
with plt.xkcd():
plt.figure(dpi=150)
labels = ['Jack', 'Rose', 'Jimmy']
year_2019 = np.arange(1, 4)
year_2020 = np.arange(1, 4) + 1
bar_width = 0.4
plt.bar(
np.arange(len(labels)) - bar_width / 2, #為了兩個柱子一樣寬
year_2019,
color='#dc2624',
width=bar_width,
label='year_2019' #圖例
)
plt.bar(
np.arange(len(labels)) + bar_width / 2,
year_2020,
color='#45a0a2',
width=bar_width,
label='year_2020' #圖例
)
plt.xticks(np.arange(0, 3, step=1), labels, rotation=45) #定義柱子名稱
plt.legend(loc=2) #圖例在左邊以下參考: Python可視化|matplotlib10-繪製散點圖scatter
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
#數據準備
from sklearn import datasets
iris = datasets.load_iris()
x, y = iris.data, iris.target
pd_iris = pd.DataFrame(np.hstack((x, y.reshape(150, 1))),
columns=[
'sepal length(cm)', 'sepal width(cm)',
'petal length(cm)', 'petal width(cm)', 'class'
])
with plt.xkcd():
plt.figure(dpi=150) #設置圖的解析度
#plt.style.use('Solarize_Light2') #使用Solarize_Light2風格繪圖
iris_type = pd_iris['class'].unique() #根據class列將點分為三類
iris_name = iris.target_names #獲取每一類的名稱
colors = ['#dc2624', '#2b4750', '#45a0a2'] #三種不同顏色
markers = ['$\clubsuit, '.', '+'] #三種不同圖形
for i in range(len(iris_type)):
plt.scatter(
pd_iris.loc[pd_iris['class'] == iris_type[i],
'sepal length(cm)'], #傳入數據x
pd_iris.loc[pd_iris['class'] == iris_type[i],
'sepal width(cm)'], #傳入數據y
s=50, #散點圖形(marker)的大小
c=colors[i], #marker顏色
marker=markers[i], #marker形狀
#marker=matplotlib.markers.MarkerStyle(marker = markers[i],fillstyle='full'),#設置marker的填充
alpha=0.8, #marker透明度,範圍為0-1
facecolors='r', #marker的填充顏色,當上面c參數設置了顏色,優先c
edgecolors='none', #marker的邊緣線色
linewidths=1, #marker邊緣線寬度,edgecolors不設置時,該參數不起作用
label=iris_name[i]) #後面圖例的名稱取自label
plt.legend(loc='upper right')Ref: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.xkcd.html#matplotlib.pyplot.xkcd
如需聯繫EasyShu團隊
請加微信:EasyCharts
微信公眾號【EasyShu】博文代碼集合地址
https://github.com/Easy-Shu/EasyShu-WeChat
《Python數據可視化之美》-配套原始碼下載地址
https://github.com/Easy-Shu/Beautiful-Visualization-with-python
《R語言數據可視化之美》-增強版配套原始碼下載地址
https://github.com/Easy-Shu/Beautiful-Visualization-with-R