技巧#

import matplotlib.pyplot as plt
import numpy as np

Zoom-in#

step = 0.1
x = np.arange(0, 10 + step, step)
y = x**2
_, ax = plt.subplots(figsize=[10, 8])
ax.plot(x, y)

axins = ax.inset_axes([0.1, 0.5, 0.4, 0.4])
axins.plot(x[:10], y[:10])

ax.indicate_inset_zoom(axins, lw=3)

axins.set_xticklabels("")
axins.set_yticklabels("")
plt.show()
../_images/f4499f08589a529ef336f50b0e8192adb9a22ba840b1529aa8c8753f48f54cd3.png

Table#

_, ax = plt.subplots(figsize=(10, 6))

x = np.random.rand(5, 8) * 0.7

ax.plot(x.mean(axis=0), "-o", label="average per column")
ax.set(xticks=[])
ax.table(
    cellText=[[f"{xxx}%1.2f" for xxx in xx] for xx in x],
    cellColours=plt.cm.GnBu(x),
    fontsize="large",
    loc="bottom",
)
plt.show()
../_images/9dff9af4eda62a7329ac4b0fc1ad88d39f49a06849e0af241f78f1739ae1ff4a.png

Broken Bars#

_, ax = plt.subplots(figsize=(10, 6))

x1 = [(5, 5), (20, 5), (20, 7)]
y1 = (2, 1)
ax.broken_barh(x1, y1, facecolors="green")

x2 = [(6, 2), (17, 5), (50, 2)]
y2 = (15, 1)
ax.broken_barh(x2, y2, facecolors="orange")

x3 = [(5, 2), (28, 5), (40, 2)]
y3 = (30, 1)
ax.broken_barh(x3, y3, facecolors="red")

ax.set(xlabel="Sales", ylabel="Days of the Month")
plt.show()
../_images/021dd332ca4817f722529f6e9873d4f43c0d83f6cf44a048fd7f0fa8c445cd72.png

Transform#