如何在matplotlib中创建损坏的垂直条形图?
发布时间:2020-12-30 16:58:27 所属栏目:Python 来源:互联网
导读:我想在matplotlib中创建一个破碎的垂直条形图. 为了更好地了解我所追求的结果,我和Balsamiq一起举了一个例子: 我看过matpltolib docs和examples,但我似乎找不到合适的图表类型.唯一看起来很相似的是boxplot,但这不是我需要的. 我宁愿不必使用图形基元手动绘
|
我想在matplotlib中创建一个破碎的垂直条形图. 为了更好地了解我所追求的结果,我和Balsamiq一起举了一个例子: 我看过matpltolib docs和examples,但我似乎找不到合适的图表类型.唯一看起来很相似的是boxplot,但这不是我需要的. >我宁愿不必使用图形基元手动绘制图形. PS:如果你知道一个好的库以另一种语言(例如javascript)执行此操作,我也会对指针感激不尽. 解决方法听起来你有几个系列的开始日期时间和停止日期时间.在这种情况下,只需使用条形图来绘制内容,并告诉matplotlib轴是日期. 为了获得时间,您可以利用matplotlib的内部日期格式是浮点数的事实,其中每个整数对应于当天的0:00.因此,为了获得时间,我们可以做的时间=日期%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,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轴延伸到第二天.如果您愿意,可以通过其他方式处理它,但我认为这是最简单的选择. (编辑:哈尔滨站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读
