Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7727425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:27:21+00:00 2026-06-01T05:27:21+00:00

I’d like to create a broken vertical bar graph in matplotlib. To give a

  • 0

I’d like to create a broken vertical bar graph in matplotlib.

To give a better idea of the result I’m after, I put an example together with Balsamiq:

enter image description here

I’ve had a look at the matpltolib docs and examples but I can’t seem to find the appropriate chart type to use. The only thing that looks remotely similar is the boxplot but this isn’t what I need.

  • I’d rather not have to draw the graph manually with graphics primitive.
  • I can massage the data into shape as needed.

PS: If you know of a good library that does this in another language (javascript, for example), I’d be grateful for the pointer too.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T05:27:22+00:00Added an answer on June 1, 2026 at 5:27 am

    It sounds like you have a few series of start datetimes and stop datetimes.

    In that case, just use bar to plot things, and tell matplotlib that the axes are dates.

    To get the times, you can exploit the fact that matplotlib’s internal date format is a float where each integer corresponds to 0:00 of that day. Therefore, to get the times, we can just do times = dates % 1.

    As an example (90% of this is generating and manipulating dates. The plotting is just a single call to 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, stops, 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()
    

    enter image description here

    On a side note, for events that start on one day and end on the next, this will extend the y-axis into the next day. You can handle it in other ways if you prefer, but I think this is the simplest option.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.