Java Code Examples for tech.tablesaw.api.Table#nCol()

The following examples show how to use tech.tablesaw.api.Table#nCol() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScatterLegendExample.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
  Table tornadoes = Table.read().csv("../data/tornadoes_1950-2014.csv");
  tornadoes = tornadoes.where(tornadoes.nCol("Start lat").isGreaterThan(20));
  NumericColumn<?> x = tornadoes.nCol("Start lon");
  NumericColumn<?> y = tornadoes.nCol("Start lat");

  // show a legend even though there's only one trace, by setting showLegend explicitly to true
  Layout layout =
      Layout.builder()
          .title("tornado start points")
          .height(600)
          .width(800)
          .showLegend(true)
          .build();
  Trace trace =
      ScatterTrace.builder(x, y).marker(Marker.builder().size(1).build()).name("lat/lon").build();
  Plot.show(new Figure(layout, trace));
}
 
Example 2
Source File: ScatterplotWithSpecificAxisRange.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
  Table tornadoes = Table.read().csv("../data/tornadoes_1950-2014.csv");
  tornadoes = tornadoes.where(tornadoes.nCol("Start lat").isGreaterThan(20));
  NumericColumn<?> x = tornadoes.nCol("Start lon");
  NumericColumn<?> y = tornadoes.nCol("Start lat");
  Layout layout =
      Layout.builder()
          .title("tornado start points")
          .height(600)
          .width(800)
          .yAxis(Axis.builder().range(20, 60).build())
          .build();
  Trace trace =
      ScatterTrace.builder(x, y).marker(Marker.builder().size(1).build()).name("lat/lon").build();
  Plot.show(new Figure(layout, trace));
}
 
Example 3
Source File: QuantilePlot.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a figure containing a Quantile Plot describing the distribution of values in the column
 * of interest
 *
 * @param title A title for the plot
 * @param table The table containing the column of interest
 * @param columnName The name of the numeric column containing the data to plot
 * @return A quantile plot
 */
public static Figure create(String title, Table table, String columnName) {

  NumericColumn<?> xCol = table.nCol(columnName);

  double[] x = new double[xCol.size()];

  for (int i = 0; i < x.length; i++) {
    x[i] = i / (float) x.length;
  }

  NumericColumn<?> copy = xCol.copy();
  copy.sortAscending();

  ScatterTrace trace = ScatterTrace.builder(x, copy.asDoubleArray()).build();
  Layout layout = Layout.builder().title(title).build();
  return new Figure(layout, trace);
}
 
Example 4
Source File: ScatterLegendExample.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
  Table tornadoes = Table.read().csv("../data/tornadoes_1950-2014.csv");
  tornadoes = tornadoes.where(tornadoes.nCol("Start lat").isGreaterThan(20));
  NumericColumn<?> x = tornadoes.nCol("Start lon");
  NumericColumn<?> y = tornadoes.nCol("Start lat");

  // show a legend even though there's only one trace, by setting showLegend explicitly to true
  Layout layout =
      Layout.builder()
          .title("tornado start points")
          .height(600)
          .width(800)
          .showLegend(true)
          .build();
  Trace trace =
      ScatterTrace.builder(x, y).marker(Marker.builder().size(1).build()).name("lat/lon").build();
  Plot.show(new Figure(layout, trace));
}
 
Example 5
Source File: LinePlotExampleWithSmoothing.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
  Table robberies = Table.read().csv("../data/boston-robberies.csv");
  NumericColumn<?> x = robberies.nCol("Record");
  NumericColumn<?> y = robberies.nCol("Robberies");

  Layout layout =
      Layout.builder().title("Monthly Boston Armed Robberies Jan. 1966 - Oct. 1975").build();

  ScatterTrace trace =
      ScatterTrace.builder(x, y)
          .mode(ScatterTrace.Mode.LINE)
          .line(Line.builder().shape(Line.Shape.SPLINE).smoothing(1.2).build())
          .build();

  Plot.show(new Figure(layout, trace));
}
 
Example 6
Source File: LinePlotExample.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  Table robberies = Table.read().csv("../data/boston-robberies.csv");
  NumericColumn<?> x = robberies.nCol("Record");
  NumericColumn<?> y = robberies.nCol("Robberies");

  Layout layout =
      Layout.builder().title("Monthly Boston Armed Robberies Jan. 1966 - Oct. 1975").build();
  ScatterTrace trace = ScatterTrace.builder(x, y).mode(ScatterTrace.Mode.LINE).build();
  Plot.show(new Figure(layout, trace));
}
 
