question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error bars on XYCharts when adding a series with standard deviations

See original GitHub issue

Hi, thanks for your great software.

I’m investigating using XChart, and I’ve come across what I’m guessing is problem in the error bar and display of XY Scatter graphs. It’s not clear to me what the units of the error bars are. I’ve assumed they were one standard deviation.

Regardless of this fact, I have found that the Y values when displayed with error bars are centered at a point approximately equal to the error bar value.

For example, if these values are added to List<Double> yValues and List<Double> errors, then I get the the chart that I have attached.

M = 0.0 SD = 0.0 M = 0.5 SD = 0.11785113019775793 M = 0.3333333333333333 SD = 0.09001028747788695 M = 0.25 SD = 0.07216878364870322 M = 0.2 SD = 0.06106058517348482 M = 0.3333333333333333 SD = 0.05328406170749724

In the Chart, I except that the first few Y values where it is centered to be 0.0, 0.5 and 0.3333333 respectively. This works perfectly without error bars. As soon as I add a set of doubles containing the standard deviation values, the plotted Y values are less, by approximately the standard deviation (see the image attached).

Screenshot from 2020-12-13 11-36-15

If it helps, here is a class I am messing about with to try to get this set up: When “addSeries” is called with the data and standard deviation values, I get the output in the image above.

package xcharttest;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import org.knowm.xchart.XChartPanel;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import org.knowm.xchart.XYSeries;
import org.knowm.xchart.XYSeries.XYSeriesRenderStyle;
import org.knowm.xchart.style.markers.SeriesMarkers;

public class ScatterChartMeanStandardDev
{
	// Up to six colours for six series
	private static final Color[] COLORS = new Color[] {Color.blue, Color.red,Color.green,Color.cyan,Color.orange,Color.black};
	private int currentColorIdx;
	private static final int MAX_COLORS = COLORS.length;
	
	private XYChart chart;
	
	public ScatterChartMeanStandardDev(String title,
									   String yLabel,
									   Dimension dimension)
	{
		currentColorIdx = 0;
		
	    this.chart = setChartStyle(title,
	    							  "Test Number",
	    							  yLabel, 
	    							  dimension);
	    
	}
	 
	
	public XYChart setChartStyle(String title, String xLabel, String yLabel, Dimension windowSize) 
	{
		
		title += " (Error bar = +/- 1 SD)";

		XYChart chart = new XYChartBuilder().width((int)windowSize.getWidth())
	    									.height((int)windowSize.getHeight())
	    									.title(title)
	    									.xAxisTitle(xLabel)
	    									.yAxisTitle(yLabel)
	    									.build();
	 
	    chart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Line);
	    chart.getStyler().setChartTitleVisible(true);
	    chart.getStyler().setLegendVisible(true);
	    chart.getStyler().setAxisTitlesVisible(true);
	    chart.getStyler().setXAxisDecimalPattern("0");
	    
	    return chart;
	}
	
	
	
	/**
	 * Add a series to the chart as indicated by the parameters
	 * 
	 * @param seriesName				String label that appears to identify the series line
	 * @param yValues					The Y values of the series (X is assumed to be test number)
	 * @param standardDeviations		The standard deviations ("error" = 68% of normally distributed data)
	 * 
	 * @return
	 */
	public void addSeries(String seriesName,
						  ArrayList<Double> yValues,
						  ArrayList<Double> standardDeviations)
	{
	 
		if (yValues == null || standardDeviations == null) throw new IllegalArgumentException("Data must not be null");
		if (yValues.size() < 1) throw new IllegalArgumentException("No data for chart");
		if (yValues.size() != standardDeviations.size())
			throw new IllegalArgumentException("Y values and standard deviation list sizes should match");
		
		if (currentColorIdx >= MAX_COLORS)
		{
			throw new IllegalArgumentException("Run out of colors in Scatter Chart addSeries()!");
		}

		List<Double> testNumbers = new LinkedList<>();
	    	    
	    for (int i = 0; i < yValues.size(); i++) 
	    {
	    	testNumbers.add(new Double(i+1));
	    }
	    
	    XYSeries series = chart.addSeries(seriesName, testNumbers, yValues, standardDeviations);
	    series.setMarkerColor(COLORS[currentColorIdx++]);
	    series.setMarker(SeriesMarkers.DIAMOND);
	}
	
	public void swingShow()
	{	    
	    
	    javax.swing.SwingUtilities.invokeLater(new Runnable() {

	    	  @Override
	    	  public void run() {

	    	    // Create and set up the window.
	    	    JFrame frame = new JFrame("Test Stats");
	    	    frame.setLayout(new BorderLayout());
	    	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	    	    // chart
	    	    JPanel chartPanel = new XChartPanel<XYChart>(chart);
	    	    frame.add(chartPanel, BorderLayout.CENTER);

	    	    // label
	    	    JLabel label = new JLabel("Test Error Bars", SwingConstants.CENTER);
	    	    frame.add(label, BorderLayout.SOUTH);

	    	    // Display the window.
	    	    frame.pack();
	    	    frame.setVisible(true);
	    	  }
	    	});
	}
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
bluejazzsharkcommented, Dec 29, 2020

Hi Tim,

Many thanks for letting me know. I’ve just tried it out and the error bars work fine now.

Thanks,

Huw

On Wed, Dec 23, 2020 at 8:42 AM Tim Molter notifications@github.com wrote:

Closed #540 https://github.com/knowm/XChart/issues/540.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/knowm/XChart/issues/540#event-4143790002, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANC6WKR3RDHJT3LZFP7FDCLSWHJUHANCNFSM4UZRQRIQ .

Antes de juzgar a alguien, primero anda una milla en sus zapatos. Así estás a una milla, y tienes sus zapatos.

0reactions
timmoltercommented, Dec 23, 2020

FYI, 3.7.0 released.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add, change, or remove error bars in a chart - Microsoft Support
Error bars in charts you create can help you see margins of error and standard deviations at a glance. They can be shown...
Read more >
Excel Standard Deviations and Error Bars for Better Graphs
Data isn't always perfect, absolute and complete. Convey more information in your graphs by adding Excel standard deviations and error bars. Here's how....
Read more >
Adding error bars - Microsoft Excel 2003
Standard Deviation (s): The error bars are in the number of standarddeviation units that you specify. (Excel calculates the standard deviation of the...
Read more >
How to add error bars in Excel: standard and custom - Ablebits
In this example, I'll show you how to make individual standard deviation error bars. To begin with, enter all the error bar values...
Read more >
X/Y Error Bars - LibreOffice Help
When "standard deviation" is selected, x or y is the mean value of the data series. NegativeErrorValue and PositiveErrorValue are the amounts calculated...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found