您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

5b51 2022/1/14 8:22:49 python 字数 5383 阅读 572 来源 www.jb51.cc/python

我想在matplotlib中创建一个破碎的垂直条形图. 为了更好地了解我所追求的结果,我和Balsamiq一起举了一个例子: 我看过matpltolib docs和examples,但我似乎找不到合适的图表类型.唯一看起来很相似的是boxplot,但这不是我需要的. >我宁愿不必使用图形基元手动绘制图形. >我可以根据需要按摩数据. PS:如果你知道一个好的库以另一种语言(例如javascript

概述

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

我看过matpltolib docsexamples,但我似乎找不到合适的图表类型.唯一看起来很相似的是@L_404_3@,但这不是我需要的.@H_502_3@

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

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

在这种情况下,只需使用条形图来绘制内容,并告诉matplotlib轴是日期.@H_502_3@

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

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

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

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

总结

以上是编程之家为你收集整理的如何在matplotlib中创建损坏的垂直条形图?全部内容,希望文章能够帮你解决如何在matplotlib中创建损坏的垂直条形图?所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