Search code examples
pythonmatplotlib

Making a bar plot using matplotlib.pyplot


I started working with python a couple of weeks ago and thus my knowledge of python is low.

I wish to plot a bar plot with some continuous data, but do not want to fill it with any color. I want to see only the final edges like a line histogram. I can not use white color because I will be overlapping different bar plots with errorbar plots in the same canvas. Any clue on how should I change it to a line histogram and set its line color?

For x-axis, I have used numpy.array. For y-axis, the height I need is in numpy.histogram form.

I am using following method:

import matplotlib.pyplot as plt

plt.bar(np_array_bins, np_histogram,width=binwidth,
        label='data', alpha=0.5, edgecolor = 'red', 
        color = 'green', linewidth=0.1)

I can't put the real data online but what I have is something like enter image description here

And I want : enter image description here

Neglect the data shown. I am concerned only about the style of the plot. Thanks!


Solution

  • From your description it sounds like you want a step plot or a histogram.

    The step plot can be achieved with:

    matplotlib.pyplot.step(np_array_bins, np_histogram, color="red", label="data")
    

    enter image description here

    The histogram can be achieved with:

    matplotlib.pyplot.hist(values, histtype="step", edgecolor="red", label="data")
    

    enter image description here

    The important parameter here is histtype, setting it to step draws an unfilled line around the histogram. In this example your original array would be passed in and matplotlib would calculate the bin edges. You can define the bin edges yourself and set other parameters to get more control over the final plot, these are described in the matplotlib docs.