Python Matplotlib 画图自用模板

Python Matplotlib 画图自用模板

在这里存一个自用模板代码。有些命令光用脑子记不住诶。

Boilerplate

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pickle
from matplotlib import rcParams

# font
rcParams['font.family'] = "Roboto"  # Roboto (download it from Google Font)

fig, ax = plt.subplots(tight_layout=True, figsize=(14, 5))

x = np.linspace(0, 20, 100)
y = np.cos(x)

ax.plot(x, y, color='k', linewidth=1.5, linestyle='--', label='Xovee')

# axis
ax.set_xlabel('Xovee', fontsize=16)
ax.tick_params(axis='both', which='both', labelsize=14)

# spines
for spine in ['top', 'right', 'bottom', 'left']:
    ax.spines[spine].set_linewidth(1.5)

# grid
ax.grid(axis='y', linestyle='--', color='lightgray', linewidth=.5, alpha=.5, zorder=0)

ax.legend(fontsize=16)

plt.show()

Command Reference

import matplotlib.pyplot as plt
from matplotlib import rcParams

# font
rcParams['font.family'] = "Times New Roman"  # Roboto (download it from Google Font)

fig, ax = plt.subplots(tight_layout=True, figsize=(8, 5))

ax.plot(xovee, yovee)

# axis
ax.set_xlim(0, 1)
ax.set_xscale('log')
ax.set_xlabel('Xovee', fontsize=16)
ax.set_xticks([1, 2, 3], ['a', 'b', 'c'], fontsize=14, rotation=20)
ax.tick_params(axis='both/x/y', which='both/major/minor', labelsize=14, bottom=True, labelbottom=True)

# spines
for spine in ['top', 'right', 'bottom', 'left']:
    ax.spines[spine].set_linewidth(1.5)
ax.spines[:].set_visible(True)

# legend
handle1 = matplotlib.patches.Patch(facecolor='green', label='Xovee', ec='grey', linestyle='dotted')
ax.legend(handles=[handle1, handle2], fontsize=14, loc='lower left', ncol=2, frameon=True/False, bbox_to_anchor=(.5, 1.22))

# colors
cmap = plt.cm.tab20
twenty_colors = [c for c in cmap.colors]
cbar.outline.set_linewidth(1.5)
cbar.dividers.set_color('green')

# grid
ax.grid(linestyle='--', color='lightgray', linewidth=.5, alpha=.5, zorder=0)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Xovee

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值