5

The problem is connected with the matplotlib bar chart. To be specific, when I draw a bar chart some of the bars are not visible.

Dots and bars uses the same data series so should be in the same places Dots and bars uses the same data series so should be in the same places.

I do not know what is the source of this problem that is why I am posting this question. I have also noticed that for the same plot, when I enlarge the window to the full screen the missing bars magically appears

The same data series as in the first picture, just full screen window The same data series as in the first picture, just full screen window.

The same happens also when I change the color of the plot, so lets say I use:

artist = plot.bar(x_data_series, y_data_series, color="black")

Right now I have this not complete chart, but when I use (just setting the color for the second time right after):

artist = plot.bar(x_data_series, y_data_series, color="black")
matplotlib.artist.setp(artist, color="black")

I get this:

Now all bars are visible Now all bars are visible.

The same happens when I zoom in this incomplete chart:

Here also all bars are visible Here also all bars are visible.

What is more the "invisible bars", when they are visible, they seem to be a little bit thicker than the others, however they width in the artist properties is the same as you can see here:

One of this bars is the thicker one and the other the "normal" one, both are 0.8 in width One of this bars is the thicker one and the other the "normal" one, both are 0.8 in width.

Just to give you more information this is a graph embedded in the tkinter. Here are the figure, canvas, and axes (parent frame is just a frame in which the plot is placed):

figure = Figure(dpi=100)
plot = figure.add_subplot(111)
canvas = FigureCanvasTkAgg(figure, parent_frame)

The "work arounds" I have found are not satisfactory for me and I would really like to find the source of the problem, plus the width problem is something I can't figure out.

furas
  • 134,197
  • 12
  • 106
  • 148
Mariusz Ch.
  • 51
  • 1
  • 5
  • Are the images embedded for anyone? In its current state I'm only able to see the images using right click and "Open Image in New Tab" option and also image information are written twice? – Nae Dec 10 '17 at 21:09

1 Answers1

4

A bar width of 0.8 means that bars are 0.8 data units wide. Showing ~500 data units on the graph which is ~500 pixels wide means a bar is ~0.8 pixels wide. Or in other words, chances to see the bar are 80% or on average, every fifth bar may not be shown. So the result is rather expected.

Same when you zoom in. Chances are high to see the bar cover one pixel, but some bars are 2 pixels wide, because the bar width is somewhere in between 1 and 2 pixels.

The reason you see every bar when using setp(artist, color="black") is that the color applies to the bar and its edge. The edge is 1 point, i.e. a little bigger than a pixel, such that each bar is visible.

At the end it seems you either want a bar graph. In that case, make the bar become a real bar with some meaningful width in data units. Or, you may want a line graph. I.e. plotting a stem plot may make sense here.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712