Example 7
Source File: ScatterplotWithTwoYAxes.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  Table baseball = Table.read().csv("../data/baseball.csv");
  NumericColumn<?> x = baseball.nCol("BA");
  NumericColumn<?> y = baseball.nCol("W");
  NumericColumn<?> y2 = baseball.nCol("SLG");

  Layout layout =
      Layout.builder()
          .title("Wins vs BA and SLG")
          .xAxis(Axis.builder().title("Batting Average").build())
          .yAxis(Axis.builder().title("Wins").build())
          .yAxis2(
              Axis.builder()
                  .title("SLG")
                  .side(Axis.Side.right)
                  .overlaying(ScatterTrace.YAxis.Y)
                  .build())
          .build();

  Trace trace =
      ScatterTrace.builder(x, y)
          .name("Batting avg.")
          .marker(Marker.builder().opacity(.7).color("#01FF70").build())
          .build();

  Trace trace2 =
      ScatterTrace.builder(x, y2)
          .yAxis(ScatterTrace.YAxis.Y2)
          .name("Slugging pct.")
          .marker(Marker.builder().opacity(.7).color("rgb(17, 157, 255)").build())
          .build();

  Figure figure = new Figure(layout, trace2, trace);
  Plot.show(figure);
}
 
Example 8
Source File: BubbleExample.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
  Table marketShare = Table.read().csv("../data/market_share.csv");
  Table sub = marketShare.where(Selection.withRange(0, 4));
  NumericColumn<?> x = sub.nCol("Products");
  NumericColumn<?> y = sub.nCol("Sales");
  NumericColumn<?> data = sub.nCol("Market_Share");

  Layout layout = Layout.builder().title("Market Share").build();
  Marker marker = Marker.builder().size(data).sizeMode(Marker.SizeMode.AREA).build();
  ScatterTrace trace = ScatterTrace.builder(x, y).marker(marker).build();

  Plot.show(new Figure(layout, trace));
}
 
Example 9
Source File: LinePlotExample.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  Table robberies = Table.read().csv("../data/boston-robberies.csv");
  NumericColumn<?> x = robberies.nCol("Record");
  NumericColumn<?> y = robberies.nCol("Robberies");

  Layout layout =
      Layout.builder().title("Monthly Boston Armed Robberies Jan. 1966 - Oct. 1975").build();
  ScatterTrace trace = ScatterTrace.builder(x, y).mode(ScatterTrace.Mode.LINE).build();
  Plot.show(new Figure(layout, trace));
}
 
Example 10
Source File: ScatterplotWithTwoYAxes.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  Table baseball = Table.read().csv("../data/baseball.csv");
  NumericColumn<?> x = baseball.nCol("BA");
  NumericColumn<?> y = baseball.nCol("W");
  NumericColumn<?> y2 = baseball.nCol("SLG");

  Layout layout =
      Layout.builder()
          .title("Wins vs BA and SLG")
          .xAxis(Axis.builder().title("Batting Average").build())
          .yAxis(Axis.builder().title("Wins").build())
          .yAxis2(
              Axis.builder()
                  .title("SLG")
                  .side(Axis.Side.right)
                  .overlaying(ScatterTrace.YAxis.Y)
                  .build())
          .build();

  Trace trace =
      ScatterTrace.builder(x, y)
          .name("Batting avg.")
          .marker(Marker.builder().opacity(.7).color("#01FF70").build())
          .build();

  Trace trace2 =
      ScatterTrace.builder(x, y2)
          .yAxis(ScatterTrace.YAxis.Y2)
          .name("Slugging pct.")
          .marker(Marker.builder().opacity(.7).color("rgb(17, 157, 255)").build())
          .build();

  Figure figure = new Figure(layout, trace2, trace);
  Plot.show(figure);
}
 
Example 11
Source File: BubbleExample.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
  Table marketShare = Table.read().csv("../data/market_share.csv");
  Table sub = marketShare.where(Selection.withRange(0, 4));
  NumericColumn<?> x = sub.nCol("Products");
  NumericColumn<?> y = sub.nCol("Sales");
  NumericColumn<?> data = sub.nCol("Market_Share");

  Layout layout = Layout.builder().title("Market Share").build();
  Marker marker = Marker.builder().size(data).sizeMode(Marker.SizeMode.AREA).build();
  ScatterTrace trace = ScatterTrace.builder(x, y).marker(marker).build();

  Plot.show(new Figure(layout, trace));
}
 
Example 12
Source File: LineOptionsExample.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
private LineOptionsExample() throws Exception {
  Table robberies = Table.read().csv("../data/boston-robberies.csv");
  this.x = robberies.nCol("Record");
  this.y = robberies.nCol("Robberies");
}
 
