日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

matplotlib自定義風格

作者:薛定諤的青蛙 更新時間: 2022-09-22 編程語言

? ? 使用matplotlib進行繪圖時,經常遇到個問題,就是總是要花大量代碼對繪圖的格式進行設置。雖然可以將同一類繪圖的代碼保存以后使用,但是看著這么長一串用來設置格式就很不爽。

一、matplotlib自帶風格

matlotlib中,自帶了許多不同的風格。

通過以下方法可以查看自帶風格:

from matplotlib import pyplot as plt
# 查看 Matplotlib 可用繪圖風格
print(plt.style.available)

輸出結果如下:

?關于這些風格的預覽,可以在官方文檔中查看:matplotlib Style sheets reference。

?加上一行代碼就能輕松改變matplotlib的風格:

plt.style.use('name')

不過說實話這玩意根本沒啥用。

二、Science Plots

某位哈佛大學的野生博士不忍重復繪圖之苦,于是自己開發了science,ieee,nature等期刊的matplotlib繪圖補充包。下載和使用的詳情見:Science Plots Github。

下面是對應該補充包的一個使用例:

""" An example of the 'science' theme. """

import numpy as np 
import matplotlib.pyplot as plt 

def model(x, p):
    return x ** (2 * p + 1) / (1 + x ** (2 * p))

x = np.linspace(0.75, 1.25, 201)

with plt.style.use(['science']):
    fig, ax = plt.subplots()
    for p in [10, 15, 20, 30, 50, 100]:
        ax.plot(x, model(x, p), label=p)
    ax.legend(title='Order')
    ax.set(xlabel='Voltage (mV)')
    ax.set(ylabel='Current ($\mu$A)')
    ax.autoscale(tight=True)
    fig.savefig('figures/fig1.pdf')
    fig.savefig('figures/fig1.jpg', dpi=300)

然后便能直接繪制science風格的圖了!

感覺似乎很牛逼的樣子?但是我根本發不了science也不用投ieee啊!

三、自定義matplotlib風格

在安裝了science plot包后,我們可以在目錄中:C:\Users\user name\.matplotlib\stylelib創建一個.mplstyle文件自定義matplotlib風格。

以下是我寫的一個mystyle.mplstyle文件:

# Use serif fonts 使用新羅馬字體,加粗。
font.serif : Times New Roman
font.family : serif
font.weight : bold

# Use LaTeX for math formatting #不使用LaTeX。
text.usetex : False
text.latex.preamble : \usepackage{amsmath}

# Set color cycle: blue, green, yellow, red, violet, gray #設置循環顏色
axes.prop_cycle : cycler('color', ['0C5DA5', '00B945', 'FF9500', 'FF2C00', '845B97', '474747', '9e9e9e'])

# Set default figure size #設置圖片尺寸
#figure.figsize : 3.5,2.625

#Set default plot #線寬
lines.linewidth : 2

#Set legend style #設置圖例
legend.loc : 'best'
legend.frameon : False
legend.fontsize : 12

#Set axes #設置框
axes.linewidth : 1.5
axes.labelweight : bold

#Set labels #設置坐標字體
axes.labelsize: 14
xtick.labelsize: 12
ytick.labelsize: 12

#Set save #圖片分辨率
savefig.dpi : 600

使用mystyle前作圖:

使用mystyle后作圖:

?

?

原文鏈接:https://blog.csdn.net/weixin_51982763/article/details/126979230

欄目分類
最近更新