 # Bar graph performance

Hi,

I am new to matplotlib. So if I ask sth stupid, please bear with me.

I am using matplotlib to present large data set in different graph types,
bar, dot, line, etc. I find that the bar graph has very bad performance.
Say, I draw data points of about ten thousand. Using dot graph, it draws
in a second. But using bar graph, it draws in tens of seconds.

I was wondering what causes this difference. Is there a way to improve the
bar graph performace? (Maybe I am not drawing it right, then, please give
me a pointer)

Below is a simple example:

from np.random import uniform
from numpy.random import uniform
x = uniform(0, 10, 14000)
y = uniform(0, 100, 14000)
plt.plot(x, y, 'bo')
plt.bar(x, y)

Thanks,
Kun

Hi,

I am new to matplotlib. So if I ask sth stupid, please bear with me.

I am using matplotlib to present large data set in different graph
types,
bar, dot, line, etc. I find that the bar graph has very bad performance.
Say, I draw data points of about ten thousand. Using dot graph, it draws
in a second. But using bar graph, it draws in tens of seconds.

I was wondering what causes this difference. Is there a way to improve
the
bar graph performace? (Maybe I am not drawing it right, then, please
give
me a pointer)

Bar is intended for plots in which the bars are individually visible, so it makes sense only for a small number--say 10 or 20 bars. It is implemented using an individual patch for each bar, and this is inherently slow in mpl--but for under 100 bars, it doesn't matter at all.

If you have a large number of points, maybe what you are looking for is fill() or fill_between(). Check the links for these in http://matplotlib.sourceforge.net/.

Eric

···

On 05/02/2010 05:48 PM, Kun Hong wrote:

Below is a simple example:

from np.random import uniform
from numpy.random import uniform
x = uniform(0, 10, 14000)
y = uniform(0, 100, 14000)
plt.plot(x, y, 'bo')
plt.bar(x, y)

Thanks,
Kun

Hi,

I am new to matplotlib. So if I ask sth stupid, please bear with me.

I am using matplotlib to present large data set in different graph
types,
bar, dot, line, etc. I find that the bar graph has very bad performance.
Say, I draw data points of about ten thousand. Using dot graph, it draws
in a second. But using bar graph, it draws in tens of seconds.

I was wondering what causes this difference. Is there a way to improve
the
bar graph performace? (Maybe I am not drawing it right, then, please
give
me a pointer)

Also check out step().
http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.step

Eric

···

On 05/02/2010 05:48 PM, Kun Hong wrote:

Below is a simple example:

from np.random import uniform
from numpy.random import uniform
x = uniform(0, 10, 14000)
y = uniform(0, 100, 14000)
plt.plot(x, y, 'bo')
plt.bar(x, y)

Thanks,
Kun

------------------------------------------------------------------------------
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Eric,

Thanks a lot for the pointers. Sorry for the double posting.

I tried fill_between, which works better than bar graph.
But I need to change the data set to be able to get the filling
into a nicely-formed rectangle, and the performance is still not very good.

As the below example shows:

import matplotlib.mlab as mlab
from matplotlib.pyplot import figure, show
import numpy as np

x1 = np.arange(0.0, 10000.0, 0.1)
y1 = np.sin(2*np.pi*x1)

fig = figure()

x = []
for i in x1:
x += [i-0.05, i-0.05, i, i+0.05, i+0.05]

y = []
for i in y1:
y += [0, i, i, i, 0]

ax1.fill_between(x, 0, y)

I have also tried step, but it doesn't seem to be able
to fill the rectangular area. Am I missing something?

Kun

Eric Firing wrote:

···

On 05/02/2010 05:48 PM, Kun Hong wrote:

Hi,

I am new to matplotlib. So if I ask sth stupid, please bear with me.

I am using matplotlib to present large data set in different graph
types,
bar, dot, line, etc. I find that the bar graph has very bad performance.
Say, I draw data points of about ten thousand. Using dot graph, it draws
in a second. But using bar graph, it draws in tens of seconds.

I was wondering what causes this difference. Is there a way to improve
the
bar graph performace? (Maybe I am not drawing it right, then, please
give
me a pointer)

Also check out step().
http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.step

Eric

Eric,

Thanks a lot for the pointers. Sorry for the double posting.

I tried fill_between, which works better than bar graph.
But I need to change the data set to be able to get the filling
into a nicely-formed rectangle, and the performance is still not very good.

As the below example shows:

import matplotlib.mlab as mlab
from matplotlib.pyplot import figure, show
import numpy as np

x1 = np.arange(0.0, 10000.0, 0.1)
y1 = np.sin(2*np.pi*x1)

fig = figure()

x = []
for i in x1:
x += [i-0.05, i-0.05, i, i+0.05, i+0.05]

y = []
for i in y1:
y += [0, i, i, i, 0]

ax1.fill_between(x, 0, y)

I have also tried step, but it doesn't seem to be able
to fill the rectangular area. Am I missing something?

Given that you want filled regions, step won't help. It might make sense to make the step logic available to fill_between, but this has not been done yet.

I don't understand what you really want, though; your code above is trying to plot 100,000 bars. Your screen probably has fewer than 2000 pixels width. You can print with higher resolution than that, but if you are making plots for printing, usually the performance is not such an issue--and even then, I don't think that packing 100,000 bars onto a sheet of paper is going to be very useful.

Eric

···

On 05/03/2010 11:45 PM, Kun Hong wrote:

Kun

Eric Firing wrote:

>>
>> I have also tried step, but it doesn't seem to be able
>> to fill the rectangular area. Am I missing something?
>>
>
> Given that you want filled regions, step won't help. It might make
> sense to make the step logic available to fill_between, but this has not
> been done yet.
>

This will be more elegant. Hope it can be implemented eventually.

> I don't understand what you really want, though; your code above is
> trying to plot 100,000 bars. Your screen probably has fewer than 2000
> pixels width. You can print with higher resolution than that, but if
> you are making plots for printing, usually the performance is not such
> an issue--and even then, I don't think that packing 100,000 bars onto a
> sheet of paper is going to be very useful.
>

Eric, you are right. But the above is just a sample I put up quickly to test.
My real program does need to display filled step regions for 10000+ data
points. But I guess, I can live with displaying the first 1000 points in the
initial show of the graph, after confirming the performance is expected for
this amount of data. Thanks a lot for the help.

Kun

···

On 05/03/2010 11:45 PM, Kun Hong wrote: