简体   繁体   中英

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 在此处输入图片说明

And I want : 在此处输入图片说明

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

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")

在此处输入图片说明

The histogram can be achieved with:

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

在此处输入图片说明

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM