how to generate plots with error bars?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).

how to generate plots with error bars?

Postby dradorf » Thu Nov 18, 2004 8:15 am

In scientific applications one often needs to generate a line plot or a scatter plot with error bars. I am looking for information on how to do this using JFreeChart. Eventually, I'd like to invoke the functionality using the Cewolf taglib.

Here is an example:

x: 1, 2, 3, 4, 5
y: 3, 1, 2, 5, 4
errors: 0.5, 0.2, 0.3, 0.1, 0.4

Wanted: a scatter plot that draws a symbol (usually a circle) at the (x, y) position, and then for each point a little vertical bar centered on the point. The length of the bar is twice the indicated error.

I expect the functionality to exist in both JFreeChart and Cewolf, but I have searched in vain so far. A pointer to the spot in the documentation would be welcome. An example for a data set producer, or better a JSP would be even more welcome.

Thanks a lot.
Posts: 1
Joined: Thu Nov 18, 2004 8:06 am

error bars...

Postby rinkeh » Tue Mar 15, 2005 12:50 pm

I'm running into the same problem. did you solve this allready??

I'm not sure, but I get the impression that JFreeChart only supports error bars in Charts based on category Data (via the StatisticalBarRenderer class); it does not seem to support error bars in for example xy-line charts. Please correct me if I'm wrong, personally I would like to have those bars in a line chart.

As for cewolf: again just what I would want to do. However, it seems that Cewolf does not support error bars at all. Did you post this on the cewolf forums??

Postby david.gilbert » Tue Mar 15, 2005 1:55 pm

No-one has written the required renderer (and dataset interface) yet to do this with the XYPlot class, but it would be a nice addition.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
JFreeChart Project Leader
Posts: 11622
Joined: Fri Mar 14, 2003 10:29 am

Postby anthuan » Mon Mar 28, 2005 2:59 pm

Unfortunately, I've run into the same situation as you. But I've looked here and there through the code and... isn't it possible to merge the examples YInterval and XYLineAndShape to get the desired result? I mean, if we supply two DataSets to the plot won't it plot them both?

The only reason I ask this here is because both demos use the XYPlot class. Don't know if it will help, but it shouldn't hurt :)
Posts: 1
Joined: Mon Mar 28, 2005 2:55 pm

Postby doonoop » Sun Apr 17, 2005 9:43 am

I'm taking the same approach as you, it sounds like, anthuan. Not there quite yet (building a StdDevDataset class implementing IntervalXYDataset) but I think it should work from what I know about these renderers.

Next step after that, I guess, would be to write a renderer that actually plots error bars. I probably won't be doing that right now (beholden to the project at hand!), but as David keeps saying it would be a very worthy addition to the library so I'll see what I can do.

If someone can remind me by replying to this in a week or two, I'll put up whatever hack I come up with.
Posts: 4
Joined: Sun Apr 17, 2005 9:37 am

Building an error-bar-able line chart -- feedback?

Postby doonoop » Mon Apr 18, 2005 10:41 am

Thought I'd run this by the forum, to see if anyone has feedback or wants to warn me about duplicating someone else's effort.

I've subclassed XYDataItem, XYSeries, and XYSeriesCollection into SDDataItem, SDSeries, and SDSeriesCollection respectively. Instead of taking a pair of values they take a triple: (x, y, standard deviation).

Next I need a SDTimeSeriesCollection, and the components of that.

My ultimate goal is a Renderer that draws error-bars at each point along a line chart.

I want to know if anyone has any feedback regarding how to make this most useful for a publishable class. For example, the third value in my triple is a standard-deviation value, which I'll use to plot error bars for each point. I'm a statistics ignoramus, so I won't know if there's some more general concept that would be more useful to people once I'm done with my little task here. For example, YErrorDataItem instead of SDDataItem?

Posts: 4
Joined: Sun Apr 17, 2005 9:37 am

Postby jgould » Tue Apr 19, 2005 2:07 am

I think subclassing XYDataItem is a bad idea because you can't override the equals method in XYDataItem to take into account the error without violating the equals contract. I created a new series, dataset, and renderer for making error bar plots. I'd be happy to share my code with anyone who is interested.
Posts: 5
Joined: Wed Apr 13, 2005 10:08 pm

Postby doonoop » Tue Apr 19, 2005 5:01 am

Hm, I think I see your point on breaking the equals method -- good call. So I guess what I could do is just mimic the XYDataItem class and merge it with what was previously my SDDataItem content.

I'd love to see what you've done -- I think it would be great to get this function into the jfree library too.
Posts: 4
Joined: Sun Apr 17, 2005 9:37 am

Postby mrsouthern » Wed Apr 20, 2005 3:56 pm

Anyone willing to share source code on the forum? I too need to work on this problem. I am not closed source restricted and can submit any changes back myself. This *should* be in jfree!
Posts: 4
Joined: Wed Apr 20, 2005 3:52 pm

Postby doonoop » Wed Apr 20, 2005 10:13 pm

I'll get my source up on the web, and put a link here. I still haven't had a chance to make the change that jgould suggested, or anything else for that matter in the last 36 hours but I'm planning to put some code up tonight.

Bear in mind that I'm starting from the bottom up: SDDataItem first, and any renderer I do would be last. Not the one to ask about a renderer right now...
Posts: 4
Joined: Sun Apr 17, 2005 9:37 am

Code posted

Postby jgould » Mon Apr 25, 2005 10:41 pm

Posts: 5
Joined: Wed Apr 13, 2005 10:08 pm

Postby mrsouthern » Wed Apr 27, 2005 4:10 pm

Very cool jgould! Most helpful indeed.

Your code did almost everything I needed and really saved me a lot of time.

Here's a couple of alterations that might be useful...

1) added a removeAllSeries() method to XYErrorBarSeriesCollection (basically the same as XYSeriesCollection). Another reason to have a common superclass!

2) added a check to the XYErrorBarRenderer to see if the height of the error bar > 0. If not then return instead of drawing it...

IntervalXYDataset intervalDataset = (IntervalXYDataset) dataset;
double yLow = intervalDataset.getStartYValue(series, item);
double yHigh = intervalDataset.getEndYValue(series, item);
if( yHigh - yLow <= 0 )
Posts: 4
Joined: Wed Apr 20, 2005 3:52 pm

Postby Guest » Fri May 27, 2005 8:58 am

david.gilbert wrote:No-one has written the required renderer (and dataset interface) yet to do this with the XYPlot class, but it would be a nice addition.

Is this request is accepted officially?
I also need this feature. I tried jgould’s sample code, but the many other features such as connects line between points, auto rewrites, customization, etc. are needed.

Postby jgould » Fri May 27, 2005 3:13 pm

All featues that exist for line/scatter plots also exist in my sample code since my renderer inherits from StandardXYItemRenderer.
Posts: 5
Joined: Wed Apr 13, 2005 10:08 pm

Postby Guest » Mon May 30, 2005 2:04 am

I mean the property dialog doesn’t come up even if I push right button.

And XYErrorBarSeries.add(double,double,double,double) doesn’t update the graph dynamically. Although I add new data using this method, the graph doesn’t redraw.


Return to JFreeChart

Who is online

Users browsing this forum: Bing [Bot] and 8 guests