Error bars on XYCharts when adding a series with standard deviations
See original GitHub issueHi, 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).
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:
- Created 3 years ago
- Comments:9 (4 by maintainers)
Top GitHub Comments
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:
– Antes de juzgar a alguien, primero anda una milla en sus zapatos. Así estás a una milla, y tienes sus zapatos.
FYI, 3.7.0 released.