如何在matplotlib中创建损坏的垂直条形图?

bri*_*ice 6 python charts graph matplotlib

我想在matplotlib中创建一个破碎的垂直条形图.

为了更好地了解我所追求的结果,我和Balsamiq一起举了一个例子:

在此输入图像描述

我已经看过matpltolib 文档示例,但我似乎找不到合适的图表类型.唯一看起来很相似的是盒子图,但这不是我需要的.

  • 我宁愿不必用图形基元手动绘制图形.
  • 我可以根据需要按摩数据.

PS:如果你知道一个好的库以另一种语言(例如javascript)执行此操作,我也会对指针感激不尽.

Joe*_*ton 11

听起来你有几个系列的开始日期时间和停止日期时间.

在这种情况下,只需用于bar绘制事物,并告诉matplotlib轴是日期.

为了获得时间,您可以利用matplotlib的内部日期格式是浮点数的事实,其中每个整数对应于当天的0:00.因此,为了获得时代,我们可以做到times = dates % 1.

举个例子(其中90%是生成和操作日期.绘图只是一次调用bar.):

import datetime as dt
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

def main():
    start, stop = dt.datetime(2012,3,1), dt.datetime(2012,4,1)

    fig, ax = plt.subplots()
    for color in ['blue', 'red', 'green']:
        starts, stops = generate_data(start, stop)
        plot_durations(starts, stops, ax, facecolor=color, alpha=0.5)
    plt.show()

def plot_durations(starts, stops, ax=None, **kwargs):
    if ax is None:
        ax = plt.gca()
    # Make the default alignment center, unless specified otherwise
    kwargs['align'] = kwargs.get('align', 'center')

    # Convert things to matplotlib's internal date format...
    starts, stops = mpl.dates.date2num(starts), mpl.dates.date2num(stops)

    # Break things into start days and start times 
    start_times = starts % 1
    start_days = starts - start_times
    durations = stops - starts
    start_times += int(starts[0]) # So that we have a valid date...

    # Plot the bars
    artist = ax.bar(start_days, durations, bottom=start_times, **kwargs)

    # Tell matplotlib to treat the axes as dates...
    ax.xaxis_date()
    ax.yaxis_date()
    ax.figure.autofmt_xdate()
    return artist

def generate_data(start, stop):
    """Generate some random data..."""
    # Make a series of events 1 day apart
    starts = mpl.dates.drange(start, stop, dt.timedelta(days=1))

    # Vary the datetimes so that they occur at random times
    # Remember, 1.0 is equivalent to 1 day in this case...
    starts += np.random.random(starts.size)

    # Make some random stopping times...
    stops = starts + 0.2 * np.random.random(starts.size)

    # Convert back to datetime objects...
    return mpl.dates.num2date(starts), mpl.dates.num2date(stops)

if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

在旁注中,对于从一天开始到下一天结束的事件,这将使y轴延伸到第二天.如果您愿意,可以通过其他方式处理它,但我认为这是最简单的选择.