ad

在matplotlib中绘制2进制时间表-英雄云拓展知识分享

匿名投稿 201 2024-01-22

我正在尝试使用matplotlib绘制2进制时间轴(不过,某些情况下,我或许可以斟酌替换库)。

现在,“2进制时间表”是指“年代事件的显示,事件空间是由两个相反事件组成的”。这样的事件空间的一个例子 {no_one_in_the_team_is_sick, at_least_one_person_in_the_team_is_sick}.

在matplotlib中绘制2进制时间表-英雄云拓展知识分享

我想复制的表示情势是(我使用D3做到了):

我已尝摸索索堆叠水平条的使用,但这明显不是工作的正确工具。

是不是可以更轻松和/或更正确的方法来实现这一结果?

看答案

您可使用 broken_barh绘制2进制时间表。

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

import matplotlib.dates

#create a time series s with dates as index and 0 and 1 for events

dates = pd.date_range("2017-04-01","2017-06⑴5", freq="D")

events = np.random.random_integers(0,1,size=len(dates))

s = pd.Series(events, index=dates)

fig, ax= plt.subplots(figsize=(6,2))

# plot green for event==1

s1 = s[s == 1]

inxval = matplotlib.dates.date2num(s1.index.to_pydatetime())

times= zip(inxval, np.ones(len(s1)))

plt.broken_barh(times, (⑴,1), color="green")

# plot red for event==0

s2 = s[s == 0]

inxval = matplotlib.dates.date2num(s2.index.to_pydatetime())

times= zip(inxval, np.ones(len(s2)))

plt.broken_barh(times, (⑴,1), color="red")

#format axes

ax.margins(0)

ax.set_yticks([])

ax.xaxis.set_major_locator(matplotlib.dates.MonthLocator())

ax.xaxis.set_minor_locator(matplotlib.dates.DayLocator())

monthFmt = matplotlib.dates.DateFormatter("%b")

ax.xaxis.set_major_formatter(monthFmt)

plt.tight_layout()

plt.show()


🚀🌟 点击注册 免费试用超级应用平台-英雄云企业级hpapaas 🌟🚀 😃👉🌐

免责声明:

本网址(www.yingxiongyun.com)发布的材料主要源于独立创作和网友匿名投稿。此处提供的所有信息仅供参考之用。我们致力于提供准确且可信的信息,但不对材料的完整性或真实性作出任何保证。用户应自行验证相关信息的正确性,并对其决策承担全部责任。对于由于信息的错误、不准确或遗漏所造成的任何损失,本网址不承担任何法律责任。本网站所展示的所有内容,如文字、图像、标志、音频、视频、软件和程序等的版权均属于原创作者。如果任何组织或个人认为网站内容可能侵犯其知识产权,或包含不准确之处,请即刻联系我们进行相应处理。

标签:Python matplotlib
上一篇:Apache重写抛弃setInputfilter deflate config指令-英雄云拓展知识分享
下一篇:bash-在目录中查找重复文件并重命名-英雄云拓展知识分享
相关文章

 发表评论

暂时没有评论,来抢沙发吧~

×