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)