Skip to content

Commit

Permalink
Suggested fix for allowing flexible labels
Browse files Browse the repository at this point in the history
  • Loading branch information
Xyclade committed Feb 11, 2015
1 parent c80a84b commit 5ffaaa0
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions SmilePlot/src/smile/plot/ScatterPlot.java
Expand Up @@ -7,6 +7,8 @@

import java.awt.Color;
import java.util.Arrays;
import java.util.HashMap;

import smile.math.Math;

/**
Expand Down Expand Up @@ -34,6 +36,7 @@ public class ScatterPlot extends Plot {
*/
private Color[] palette;

private HashMap<Integer,Integer> valueLookupTable;
/**
* The legend of points.
*/
Expand Down Expand Up @@ -128,19 +131,26 @@ public ScatterPlot(double[][] data, int[] y, char[] legends, Color[] palette) {
// class label set.
int[] id = Math.unique(y);
Arrays.sort(id);


valueLookupTable = new HashMap<Integer, Integer>(id.length);

for (int i =0; i < id.length; i++) {
valueLookupTable.put(id[i], i);
}

for (int i = 0; i < id.length; i++) {
if (id[i] < 0) {
throw new IllegalArgumentException("Negative class label: " + id[i]);
}
}

int k = Math.max(id);
if (legends != null && k >= legends.length) {
int k = id.length;

if (legends != null && k > legends.length) {
throw new IllegalArgumentException("Too few legends.");
}

if (palette != null && k >= palette.length) {
if (palette != null && k > palette.length) {
throw new IllegalArgumentException("Too few colors.");
}

Expand All @@ -167,11 +177,11 @@ public void paint(Graphics g) {
} else {
for (int i = 0; i < data.length; i++) {
if (palette != null) {
g.setColor(palette[y[i]]);
g.setColor(palette[valueLookupTable.get(y[i])]);
}

if (legends != null) {
g.drawPoint(legends[y[i]], data[i]);
g.drawPoint(legends[valueLookupTable.get(y[i])], data[i]);
} else {
g.drawPoint(legend, data[i]);
}
Expand Down

0 comments on commit 5ffaaa0

Please sign in to comment.