Example 13
Source File: DotPlotExample.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

    Table bush = Table.read().csv("../data/bush.csv");

    NumericColumn<?> x = bush.nCol("approval");
    CategoricalColumn<?> y = bush.stringColumn("who");

    Layout layout = Layout.builder().title("Approval ratings by agency").build();

    ScatterTrace trace = ScatterTrace.builder(x, y).mode(ScatterTrace.Mode.MARKERS).build();
    Plot.show(new Figure(layout, trace));

    // A more complex example involving two traces
    IntColumn year = bush.dateColumn("date").year();
    year.setName("year");
    bush.addColumns(year);
    bush.dropWhere(bush.intColumn("year").isIn((Number) 2001, (Number) 2002));
    Table summary = bush.summarize("approval", AggregateFunctions.mean).by("who", "year");

    Layout layout2 =
        Layout.builder()
            .title("Mean approval ratings by agency and year for 2001 and 2002")
            .build();

    Table year1 = summary.where(summary.intColumn("year").isEqualTo(2001));
    Table year2 = summary.where(summary.intColumn("year").isEqualTo(2002));
    ScatterTrace trace2 =
        ScatterTrace.builder(year1.nCol("Mean [approval]"), year1.stringColumn("who"))
            .name("2001")
            .mode(ScatterTrace.Mode.MARKERS)
            .marker(Marker.builder().symbol(Symbol.DIAMOND).color("red").size(10).build())
            .build();

    ScatterTrace trace3 =
        ScatterTrace.builder(year2.nCol("Mean [approval]"), year2.stringColumn("who"))
            .name("2002")
            .mode(ScatterTrace.Mode.MARKERS)
            .marker(Marker.builder().symbol(Symbol.STAR).size(10).color("blue").build())
            .build();

    Plot.show(new Figure(layout2, trace2, trace3));
  }
 
Example 14
Source File: MultiPlotExample.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

    /*
    Preliminaries:
    1. Get a table
    2. Split the data into two tables, one for each league
    3. Get the columns we're going to use in the plot as x1, y1, x2, y2
    4. Create a layout for each plot
    5. Create the traces for each plot
    6. Build a Figure for each plot from the layout and trace
    7. Create an HTML page as a string
    8. Write the string to a file
    9. Open the default desktop Web browser on the file so you can see it
    */

    // 1. Get a table
    Table baseball = Table.read().csv("../data/baseball.csv");

    // 2. Split the data into two tables, one for each league
    List<Table> leagueTables = baseball.splitOn("league").asTableList();
    Table league1 = leagueTables.get(0);
    Table league2 = leagueTables.get(1);

    // 3. Get the columns we're going to use in the plot as x1, y1, x2, y2
    NumericColumn<?> x1 = league1.nCol("BA");
    NumericColumn<?> y1 = league1.nCol("W");

    NumericColumn<?> x2 = league2.nCol("BA");
    NumericColumn<?> y2 = league2.nCol("W");

    // 4. Create a layout for each plot
    Layout layout1 =
        Layout.builder()
            .title("American League Wins vs BA")
            .xAxis(Axis.builder().title("Batting Average").build())
            .yAxis(Axis.builder().title("Wins").build())
            .build();

    Layout layout2 =
        Layout.builder()
            .title("National League Wins vs BA")
            .xAxis(Axis.builder().title("Batting Average").build())
            .yAxis(Axis.builder().title("Wins").build())
            .build();

    // 5. Create the traces for each plot
    Trace trace1 = ScatterTrace.builder(x1, y1).build();
    Trace trace2 = ScatterTrace.builder(x2, y2).build();

    // 6. Build a Figure for each plot from the layout and trace
    Figure figure1 = new Figure(layout1, trace1);
    Figure figure2 = new Figure(layout2, trace2);

    // 7. Create an HTML page as a string
    String divName1 = "plot1";
    String divName2 = "plot2";
    String page = makePage(figure1, figure2, divName1, divName2);

    // 8. Write the string to a file
    // uncomment to try (see imports also)

    /*
        File outputFile = Paths.get("multiplot.html").toFile();
        try {
          try (FileWriter fileWriter = new FileWriter(outputFile)) {
            fileWriter.write(page);
          }
        } catch (IOException e) {
          e.printStackTrace();
        }
    */

    // 9. Open the default desktop Web browser on the file so you can see it
    // uncomment to try

    // new Browser().browse(outputFile);
  }
 
Example 15
Source File: LineOptionsExample.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
private LineOptionsExample() throws Exception {
  Table robberies = Table.read().csv("../data/boston-robberies.csv");
  this.x = robberies.nCol("Record");
  this.y = robberies.nCol("Robberies");
}
 
