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

  • SEARCH
  • Home
  • 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 9114961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:19:06+00:00 2026-06-17T04:19:06+00:00

Background: I’m working on a program to show a 2d cross section of 3d

  • 0

Background:

I’m working on a program to show a 2d cross section of 3d data. The data is stored in a simple text csv file in the format x, y, z1, z2, z3, etc. I take a start and end point and flick through the dataset (~110,000 lines) to create a line of points between these two locations, and dump them into an array. This works fine, and fairly quickly (takes about 0.3 seconds). To then display this line, I’ve been creating a matplotlib stacked bar chart. However, the total run time of the program is about 5.5 seconds. I’ve narrowed the bulk of it (3 seconds worth) down to the code below.
‘values’ is an array with the x, y and z values plus a leading identifier, which isn’t used in this part of the code. The first plt.bar is plotting the bar sections, and the second is used to create an arbitrary floor of -2000. In order to generate a continuous looking section, I’m using an interval between each bar of zero.

    import matplotlib.pyplot as plt

    for values in crossSection:
        prevNum = None
        layerColour = None
        if values != None:
            for i in range(3, len(values)):
                if values[i] != 'n':
                    num = float(values[i].strip())
                    
                    if prevNum != None:
                        plt.bar(spacing, prevNum-num, width=interval, \
                                bottom=num, color=layerColour, \
                                edgecolor=None, linewidth=0)
                        
                    prevNum = num
                    
                    layerColour = layerParams[i].strip()
                    
            if prevNum != None:        
                plt.bar(spacing, prevNum+2000, width=interval, bottom=-2000, \
                                    color=layerColour, linewidth=0)
        
        spacing += interval

I’m sure there’s a more efficient way to do this, but I’m new to Matplotlib and still unfamilar with its capabilities. The other main use of time in the code is:

plt.savefig('output.png')

which takes about a second, but I figure this is to be expected to save the file and I can’t do anything about it.

Question:

Is there a faster way of generating the same output (a stacked bar chart or something that looks like one) by using plt.bar() better, or a different Matplotlib function?

EDIT:
I forgot to mention in the original post that I’m using Python 3.2.3 and Matplotlib 1.2.0

  • 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-17T04:19:07+00:00Added an answer on June 17, 2026 at 4:19 am

    Leaving this here in case someone runs into the same problem…
    While not exactly the same as using bar(), with a sufficiently large dataset (large enough that using bar() takes a few seconds) the results are indistinguishable from stackplot(). If I sort the data into layers using the method given by tcaswell and feed it into stackplot() the chart is created in 0.2 seconds, rather than 3 seconds.

    EDIT

    Code provided by tcaswell to turn the data into layers:

    accum_values = []
    for values in crosssection:
        accum_values.append([float(v.strip()) for v iv values[3:]])
    accum_values = np.vstack(accum_values).T 
    layer_params = [l.strip() for l in layerParams]
    bottom = numpy.zeros(accum_values[0].shape)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background Hundreds of database objects (views, packages, stored procedures, etc.) in a system have
Background: I'm working on a program that needs to be able to capture the
Background: We're building an application that allows our customers to supply data in a
Background I am working with a monad built of a stack of transformers one
Background I am trying to build a Java program that make use of an
Background To replace invalid zip codes. Sample Data Consider the following data set: Typo
Background: I have uploaded a xls file using the FileField. Now I want to
Background I have written a simple WebGL puzzle game and I want to allow
Background I'm learning Objective-C and Cocoa, and I thought creating simple programs to answer
Background I first wanted to upload a file via json and get a response

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.