首页 > 解决方案 > 用于多变量绘图的 Python Matplotlib/Seaborn 模板

问题描述

我知道了一个美丽的情节,我在下面复制它。我想知道用于这种情节的模板。

在此处输入图像描述

标签: pythonpandasdataframematplotlibseaborn

解决方案


看起来没有可用的 seaborn 模板,但您可以使用 matplotlib 子图和注释来生成类似的图。

我认为一般的方法是填充所有曲线之间的区域,并注释最小值和最大值。这是使用具有不同标准偏差的高斯曲线生成中间子图的示例。

import pandas as pd
import numpy as np
from scipy.signal.windows import gaussian
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

## create some simple gaussian curves
x = pd.date_range("2020-01-01", "2020-12-31", freq="D")
y_curves = [gaussian(len(x), std=s) for s in [40,80,120,160]]
colors = ["OrangeRed","Orchid","PeachPuff","LightGoldenRodYellow"]

y_max = max([max(y) for y in y_curves])

## loop through pairs of curves, filling between them
for i, y in enumerate(y_curves):
    if i == 0:
        plt.fill_between(x,y,color=colors[i])
    elif i == 2:
        plt.plot(x, y, color="black", linewidth=0.5)
        plt.fill_between(x,y,y_curves[i-1],color=colors[i])

        ## annotate max value
        y_max = max(y)
        y_max_idx = y.tolist().index(max(y))
        x_max = x[y_max_idx] 
        plt.annotate(text="muggy: 100%", xy=(x_max, y_max-0.05))

    else:
        plt.fill_between(x,y,y_curves[i-1],color=colors[i])

# Set the locator
locator = mdates.MonthLocator()  # every month
# Specify the format - %b gives us Jan, Feb...
fmt = mdates.DateFormatter('%b')

X = plt.gca().xaxis
X.set_major_locator(locator)
# Specify formatter
X.set_major_formatter(fmt)

plt.xlim([x[0], x[-1]])
plt.ylim([0, 1])
plt.show()

在此处输入图像描述


推荐阅读