python (matplotlib) 中的龙卷风图和 p10-p90

Sal*_*874 2 python plot matplotlib

我需要使用 Python (matplotlib) 绘制以下两件事:

  1. 龙卷风图(有点总结敏感性分析)
  2. 并对一系列产品的 p10 和 p90 平均值进行比较。

我以前从未这样做过,并尝试使用 Python (matplotlib)。到目前为止还没有成功。附上我正在/正在尝试绘制的内容。我用手画了它们,因为我只是觉得用 Python 直观地解释我想要绘制的内容对我来说可能更容易。

一件重要的事情是,在龙卷风图表中,我希望看到在图表中心划分的线,顶部是基本案例编号(值范围从 2000 到 5000),以及我的每个产品的值分别在右侧。我发现了一些非常漂亮的龙卷风图表,这些图表看起来非常酷,但太长且复杂(其中有很多时髦和酷的东西,并且专门用于该特定图表)。

Mar*_*iet 5

不幸的是,matplotlib 没有内置的龙卷风图表功能。你必须自己动手。这是我尝试制作类似于您的绘图的情节。

import numpy as np
from matplotlib import pyplot as plt

###############################################################################
# The data (change all of this to your actual data, this is just a mockup)
variables = [
    'apple',
    'juice',
    'orange',
    'peach',
    'gum',
    'stones',
    'bags',
    'lamps',
]

base = 3000

lows = np.array([
    base - 246 / 2,
    base - 1633 / 2,
    base - 500 / 2,
    base - 150 / 2,
    base - 35 / 2,
    base - 36 / 2,
    base - 43 / 2,
    base - 37 / 2,
])

values = np.array([
    246,
    1633,
    500,
    150,
    35,
    36,
    43,
    37,
])

###############################################################################
# The actual drawing part

# The y position for each variable
ys = range(len(values))[::-1]  # top to bottom

# Plot the bars, one by one
for y, low, value in zip(ys, lows, values):
    # The width of the 'low' and 'high' pieces
    low_width = base - low
    high_width = low + value - base

    # Each bar is a "broken" horizontal bar chart
    plt.broken_barh(
        [(low, low_width), (base, high_width)],
        (y - 0.4, 0.8),
        facecolors=['white', 'white'],  # Try different colors if you like
        edgecolors=['black', 'black'],
        linewidth=1,
    )

    # Display the value as text. It should be positioned in the center of
    # the 'high' bar, except if there isn't any room there, then it should be
    # next to bar instead.
    x = base + high_width / 2
    if x <= base + 50:
        x = base + high_width + 50
    plt.text(x, y, str(value), va='center', ha='center')

# Draw a vertical line down the middle
plt.axvline(base, color='black')

# Position the x-axis on the top, hide all the other spines (=axis lines)
axes = plt.gca()  # (gca = get current axes)
axes.spines['left'].set_visible(False)
axes.spines['right'].set_visible(False)
axes.spines['bottom'].set_visible(False)
axes.xaxis.set_ticks_position('top')

# Make the y-axis display the variables
plt.yticks(ys, variables)

# Set the portion of the x- and y-axes to show
plt.xlim(base - 1000, base + 1000)
plt.ylim(-1, len(variables))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述