简体   繁体   中英

plotting parallel events in python

I have 5 types of events: A,B,C,D,E

And a timeline between 0 and 100 These events can "start" and "end" any number of times.

To make it easier, that data is built as follows: A = [(0,3), (50,58)] B = [(40,60)] ... (lists, with 2-tuples representing start and end time)

I want to plot then as vertical bars with the label AE are the y axis. I think it's like a regular vertical bar, except there can be multiple bars (matching each start-end)

Something like this: 在此处输入图片说明

Thx!

I think using LineCollection should be a good idea. See code below.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

A = [(0,3), (50,58)]
B = [(40,60)]

def plot_event(ax, A, i, **kwargs):
    segs = [[(x, i), (y, i)] for (x, y) in A]
    line_segments = LineCollection(segs, lw=10, **kwargs)
    ax.add_collection(line_segments)

colors = ['r', 'g', 'b', 'k', 'crimson']
fig, ax = plt.subplots(1, 1, figsize=(7.2, 7.2))
for j, (i, event) in enumerate(zip([10, 30, 50, 70, 90], [A, B, A, B, A])):
    plot_event(ax, event, i, color=colors[j])
    ax.axhline(i, lw=2, color='gray', alpha=0.8, zorder=-1)
ax.set_xlim(0, 100)
ax.set_ylim(0, 100)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM