当前位置: 华文头条 > 推荐

Matplotlib 强势变身:6 个 Python 风格库惊艳众人

2024-03-22推荐

如果你也是一名数据科学家,你一定明白,无论你对数据的理解和研究有多深入,你最终对结果的判断都将基于你可视化成果。更糟的是,对于随机观察者来说,这些图表不仅需要具有信息量和直观性,还要具有潮流感。

在我看来,python matplotlib和seaborn样式有些乏味,而且被过度使用。有时,它们甚至会让人觉得作者没有投入太多时间或精力。为了给绘图增添亮点,我发现了6个Python库,它们可以在常规的线图/散点图、直方图和其他基本可视化图上增加色彩。每个库都有对应的github repo链接!

首先,生成一些综合数据。使用每个库制作4个子图,这样就可以在同类数据上对它们进行比较。

import matplotlibimport matplotlib.pyplot as pltimport numpy as npimport pandas as pdimport seaborn as sns'''Generating points to create a scatter plot'''def scatter(): x = np.random.random(100) y = x-np.random.random(100) z = np.random.randint(0,4,100) df = pd.DataFrame({'X':x, 'Y': y, 'Z':z}) return df'''Generating points to create a line plot'''def line(): x = np.arange(0,10,0.1) y_1 = np.sin(x) y_2 = np.sin(x+5) y_3 = np.sin(x+10) y_4 = np.sin(x+15) y_5 = np.sin(x+20) df = pd.DataFrame({'X':x,'y_1':y_1, 'y_2':y_2, 'y_3':y_3, 'y_4':y_4, 'y_5':y_5}) return df'''Sampling data from several distributions'''def hist(): x_1 = np.random.normal(1,0.1,1000) x_2 = np.random.gamma(1,0.25,1000) x_3 = np.random.normal(0.4, 0.1,1000) x_4 = np.random.normal(-0.1, 0.3,1000) df = pd.DataFrame({'X_1': x_1, 'X_2':x_2, 'X_3': x_3, 'X_4':x_4}) return df

注意: 没有特定的颜色或图案,只是在同一组绘画中应用一种风格。

1. Aquarel

Aquarel库[1] 提供 11 种 不同的样式,包括深色和浅色样式,可使用 pip 安装:

pip install aquarel

可以像下面这样使用上下文管理器来使用这些主题:

with load_theme("arctic_light"): fig, ax = plt.subplots(ncols=2, nrows=2, figsize=(16,9)) df = scatter() f= ax[0,0].scatter(df.X,df.Y, c=df.Z, s=50) ax[0,0].set_xlabel('X data') ax[0,0].set_ylabel('Y data') handles, labels = f.legend_elements(prop="colors", alpha=0.6) legend2 = ax[0,0].legend(handles, labels, loc="lower right") df=line() df.plot(x='X', ax=ax[0,1]) df=hist() sns.kdeplot(df, fill=True, ax=ax[1,0]) ax[1,0].set_xlabel('Value') sns.kdeplot(df, x="X_1", y="X_2", fill=True, ax=ax[1,1]) sns.kdeplot(df, x="X_3", y="X_4",fill=True, ax=ax[1,1]) ax[1,1].set_xlabel('Dist 1') ax[1,1].set_ylabel('Dist 2') plt.suptitle('Aquarel\narctic_light', fontsize=24) plt.savefig('arctic_light.jpg') plt.show()

或者,你也可以直接使用

from aquarel import load_themetheme = load_theme("arctic_light")theme.apply()# ... plotting code heretheme.apply_transforms()

2. Rosé Pine

第二个 repo[2] 我非常喜欢,它不是一个库,而是一组主题,你需要下载这些主题,然后指定 matplotlib 的路径:

plt. style.use('./themes/rose-pine-moon.mpl style')

之后,只需要按照相同的绘图步骤进行即可。这个软件包的颜色非常温和,同时对比度也很高。

3. Catppuccin

需要使用 pip 安装 Catppuccin库[3] 。它包括 4 种不同的风格和不同的暗度。

matplotlib. style.use("mocha")

Catppuccin软件包还提供了一个有趣的功能,可以将不同的样式表进行混合。可以尝试将基本的seaborn-v0_8-dark和mocha样式表结合起来。

Catppuccin混合主题

4. mplcyberpunk

下一个库 mplcyberpunk[4] 非常有名,每个人都钟爱赛博朋克,这款软件不仅能够提供恰到好处的颜色和背景,还能为图片添加发光效果,绝对让人惊艳!

import matplotlib.pyplot as pltimport mplcyberpunkplt. style.use("cyberpunk")...mplcyberpunk.add_glow_effects()plt.show()

fig, ax = plt.subplots()...mplcyberpunk.make_lines_glow(ax)

5. matplotx

matplotx[5] 是matplotlib的另一个扩展包,可以通过pip像之前的扩展包一样安装。它提供了20种不同的主题,可用于科学研究、投影或其他任何用途,因为这些风格既有严谨的特点,也有非正式和时尚的风格。

6. GruvBox

今天的最后一个是 GruvBox[6] 。,它不是一个真正的库,而是一个文件,适用时需要上传它:

matplotlib. style.use("./gruvbox.mpl style")

尽管这个软件仓库中只有一个主题,但我非常喜欢它的字体、线条和背景颜色的搭配!

希望以上的内容能够帮助到你~

参考资料

[1] Aquarel库: https://github.com/lgienapp/aquarel

[2] repo: https://github.com/ppZ/rose-pine-matplotlib

[3] Catppuccin库: https://github.com/catppuccin/matplotlib

[4] mplcyberpunk: https://github.com/dhaitz/mplcyberpunk

[5] matplotx: https://github.com/nschloe/matplotx

[6] GruvBox: https://github.com/thriveth/gruvbox-mpl/tree/master