Example 16
Source File: DotPlotExample.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

    Table bush = Table.read().csv("../data/bush.csv");

    NumericColumn<?> x = bush.nCol("approval");
    CategoricalColumn<?> y = bush.stringColumn("who");

    Layout layout = Layout.builder().title("Approval ratings by agency").build();

    ScatterTrace trace = ScatterTrace.builder(x, y).mode(ScatterTrace.Mode.MARKERS).build();
    Plot.show(new Figure(layout, trace));

    // A more complex example involving two traces
    IntColumn year = bush.dateColumn("date").year();
    year.setName("year");
    bush.addColumns(year);
    bush.dropWhere(bush.intColumn("year").isIn((Number) 2001, (Number) 2002));
    Table summary = bush.summarize("approval", AggregateFunctions.mean).by("who", "year");

    Layout layout2 =
        Layout.builder()
            .title("Mean approval ratings by agency and year for 2001 and 2002")
            .build();

    Table year1 = summary.where(summary.intColumn("year").isEqualTo(2001));
    Table year2 = summary.where(summary.intColumn("year").isEqualTo(2002));
    ScatterTrace trace2 =
        ScatterTrace.builder(year1.nCol("Mean [approval]"), year1.stringColumn("who"))
            .name("2001")
            .mode(ScatterTrace.Mode.MARKERS)
            .marker(Marker.builder().symbol(Symbol.DIAMOND).color("red").size(10).build())
            .build();

    ScatterTrace trace3 =
        ScatterTrace.builder(year2.nCol("Mean [approval]"), year2.stringColumn("who"))
            .name("2002")
            .mode(ScatterTrace.Mode.MARKERS)
            .marker(Marker.builder().symbol(Symbol.STAR).size(10).color("blue").build())
            .build();

    Plot.show(new Figure(layout2, trace2, trace3));
  }
 
Example 17
Source File: QQPlot.java    From tablesaw with Apache License 2.0 3 votes vote down vote up
/**
 * Returns a figure containing a QQ Plot describing the differences between the distribution of
 * values in the columns of interest
 *
 * @param title A title for the plot
 * @param table The table containing the columns of interest
 * @param columnName1 The name of the first numeric column containing the data to plot
 * @param columnName2 The name of the second numeric column containing the data to plot
 * @return A quantile plot
 */
public static Figure create(String title, Table table, String columnName1, String columnName2) {

  NumericColumn<?> xCol = table.nCol(columnName1);
  NumericColumn<?> yCol = table.nCol(columnName2);

  return create(title, xCol.name(), xCol.asDoubleArray(), yCol.name(), yCol.asDoubleArray());
}
 
Example 18
Source File: QQPlot.java    From tablesaw with Apache License 2.0 3 votes vote down vote up
/**
 * Returns a figure containing a QQ Plot describing the differences between the distribution of
 * values in the columns of interest
 *
 * @param title A title for the plot
 * @param table The table containing the columns of interest
 * @param columnName1 The name of the first numeric column containing the data to plot
 * @param columnName2 The name of the second numeric column containing the data to plot
 * @return A quantile plot
 */
public static Figure create(String title, Table table, String columnName1, String columnName2) {

  NumericColumn<?> xCol = table.nCol(columnName1);
  NumericColumn<?> yCol = table.nCol(columnName2);

  return create(title, xCol.name(), xCol.asDoubleArray(), yCol.name(), yCol.asDoubleArray());
}
 
Example 19
Source File: TukeyMeanDifferencePlot.java    From tablesaw with Apache License 2.0 3 votes vote down vote up
/**
 * Returns a figure containing a Tukey Mean-Difference Plot describing the differences between the
 * data in two columns of interest
 *
 * @param title A title for the plot
 * @param measure The measure being compared on the plot (e.g "inches" or "height in inches"
 * @param table The table containing the columns of interest
 * @param columnName1 The name of the first numeric column containing the data to plot
 * @param columnName2 The name of the second numeric column containing the data to plot
 * @return A quantile plot
 */
public static Figure create(
    String title, String measure, Table table, String columnName1, String columnName2) {

  NumericColumn<?> xCol = table.nCol(columnName1);
  NumericColumn<?> yCol = table.nCol(columnName2);

  return create(title, measure, xCol.asDoubleArray(), yCol.asDoubleArray());
}
 
Example 20
Source File: TukeyMeanDifferencePlot.java    From tablesaw with Apache License 2.0 3 votes vote down vote up
/**
 * Returns a figure containing a Tukey Mean-Difference Plot describing the differences between the
 * data in two columns of interest
 *
 * @param title A title for the plot
 * @param measure The measure being compared on the plot (e.g "inches" or "height in inches"
 * @param table The table containing the columns of interest
 * @param columnName1 The name of the first numeric column containing the data to plot
 * @param columnName2 The name of the second numeric column containing the data to plot
 * @return A quantile plot
 */
public static Figure create(
    String title, String measure, Table table, String columnName1, String columnName2) {

  NumericColumn<?> xCol = table.nCol(columnName1);
  NumericColumn<?> yCol = table.nCol(columnName2);

  return create(title, measure, xCol.asDoubleArray(), yCol.asDoubleArray());
}