0

I have a strange behavior using matplotlib in an ipython notebook: when I change the attribute "color" of the barchart, the result is ugly, I have some bars in red and some others in black. This is happening only when I have a high amount of bars to display (>100).

You can execute the code below to reproduce the problem, playing with the dataPoints parameter to see the effect of the number of bars:

import random
import matplotlib.pyplot as plt

dataPoints = 400
data = random.sample(range(300, 1000), dataPoints)
xCoords = range(dataPoints)

fig = plt.figure(figsize=[13,9])
plt.bar(xCoords,data,color='red')
plt.show()

Here is an example of the result :

Output

2 Answers 2

3

It is not the problem of pyplot, nor is it the problem in your code or your version of iPython.

It is just the problem of resolution. Since you are adding so many dataPoints (400 in your case), you are creating too many bins!! This makes it overlap in the default plot that is generated.

The default barchart plot with 200 dataPoints looks like this

enter image description here

But when you zoom in to take a look, you can see that your bins are actually seperated and the black lines are now not clearly visible.

enter image description here

The above is the same execution of your program with 200 dataPoints.

Sign up to request clarification or add additional context in comments.

3 Comments

Small correction: they don't "disappear", they're still there as I think they're due to the borders of the bars, which are obviously still there.
@Ffisegydd: well yes you are true. but I think this is the only answer!! I will add the suggested edit
No I agree you're correct :) it's just the black isn't disappearing, it's just not a problem anymore (it was more of a nit-picking correction than a "You're wrong!" correction)
1

If you zoom in on the bars, you will see that actually, there are no black bars. The black you see is the result of the black border around the bars and interpolation to fit the pixels of your screen.

What you can do to have only red in your image is changing the color of the border to red using

plt.bar(xCoords, data, color='red', edgecolor='red')

1 Comment

The edgecolor param helps to have a much cleaner result, thanks !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.