0

I'm completely new to matplotlib, and I wanted to create a simple histogram of a fly's various positions within a test chamber. I seem to have done this, but for some reason the center of the histogram has condensed orange bars. I'm not sure why this is happening. (Btw I know I'm using lists instead of np arrays, but changing this doesn't remedy the error). Sorry if this is really simple, but I can't seem to find anything online adressing it.

import matplotlib.pyplot as plt
import _pickle           as pickle
import numpy             as np

def loadFile(pname):
with open(pname, 'r+b') as file:
    pos = pickle.load(file)
    return pos

#specifies which fly out of the many tracked
fly   = 2

pos   = loadFile('/Users/JKTechnical/Codes/FlyWork/posTester.vD')

#transpose the list into x and y values
t_pos = list(zip(*pos[fly]))
x     = t_pos[0]
y     = t_pos[1]

fig, axs = plt.subplots(1,1)

axs.hist(x, bins=20)
axs.hist(y, bins=20)
plt.show()

Here's what I get:

output

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
hiMom
  • 93
  • 1
  • 1
  • 5
  • 2
    Is the loadfile method not indented properly? Looks like the three lines that follow are missing a leading tab – Easton Bornemeier Jul 10 '18 at 14:45
  • 2
    You are plotting two histograms here, one of the x values and one of the y values. Not sure what you intend to do though. – ImportanceOfBeingErnest Jul 10 '18 at 14:48
  • @hiMom: Perhaps you are trying to [make a heatmap](https://stackoverflow.com/q/2369492/190597)? – unutbu Jul 10 '18 at 14:51
  • 1
    @hiMom: The code pasted above contains both tabs and spaces. Be careful -- using both tabs and spaces tends to lead to IndentationErrors. It's better to [find a text editor](https://wiki.python.org/moin/PythonEditors) which automatically converts tabs to, say, 4 spaces. (Most Python programmers use only spaces, no tabs.) – unutbu Jul 10 '18 at 14:54

1 Answers1

0

The code you are writing will create an overlapping histogram. The histogram for list y would overlap the histogram with the list 'x'. The orange bars would be one of your histograms, probably the second one, which in this case is your list y.

Try changing your bin size for one of the histograms and you can identify which one represents the orange bars.

Just a note, posting sample data always helps in understanding and answering your question better.

Namratha
  • 32
  • 2
  • Due to the color order in matplotlib you already know that the histogram that is created second is the orange one (i.e. the one of the y values); no need to perform any bin size tests to find out. – ImportanceOfBeingErnest Jul 10 '18 at 17:43
  • @ImportanceOfBeingErnest That is good to know. I was not sure about that. – Namratha Jul 19 '18 at 16:07