Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python – How to always plot up to 100% on bar chart

I have a a snippet of Python code to plot a very basic bar graph which represents a list containing pre-calculated percentages:

def plot_errors_percent_bar_graph(list):
    fig, ax = plt.subplots()
    x_labels = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
    ax.bar(x_labels, list, color='green')
    ax.set_ylabel('Percentage of Number of Errors')
    ax.set_xlabel('Labels')
    ax.set_title('Total Percentage Number of Errors')
    plt.show(block=False)
    plt.pause(0.001)

The graph only shows the highest percentage from the list on the y-axis (e.g. 80% on the plot shown below):

Example Graph

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I always make the graph show from 0% to 100% even if the list doesn’t contain a value near that?

>Solution :

With matplotlib you can define the limits of values on each axis. To solve your problem, just add the line ax.set_ylim(ymin=0, ymax=100) in your fonction.

Matplotlib image

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading