python中的龙卷风图和p10-p90(matplotlib) [英] A tornado chart and p10-p90 in python (matplotlib)

查看:89
本文介绍了python中的龙卷风图和p10-p90(matplotlib)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to plot the following two things using the Python (matplotlib):

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

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

I have never done this before and was trying to use Python (matplotlib). So far no success. Attached what I am/was trying to plot. I drew them by hand as I just thought it might be a lot easier for me to explain visually what I was trying to plot using the Python.

一件重要的事情是,在龙卷风图表中,我希望看到在图表中心划分的那条线,顶部是基本情况数字(值范围从 2000 到 5000),以及每个我的产品分别在右手边.我发现了一些非常好看的龙卷风图表,这些图表看起来很酷,但过于冗长和复杂(其中有很多时髦和酷的东西,并且专门用于该特定图表).

One important thing is that in the tornado chart I would like to see that line that divides on the center of the chart, and on the top are base case numbers (values ranging from 2000 to 5000), and the values for each of my product on the right hand side, respectively. I found some really good looking tornado charts, and those looked really cool, but too lengthy and complicated (lots of funky and cool things in that, and used specifically just for that particular chart).

推荐答案

遗憾的是,matplotlib 没有内置的龙卷风图表功能.你将不得不推出自己的.这是我尝试制作与您的绘图相似的情节.

Unfortunately, matplotlib has no build-in tornado chart function. You will have to roll your own. Here is my attempt at making a plot that resembles your drawing.

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))

这篇关于python中的龙卷风图和p10-p90(matplotlib)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