Exemple #1
0
def scatterplot(data, name="", xlabel="", ylabel="", size= 3):
  """
  Creates a scatter plot from x,y data.

  *data* is a list of (x,y) tuples.
  """
    
  xAxis = NumberAxis(xlabel)   
  xAxis.setAutoRangeIncludesZero(False)   
  yAxis = NumberAxis(ylabel)   
  yAxis.setAutoRangeIncludesZero(False)   
   
  series = XYSeries("Values");     
  for (i,j) in data:         
    series.add(i, j)

  dataset = XYSeriesCollection()
  dataset.addSeries(series);
  chart = ChartFactory.createScatterPlot(name, xlabel, ylabel, dataset, 
    PlotOrientation.VERTICAL, True, True, False)    
  plot = chart.getPlot()
  plot.getRenderer().setSeriesShape(0, 
    ShapeUtilities.createRegularCross(size,size));                  
    
  return Chart(chart)
    
Exemple #2
0
def curve(data, name="", smooth=True, trid=True):
    """
  Creates a curve based on a list of (x,y) tuples.
    
  Setting *smooth* to ``True`` results in a spline renderer renderer is used.

  Setting *trid* to ``True`` results in a 3D plot. In this case the ``smooth``
  argument is ignored.
  """

    dataset = XYSeriesCollection()
    xy = XYSeries(name)
    for d in data:
        xy.add(d[0], d[1])
    dataset.addSeries(xy)
    chart = ChartFactory.createXYLineChart(None, None, None, dataset,
                                           PlotOrientation.VERTICAL, True,
                                           True, False)

    if smooth:
        chart.getXYPlot().setRenderer(XYSplineRenderer())
    if trid:
        chart.getXYPlot().setRenderer(XYLine3DRenderer())

    return Chart(chart)
Exemple #3
0
def regression(data, regtype=0):
    xAxis = NumberAxis("x")
    xAxis.setAutoRangeIncludesZero(False)
    yAxis = NumberAxis("y")
    yAxis.setAutoRangeIncludesZero(False)

    series = XYSeries("values")
    xmax = xmin = None
    for (x, y) in data:
        series.add(x, y)
        if xmax is None:
            xmax = xmin = x
        else:
            xmax = max(xmax, x)
            xmin = min(xmin, x)

    dataset = XYSeriesCollection()
    dataset.addSeries(series)

    renderer1 = XYDotRenderer()
    plot = XYPlot(dataset, xAxis, yAxis, renderer1)

    if regtype == 1:
        coefficients = Regression.getPowerRegression(dataset, 0)
        curve = PowerFunction2D(coefficients[0], coefficients[1])
        regdesc = "Power Regression"
    else:
        coefficients = Regression.getOLSRegression(dataset, 0)
        curve = LineFunction2D(coefficients[0], coefficients[1])
        regdesc = "Linear Regression"

    regressionData = DatasetUtilities.sampleFunction2D(
        curve, xmin, xmax, 100, "Fitted Regression Line")

    plot.setDataset(1, regressionData)
    renderer2 = XYLineAndShapeRenderer(True, False)
    renderer2.setSeriesPaint(0, Color.blue)
    plot.setRenderer(1, renderer2)

    jfchart = JFreeChart(regdesc, JFreeChart.DEFAULT_TITLE_FONT, plot, True)

    chart = Chart(jfchart)
    chart.coeffs = coefficients

    return chart
