简体   繁体   中英

Why is pyplot's histogram giving two different colors?

edit: What I am trying to reproduce is this histogram: enter image description here .

So I'm plotting a pretty large dataset of two different sets of times. But I'm getting a histogram with blue and orange bars.

在此处输入图片说明

Everywhere else I looked to see if someone has asked/answered this question yields results for wanting to change the colors. However, every other time I've used hist, I just get blue bars as default.

Here is my code:

filename='24.txt'
data=np.genfromtxt(filename, usecols=(0,3), skip_header=4)
bins=15    
entries, edges, _ = plt.hist(data,bins,)

This is a relevant sample of my text file

You are reading 2 columns in the file, so data has a shape (n, 2) where n is the number of lines. When calling the hist() function, it makes 2 histograms, one for each column.

If you want an histogram of the first of your 2 selected columns, you can use plt.hist(data[:,0],bins) .

The problem is that you are importing 2 columns from your text file, ie, column 0 and column 3. Now your data contains two columns. When you plot a histogram using hist , it will plot two histograms: one for each column.

That is the reason you see two sets of bars: orange and blue. The histogram is plotted for a single dataset to see its distribution. It seems that you want two separate plots, each having a histogram of a single column.

To see a histogram with single color, you need to do

entries, edges, _ = plt.hist(data[:,0],bins) 

for the first column (column index 0) and

entries, edges, _ = plt.hist(data[:,1],bins)

for the fourth column (column index 3)

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