網站首頁 編程語言 正文
? ? 使用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
相關推薦
- 2022-05-08 ASP.NET中Web?API的參數綁定_實用技巧
- 2022-05-01 Python字符串和其常用函數合集_python
- 2022-08-17 C++詳解如何通過模板實現元素的反序_C 語言
- 2024-02-16 ReentrantLock相較于synchronized的區別---鎖超時(源碼理解)
- 2022-05-07 如何在Python中進行異常處理_python
- 2022-11-01 go項目打包部署的完整步驟_Golang
- 2022-04-07 C語言的線性表之順序表你了解嗎_C 語言
- 2022-08-22 python作圖基礎之plt.contour實例詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支