Exemple #4
0
def regression(data, regtype=0):
  xAxis = NumberAxis("x")   
  xAxis.setAutoRangeIncludesZero(False)   
  yAxis = NumberAxis("y")   
  yAxis.setAutoRangeIncludesZero(False)   
   
  series = XYSeries("values"); 
  xmax = xmin = None       
  for (x,y) in data:
    series.add(x, y);
    if xmax is None:
      xmax = xmin = x
    else:
      xmax = max(xmax, x)
      xmin = min(xmin, x)
            
  dataset = XYSeriesCollection()
  dataset.addSeries(series);

  renderer1 = XYDotRenderer()
  plot = XYPlot(dataset, xAxis, yAxis, renderer1)   
      
  if regtype == 1:
    coefficients = Regression.getPowerRegression(dataset, 0)
    curve = PowerFunction2D(coefficients[0], coefficients[1])
    regdesc = "Power Regression"    
  else:
    coefficients = Regression.getOLSRegression(dataset, 0)   
    curve = LineFunction2D(coefficients[0], coefficients[1])
    regdesc = "Linear Regression"   
    
  regressionData = DatasetUtilities.sampleFunction2D(curve, xmin, xmax, 100, 
    "Fitted Regression Line")   
           
  plot.setDataset(1, regressionData)   
  renderer2 = XYLineAndShapeRenderer(True, False)   
  renderer2.setSeriesPaint(0, Color.blue)   
  plot.setRenderer(1, renderer2)
           
  jfchart = JFreeChart(regdesc, JFreeChart.DEFAULT_TITLE_FONT, plot, True);
    
  chart = Chart(jfchart)          
  chart.coeffs = coefficients
    
  return chart
Exemple #5
0
def box(data):
  """
  Creates a box and whiskers plot.

  *data* is a ``dict`` whose keys are category names and values are list of
  numeric values.
  """
  dataset  = DefaultBoxAndWhiskerCategoryDataset()
  for name, values in data.iteritems():    
    dataset.add(values, "", name);
  chart = ChartFactory.createBoxAndWhiskerChart("", "", "", dataset, True)
  return Chart(chart)
Exemple #6
0
def pie(data, name='', trid=False):
  """
  Creates a pie chart.

  *data* is a ``dict`` whose keys are category names and values are numeric 
  values.
  
  Setting *trid* to ``True`` results in a 3D char.
  """        
  dataset = DefaultPieDataset();    
  for k,v in data.iteritems():        
    dataset.setValue(k, v) 
  if trid:
    chart = ChartFactory.createPieChart3D(name, dataset, True, True, False)
  else:
    chart = ChartFactory.createPieChart(name, dataset, True, True, False)
  return Chart(chart)
Exemple #7
0
def xy(data, name='', xlabel='', ylabel=''):
    """
  Creates a xy bar chart.         

  *data* is a list of (x,y) tuples
  """
    series = XYSeries(name)
    for x, y in data:
        series.add(x, y)

    dataset = XYSeriesCollection(series)
    if len(data) > 1:
        # hack to set interval width
        x0, x1 = data[0][0], data[1][0]
        dataset.setIntervalWidth(x1 - x0)

    chart = ChartFactory.createXYBarChart(None, xlabel, False, ylabel, dataset,
                                          PlotOrientation.VERTICAL, True, True,
                                          False)
    return Chart(chart)
Exemple #8
0
def category(data, name='', xlabel='', ylabel='', stacked=False, trid=False):
    """
  Creates a category bar chart.
    
  *data* is a ``dict`` whose keys are category names and whose values are 
  numerical (the height of the bar). 

  To plot multiple series *data* is specified as a ``dict`` whose keys are 
  series names and values are a ``dict`` of category names to numerical values.

  Setting *stacked* to ``True`` results in a stacked bar char. Setting *trid*
  to ``True`` results in a 3D bar chart.
  """
    dataset = DefaultCategoryDataset()
    for k, v in data.iteritems():
        if isinstance(v, dict):
            for k2, v2 in v.iteritems():
                dataset.addValue(v2, k2, k)
        else:
            dataset.addValue(v, "", k)

    if trid:
        if stacked:
            chart = ChartFactory.createStackedBarChart3D(
                name, xlabel, ylabel, dataset, PlotOrientation.VERTICAL, True,
                True, True)
        else:
            chart = ChartFactory.createBarChart3D(name, xlabel, ylabel,
                                                  dataset,
                                                  PlotOrientation.VERTICAL,
                                                  True, True, True)
    else:
        if stacked:
            chart = ChartFactory.createStackedBarChart(
                name, xlabel, ylabel, dataset, PlotOrientation.VERTICAL, True,
                True, True)
        else:
            chart = ChartFactory.createBarChart(name, xlabel, ylabel, dataset,
                                                PlotOrientation.VERTICAL, True,
                                                True, True)
    return Chart(chart)