1

I want to plot segment lines with date data as x axis, and segment line shouldn't overlap with each other. However, there are some overlaps in the result figure.

Here is datafile named as data.csv

event_start,event_end,event_summary,posture
07:30,07:35,setting up desk,2
07:35,07:47,"fill water bottle, wash mug -> toilet",3
07:47,10:20,work( computer work + work discussion with office mate at desk),2
10:20,10:25,toilet,3
10:25,10:42,work,2
11:42,11:44,go find supervisor ,3
11:44,13:00,work (work discussion with supervisor at desk + computer work),2
13:00,13:30,toilet --> get lunch,2
13:30,14:00,Eat lunch,2
14:00,14:05,clean up,3
14:06,14:51,work (computer work + skype),2
14:51,14:59,toilet -> kitchen to fill ater bottle -> printing,3
14:59,16:31,work,2
16:31,16:42,toilet,3
16:42,17:15,work,2

Here is the code for plotting

import pandas as pd
import matplotlib.pyplot as plt
# plt.style.use('ggplot')

def plot_event(file_name, y_min=0, y_max=5):
    df = pd.read_csv(file_name)
    df['event_start'] = pd.to_datetime(df['event_start'])
    df['event_end'] = pd.to_datetime(df['event_end'])

    xs = zip(df['event_start'], df['event_end'])
    ys = zip(df['posture'], df['posture'])
    plt.ylim(y_min, y_max)
    for x, y in zip(xs, ys):
        plt.plot(x, y, linewidth=linewidth)
    plt.show()


if __name__ == '__main__':
    file_name = 'data.csv'
    y_min = 1
    y_max = 5
    linewidth = 8
    plot_event(file_name, y_min, y_max)

Here is the figure, and there are overlapping between different segments. It is wired because the date is not overlapped. enter image description here

1 Answer 1

3

This might be caused by the lines default drawing style.

You may want to play with solid_capstyle, and solid_joinstyle parameters to plt.plot(). For example this:

plt.plot(x, y, linewidth=linewidth, solid_capstyle='butt')

Will yeild:

enter image description here

You also might consider playing with linewidth as it adds to overlap, as well as other settings for line formatting which you can see in the docs

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.