def generatePlotPanel(self, plotTitle, listData, showLegend, showTooltip): """ 1) Create a PieDataset 2) Create a PieChart (or Create a PiePlot and put it in a JFreeChart) 3) Put the PieChart in a ChartPanel """ # Get a dictionary of value occurence in the list {value1:count, value2:count...} dataDico = Counter(listData) #print dataDico # value: counts OK # Create a Pie dataset from the dicoData pieDataset = DefaultPieDataset() for key, value in dataDico.items(): #print key, value pieDataset.setValue(key, value) # Create an instance of JFreeChart urls = False chart = ChartFactory.createPieChart(plotTitle, pieDataset, showLegend, showTooltip, urls) # Alternative way #piePlot = PiePlot(pieDataset) #chart = JFreeChart(plotTitle, piePlot) return ChartPanel(chart)
def graph(*data,**kwargs): ''' Creates a simple time series graph. Takes the output from getData (a list of Facts). Alternately, you can pass the same set of parameters that are passed to getData(). Takes an optional elements argument which expresses which elements to show. Also takes an optional showMissing argument which determines whether missing values (-9999.0) should be displayed. Also takes an optional lineWidth argument which lets you set lines heavier or lighter. Example: graph(data,elements=("temp","precip"),showMissing=False). Returns the plot object in case you want to customize the graph in some way. ''' from org.jfree.chart import ChartFactory,ChartFrame from org.jfree.data.time import TimeSeries,TimeSeriesCollection, Minute from java.awt import BasicStroke # Were we passed a dataset or parameters for obtaining a dataset? if len(data) == 3: station,date,element = data graph(getData(station,date,element)) return else: data = data[0] # unwrap from tuple showMissing = kwargs.get('showMissing',True) lineWidth = kwargs.get('lineWidth',2.0) title = kwargs.get('title',"Time series graph") elementsToShow = [e.name for e in findElements(kwargs.get('elements',""))] datasets = {} for fact in sorted(data): if fact.value in missingValues and not showMissing: continue station = str(fact.station.name) element = fact.element.name if elementsToShow and element not in elementsToShow: continue series = station+":"+element date = fact.datetime hour = date.getHour() if fact.subhourlyTime is not None: subhourlyTime = int(fact.subhourlyTime) else: subhourlyTime = 0 if subhourlyTime == 60: # JFreeChart can't handle the 1-60 representation of minute in CRN subhourlyTime = 0 minute = Minute(subhourlyTime,hour,date.getDay(),date.getMonth()+1,date.getYear()) # JFreeChart's representation of time if series not in datasets: datasets[series] = TimeSeries(series) datasets[series].add(minute,float(fact.value)) timeSeriesCollection = TimeSeriesCollection() for dataset in datasets.values(): timeSeriesCollection.addSeries(dataset) chart = ChartFactory.createTimeSeriesChart("", "UTC Date", "Value", timeSeriesCollection, True, True, False) plot = chart.getPlot() r = plot.getRenderer() r.setStroke(BasicStroke(lineWidth)) frame = ChartFrame(title, chart) frame.pack() frame.setVisible(True) return plot
def __init__(self, automations): # Create the frame frame = JFrame("Automation Viewer") frame.setSize(500, 300) frame.setLayout(BorderLayout()) series = AutomationSeries # Finalize the window frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE) frame.setVisible(True) # Create an XY dataset dataset = XYSeriesCollection() for autoname in automations: automation = ModbusPal.getAutomation(autoname) series = AutomationSeries(automation) dataset.addSeries(series) frame.addWindowListener(series) # Create chart chart = ChartFactory.createXYLineChart("Automation Viewer", "Time (seconds)", "Value", dataset, PlotOrientation.VERTICAL, Boolean.TRUE, Boolean.TRUE, Boolean.FALSE) panel = ChartPanel(chart) # Add chart to panel frame.add(panel, BorderLayout.CENTER)
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)
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)
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)
def createChart(self): dataset = XYSeriesCollection() for s in self.allSeries: dataset.addSeries(s) # title is None chart = ChartFactory.createScatterPlot( \ None, xlabel(), ylabel(), \ dataset, PlotOrientation.VERTICAL, \ True, True, False) return chart
def __init__(self): """ generated source for method __init__ """ super(ConfigurableDetailPanel, self).__init__(GridBagLayout()) model = DefaultTableModel() model.addColumn("Step") model.addColumn("My Move") model.addColumn("Time spent") model.addColumn("Out of time?") self.moveTable = JZebraTable(model) self.moveTable.setShowHorizontalLines(True) self.moveTable.setShowVerticalLines(True) sidePanel = JPanel() self.memUsage = TimeSeries("Used Memory") self.memTotal = TimeSeries("Total Memory") self.memUsage.setMaximumItemCount(36000) self.memTotal.setMaximumItemCount(36000) memory = TimeSeriesCollection() memory.addSeries(self.memUsage) memory.addSeries(self.memTotal) memChart = ChartFactory.createTimeSeriesChart(None, None, "Megabytes", memory, True, True, False) memChart.setBackgroundPaint(getBackground()) memChartPanel = ChartPanel(memChart) memChartPanel.setPreferredSize(Dimension(500, 175)) sidePanel.add(memChartPanel) self.counters = HashSet() self.countersCollection = TimeSeriesCollection() counterChart = ChartFactory.createTimeSeriesChart(None, None, None, self.countersCollection, True, True, False) counterChart.getXYPlot().setRangeAxis(LogarithmicAxis("Count per 100ms")) counterChart.getXYPlot().getRangeAxis().setAutoRangeMinimumSize(1.0) counterChart.setBackgroundPaint(getBackground()) counterChartPanel = ChartPanel(counterChart) counterChartPanel.setPreferredSize(Dimension(500, 175)) sidePanel.add(counterChartPanel) self.scoreCountersCollection = TimeSeriesCollection() scoreCounterChart = ChartFactory.createTimeSeriesChart(None, None, "Score", self.scoreCountersCollection, True, True, False) scoreCounterChart.getXYPlot().getRangeAxis().setRange(0, 100) scoreCounterChart.setBackgroundPaint(getBackground()) scoreCounterChartPanel = ChartPanel(scoreCounterChart) scoreCounterChartPanel.setPreferredSize(Dimension(500, 175)) sidePanel.add(scoreCounterChartPanel) self.add(JScrollPane(self.moveTable, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED), GridBagConstraints(0, 0, 1, 2, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, Insets(0, 0, 0, 0), 0, 0)) self.add(sidePanel, GridBagConstraints(1, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, Insets(0, 0, 0, 0), 0, 0)) self.add(JButton(resetButtonMethod()), GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE, Insets(0, 0, 0, 0), 0, 0))
def _createChart(self): self.chart = ChartFactory.createXYLineChart( self.getTitle(), # chart title self.getXAxisLabel(), # x axis label self.getYAxisLabel(), # y axis label self.dataSets[0], # data (primary) PlotOrientation.VERTICAL, True, # include legend False, # tooltips False # urls )
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)
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)
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)
def plot(title, x_label, y_label, *curves): dataset = XYSeriesCollection() for legend, curve in curves: series = XYSeries(legend) for x, y in curve: series.add(x, y) dataset.addSeries(series) chart = ChartFactory.createXYLineChart(title, x_label, y_label, dataset, PlotOrientation.VERTICAL, True, True, False) frame = ChartFrame(title, chart) frame.setVisible(True) frame.setSize(400, 300)
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) chart = ChartFactory.createXYBarChart(None, xlabel, False, ylabel, dataset, PlotOrientation.VERTICAL, True, True, False) return Chart(chart)
def createHistogram(data): ''' Creates a histogram from a list of arbitrary numerical data in range from 0..1''' from org.jfree.data.category import DefaultCategoryDataset from org.jfree.chart import ChartFactory,ChartFrame from org.jfree.chart.plot import PlotOrientation from java.lang import Float numBins=20 datamin = min(data) datamax = max(data) #binsize = 1.01 / numBins bins = {} for d in data: binkey = round(d,1) bin = bins.setdefault(binkey,0) bins[binkey] = bin + 1 # Create dataset from bins dataset = DefaultCategoryDataset() # Ensure that bins exist even if they're empty i = datamin while i <= 1.0: bins.setdefault(round(float(i),1),0) i += .1 #print "Number of bins:",len(bins) for bin in sorted(bins): #print "bin:",bin,type(bin) dataset.addValue(bins[bin],"","%05.2f"%(bin)) # Create chart from dataset chart = ChartFactory.createBarChart( "", # chart title "Bin", # domain axis label "Number of occurrences", # range axis label dataset, # data PlotOrientation.VERTICAL, # orientation True, # include legend True, # tooltips? False # URLs? ) plot = chart.getPlot() plot.getRenderer().setShadowVisible(False) frame = ChartFrame("Histogram", chart); frame.pack(); frame.setVisible(True); return plot
def plot2D(points, Ca, Cb): maxIntensity = 255.0 dataset = XYSeriesCollection() seriesNN = XYSeries(channels[Ca+1]+" -ve "+channels[Cb+1]+" -ve") seriesPP = XYSeries(channels[Ca+1]+" +ve "+channels[Cb+1]+" +ve") seriesNP = XYSeries(channels[Ca+1]+" -ve "+channels[Cb+1]+" +ve") seriesPN = XYSeries(channels[Ca+1]+" +ve "+channels[Cb+1]+" -ve") for p in points: posA = channels[Ca+1] in thresholds and p[Ca]>thresholds[ channels[Ca+1] ] posB = channels[Cb+1] in thresholds and p[Cb]>thresholds[ channels[Cb+1] ] if posA and posB: seriesPP.add(p[Cb], p[Ca]) elif posA: seriesPN.add(p[Cb], p[Ca]) elif posB: seriesNP.add(p[Cb], p[Ca]) else: seriesNN.add(p[Cb], p[Ca]) dataset.addSeries(seriesNN) dataset.addSeries(seriesPN) dataset.addSeries(seriesNP) dataset.addSeries(seriesPP) chart = ChartFactory.createScatterPlot( title+" - "+channels[Cb+1]+" vs "+channels[Ca+1], channels[Cb+1], channels[Ca+1], dataset, PlotOrientation.VERTICAL, False,True,False ) plot = chart.getPlot() plot.getDomainAxis().setRange(Range(0.00, maxIntensity), True, False) plot.getRangeAxis().setRange(Range(0.00, maxIntensity), True, False) renderer = chart.getPlot().getRenderer() renderer.setSeriesPaint(0, Color(64,64,64)) #NN renderer.setSeriesPaint(1, Color(0,255,0)) #PN renderer.setSeriesPaint(2, Color(0,0,255)) #NP renderer.setSeriesPaint(3, Color(0,255,255)) #PP shape = Ellipse2D.Float(-1,-1,3,3) renderer.setSeriesShape(0, shape ) renderer.setSeriesShape(1, shape ) renderer.setSeriesShape(2, shape ) renderer.setSeriesShape(3, shape ) frame = ChartFrame(title+" - "+channels[Cb+1]+" vs "+channels[Ca+1], chart) frame.setSize(800, 800) frame.setLocationRelativeTo(None) frame.setVisible(True)
def _createEmptyChart(self, dataset = None): chart = ChartFactory.createXYLineChart( self.title, # chart title "He", # x axis label "Fst", # y axis label dataset, # data PlotOrientation.VERTICAL, True, # include legend True, # tooltips False # urls ) chart.setBackgroundPaint(Color.white) # get a reference to the plot for further customisation... # change the auto tick unit selection to integer units only... #rangeAxis = plot.getRangeAxis() #rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()) self.confArea = None return chart
def graphArbitraryData(data,title=""): ''' Creates a line graph of arbitrary data (as opposed to the rather specific format used by graph()). Pass in a list of datapoints, each consisting of a 3-part tuple: (x,y,category). If you're only graphing one sort of data (ie you want a graph with one line), you can pass in any constant for category. Example (one category): data = [] for i in range(10): data.append((i,i*i,0)) graphArbitraryData(data) Example (multiple categories): data = [] for i in range(30): data.append((i,i*i,"squared")) data.append((i,i*i*i,"cubed")) graphArbitraryData(data) ''' from org.jfree.data.category import DefaultCategoryDataset from org.jfree.chart import ChartFactory, ChartFrame, ChartPanel from org.jfree.chart.plot import PlotOrientation from org.jfree.data.xy import XYSeriesCollection, XYSeries datasets = {} # dict of all series # First, create the individual series from the data for item in data: seriesname = str(item[2]) if seriesname not in datasets: datasets[seriesname] = XYSeries(seriesname) datasets[seriesname].add(float(item[0]), float(item[1])); # Second, add those series to a collection datasetcollection = XYSeriesCollection() for key in datasets: datasetcollection.addSeries(datasets[key]) chart = ChartFactory.createXYLineChart("","","",datasetcollection,PlotOrientation.VERTICAL,True,True,False) frame = ChartFrame(title, chart); frame.pack(); frame.setVisible(True); panel = ChartPanel(chart) return chart.getPlot()
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)
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)
def renderHistogram(values, n_bins, min_max=None, title="Histogram", color=Color.red, show=True, setThemeFn=setTheme): """ values: a list or array of numeric values. n_bins: the number of bins to use. min_max: defaults to None, a tuple with the minimum and maximum value. title: defaults to "Histogram", must be not None. show: defaults to True, showing the histogram in a new window. setThemeFn: defaults to setTheme, can be None or another function that takes a chart as argument and sets rendering colors etc. Returns a tuple of the JFreeChart instance and the window JFrame, if shown. """ hd = HistogramDataset() hd.setType(HistogramType.RELATIVE_FREQUENCY) print min_max if min_max: hd.addSeries(title, values, n_bins, min_max[0], min_max[1]) else: hd.addSeries(title, values, n_bins) chart = ChartFactory.createHistogram(title, "", "", hd, PlotOrientation.VERTICAL, False, False, False) # Adjust series color chart.getXYPlot().getRendererForDataset(hd).setSeriesPaint(0, color) # if setThemeFn: setThemeFn(chart) frame = None if show: frame = JFrame(title) frame.getContentPane().add(ChartPanel(chart)) frame.pack() frame.setVisible(True) return chart, frame
def generatePlotPanel(self, plotTitle, listData): """ 1) Create a CategoryDataset 2) Create a BarChart (or Create a BarPlot and put it in a JFreeChart) 3) Put the BarChart in a ChartPanel """ # Get a dictionary of value occurence in the list {value1:count, value2:count...} dataDico = Counter(listData) #print dataDico # value: counts OK # Create a Pie dataset from the dicoData dataset = DefaultCategoryDataset() for key, value in dataDico.items(): #print key, value dataset.setValue(value, key, "") # Create an instance of JFreeChart chart = ChartFactory.createBarChart(plotTitle, "Categories", "Count", dataset) # Alternative way #piePlot = PiePlot(pieDataset) #chart = JFreeChart(plotTitle, piePlot) return ChartPanel(chart)
def histogram(title, values): dataset = HistogramDataset() dataset.setType(HistogramType.RELATIVE_FREQUENCY) #NBINS = int(maths.sqrt(len(values))) #NBINS = int( (max(values)-min(values))/binW ) NBINS = 64 dataset.addSeries(title, values, NBINS) chart = ChartFactory.createHistogram(title, "Distance (nm)", "Relative Frequency", dataset, PlotOrientation.VERTICAL, False, True, False) plot = chart.getXYPlot() renderer = plot.getRenderer() renderer.setSeriesPaint(0, Colour.BLUE) painter = StandardXYBarPainter() renderer.setBarPainter(painter) frame = ChartFrame(title, chart) frame.setSize(1200, 800) frame.setLocationRelativeTo(None) frame.setVisible(True)
def _createEmptyChart(self, dataset=None): if self.isTemporal: hText = "Hs" fText = "Fgt" else: hText = "He" fText = "Fst" chart = ChartFactory.createXYLineChart( self.title, # chart title hText, # x axis label fText, # y axis label dataset, # data PlotOrientation.VERTICAL, True, # include legend True, # tooltips False # urls ) chart.setBackgroundPaint(Color.white) # get a reference to the plot for further customisation... # change the auto tick unit selection to integer units only... #rangeAxis = plot.getRangeAxis() #rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()) self.confArea = None return chart
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)
def measure(stack, cells, nuclei): time = [ (t-1)*cal.frameInterval for t in range(T+1) ] cellValues0 = [ 0.0 for t in range(T+1) ] cellValues1 = [ 0.0 for t in range(T+1) ] cellAreas0 = [ 0.0 for t in range(T+1) ] cellAreas1 = [ 0.0 for t in range(T+1) ] nucleusValues0 = [ 0.0 for t in range(T+1) ] nucleusValues1 = [ 0.0 for t in range(T+1) ] nucleusAreas0 = [ 0.0 for t in range(T+1) ] nucleusAreas1 = [ 0.0 for t in range(T+1) ] nonNucleusValues0 = [ 0.0 for t in range(T+1) ] nonNucleusValues1 = [ 0.0 for t in range(T+1) ] for t in range(1,T+1): ip = stack.getProcessor(t) if cells[t] is None: continue #subtract background Z from all intensity Z measurements if cells [t] is None: print("Nocellsfound" + str(t)) bothCells = ShapeRoi(cells[t][0]).or(ShapeRoi(cells[t][1])) backRoi = ShapeRoi(Rectangle(0,0,imp.getWidth(),imp.getHeight())).not( bothCells ) ip.setRoi(backRoi) backMean = ip.getStatistics().mean ip.setRoi( cells[t][0] ) stats0 = ip.getStatistics() cellValues0[t] = stats0.mean - backMean cellAreas0[t] = stats0.area * cal.pixelWidth * cal.pixelHeight nuc0 = None for nuc in nuclei[t]: rect = nuc.getBounds() nx = int(rect.x+(rect.width/2.0)) ny = int(rect.y+(rect.height/2.0)) if cells[t][0].contains(nx,ny): nuc0 = nuc break if nuc0 is not None: ip.setRoi( nuc0 ) nucStats0 = ip.getStatistics() nucleusValues0[t] = nucStats0.mean - backMean nucleusAreas0[t] = nucStats0.area * cal.pixelWidth * cal.pixelHeight nuc0.setPosition(0,0,t) nuc0.setStrokeColor(Color.CYAN) ol.add(nuc0) nonnucRoi0 = ShapeRoi(cells[t][0]).not( ShapeRoi(nuc0) ) ip.setRoi( nonnucRoi0 ) nonNucleusValues0[t] = ip.getStatistics().mean - backMean ip.setRoi( cells[t][1] ) stats1 = ip.getStatistics() cellValues1[t] = stats1.mean - backMean cellAreas1[t] = stats1.area * cal.pixelWidth * cal.pixelHeight nuc1 = None for nuc in nuclei[t]: rect = nuc.getBounds() nx = int(rect.x+(rect.width/2.0)) ny = int(rect.y+(rect.height/2.0)) if cells[t][1].contains(nx,ny): nuc1 = nuc break if nuc1 is not None: ip.setRoi( nuc1 ) nucStats1 = ip.getStatistics() nucleusValues1[t] = nucStats1.mean - backMean nucleusAreas1[t] = nucStats1.area * cal.pixelWidth * cal.pixelHeight nuc1.setPosition(0,0,t) nuc1.setStrokeColor(Color.CYAN) ol.add(nuc1) nonnucRoi1 = ShapeRoi(cells[t][1]).not( ShapeRoi(nuc1) ) ip.setRoi( nonnucRoi1 ) nonNucleusValues1[t] = ip.getStatistics().mean - backMean rt = ResultsTable() rt.showRowNumbers(False) for t in range(1,T+1): rt.setValue("Time ("+cal.getTimeUnit()+")", t-1, IJ.d2s(time[t],1)) areaRatio = cellAreas0[t] / cellAreas1[t] if cellAreas0[t]>0 and cellAreas1[t]>0 else 0.0 rt.setValue("Cell 0:Cell 1 Area Ratio", t-1, areaRatio) nucleusRatio = nucleusValues0[t] / nucleusValues1[t] if nucleusValues0[t]>0 and nucleusValues1[t]>0 else 0.0 rt.setValue("Cell 0:Cell 1 Nucleus Ratio", t-1, nucleusRatio) nonNucleusRatio = nonNucleusValues0[t] / nonNucleusValues1[t] if nonNucleusValues0[t]>0 and nonNucleusValues1[t]>0 else 0.0 rt.setValue("Cell 0:Cell 1 Non-Nucleus Ratio", t-1, nonNucleusRatio) nnnRatio0 = nucleusValues0[t] / nonNucleusValues0[t] if nucleusValues0[t]>0 and nonNucleusValues0[t]>0 else 0.0 rt.setValue("Cell 0 Nucleus:Non-Nucleus Ratio", t-1, nnnRatio0) nnnRatio1 = nucleusValues1[t] / nonNucleusValues1[t] if nucleusValues1[t]>0 and nonNucleusValues1[t]>0 else 0.0 rt.setValue("Cell 1 Nucleus:Non-Nucleus Ratio", t-1, nnnRatio1) rt.setValue("Cell 0 (red) Area ("+cal.getUnit()+u"\u00b2"+")", t-1, cellAreas0[t]) rt.setValue("Cell 0 Nucleus Area ("+cal.getUnit()+u"\u00b2"+")", t-1, nucleusAreas0[t]) rt.setValue("Cell 0 All", t-1, cellValues0[t]) rt.setValue("Cell 0 Nucleus", t-1, nucleusValues0[t]) rt.setValue("Cell 0 Non-Nucleus", t-1, nonNucleusValues0[t]) rt.setValue("Cell 1 (green) Area ("+cal.getUnit()+u"\u00b2"+")", t-1, cellAreas1[t]) rt.setValue("Cell 1 Nucleus Area ("+cal.getUnit()+u"\u00b2"+")", t-1, nucleusAreas1[t]) rt.setValue("Cell 1 All", t-1, cellValues1[t]) rt.setValue("Cell 1 Nucleus", t-1, nucleusValues1[t]) rt.setValue("Cell 1 Non-Nucleus", t-1, nonNucleusValues1[t]) rt.show(imp.getTitle()+"-Results") dataset = DefaultXYDataset() dataset.addSeries( "Cell 0", [time[1:], cellValues0[1:]] ) dataset.addSeries( "Cell 1", [time[1:], cellValues1[1:]] ) dataset.addSeries( "Nucleus 0", [time[1:], nucleusValues0[1:]] ) dataset.addSeries( "Nucleus 1", [time[1:], nucleusValues1[1:]] ) dataset.addSeries( "Non-Nucleus 0", [time[1:], nonNucleusValues0[1:]] ) dataset.addSeries( "Non-Nucleus 1", [time[1:], nonNucleusValues1[1:]] ) chart = ChartFactory.createScatterPlot( imp.getTitle(), "Time ("+cal.getTimeUnit()+")", "Intensity Z", dataset, PlotOrientation.VERTICAL, True,True,False ) plot = chart.getPlot() plot.setBackgroundPaint(Color(64, 128, 255)) plot.setDomainGridlinePaint(Color.BLACK) plot.setRangeGridlinePaint(Color.BLACK) renderer = plot.getRenderer() legend = LegendItemCollection() shapeR = 2.0 nucShape = Ellipse2D.Float(-shapeR,-shapeR,shapeR*2,shapeR*2) nonNucShape = Path2D.Float() nonNucShape.moveTo(-shapeR,-shapeR) nonNucShape.lineTo(shapeR,shapeR) nonNucShape.moveTo(shapeR,-shapeR) nonNucShape.lineTo(-shapeR,shapeR) for s in range(dataset.getSeriesCount()): if s == 0: renderer.setSeriesLinesVisible(s, True) renderer.setSeriesShapesVisible(s, False) renderer.setSeriesStroke(s, BasicStroke(1)) renderer.setSeriesPaint(s, Color.RED) legend.add( LegendItem("Cell 0", Color.RED) ) elif s == 1: renderer.setSeriesLinesVisible(s, True) renderer.setSeriesShapesVisible(s, False) renderer.setSeriesStroke(s, BasicStroke(1)) renderer.setSeriesPaint(s, Color.GREEN) legend.add( LegendItem("Cell 1", Color.GREEN) ) elif s == 2: renderer.setSeriesLinesVisible(s, False) renderer.setSeriesShapesVisible(s, True) renderer.setSeriesShape(s, nucShape) renderer.setSeriesPaint(s, Color.RED) elif s == 3: renderer.setSeriesLinesVisible(s, False) renderer.setSeriesShapesVisible(s, True) renderer.setSeriesShape(s, nucShape) renderer.setSeriesPaint(s, Color.GREEN) elif s == 4: renderer.setSeriesLinesVisible(s, False) renderer.setSeriesShapesVisible(s, True) renderer.setSeriesShape(s, nonNucShape) renderer.setSeriesPaint(s, Color.RED) elif s == 5: renderer.setSeriesLinesVisible(s, False) renderer.setSeriesShapesVisible(s, True) renderer.setSeriesShape(s, nonNucShape) renderer.setSeriesPaint(s, Color.GREEN) plot.setFixedLegendItems(legend) frame = ChartFrame(imp.getTitle()+" Z-Normalised Intensity", chart) frame.pack() frame.setSize( Dimension(800, 800) ) frame.setLocationRelativeTo(None) frame.setVisible(True)
def scatter(data,x=None,y=None,**kwargs): ''' Creates a scatter plot comparing two elements. At minimum, takes a collection of data. The second and third arguments, if they exist, are treated as the two elements to compare. If these arguments do not exist, the first two elements in the list are compared. If an optional regress=True argument is present, superimposes a linear regression for each series and prints some related info (R-value etc). Note that scatter plots can be zoomed with the mouse. Returns the plot object in case you want to customize the graph in some way. Takes an optional showMissing argument which determines whether missing values (-9999.0) should be displayed. Examples: scatter(data), scatter(data,"tmin","tmax",regress=True) ''' from org.jfree.data.xy import XYSeriesCollection,XYSeries from org.jfree.data import UnknownKeyException from org.jfree.chart import ChartFactory,ChartFrame from org.jfree.chart.plot import PlotOrientation,DatasetRenderingOrder from org.jfree.chart.renderer.xy import XYLineAndShapeRenderer from java.awt import Color regress=kwargs.get('regress',False) showMissing=kwargs.get('showMissing',False) # Try to be flexible about element parameters if x is not None: x = findElement(x).name if y is not None: y = findElement(y).name # Create a dataset from the data collection = XYSeriesCollection() for ob in data.groupedByObservation().items(): key,values = ob name = str(key[0]) if x==None: x = values[0].element.name try: xFact = (i for i in values if i.element.name == x).next() except StopIteration: # missing value continue xval = xFact.value if xval in missingValues and not showMissing: continue if y==None: try: y = values[1].element.name except IndexError: raise Exception("Error! Your data request returned only 1 value per observation. " "Must have 2 values to generate a scatter plot.") try: yFact = (i for i in values if i.element.name == y).next() except StopIteration: # missing value continue yval = yFact.value if yval in missingValues and not showMissing: continue try: series = collection.getSeries(name) except UnknownKeyException: collection.addSeries(XYSeries(name)) series = collection.getSeries(name) series.add(float(xval),float(yval)) # Create chart from dataset chart = ChartFactory.createScatterPlot( "", x, y, collection, PlotOrientation.VERTICAL, True, True, False ); plot = chart.getPlot() frame = ChartFrame("Scatter Plot", chart); frame.pack(); frame.setVisible(True); # Superimpose regression if desired if regress: regressioncollection = XYSeriesCollection() for series in collection.getSeries(): regression = _getregression(series) x1 = series.getMinX() y1 = regression.predict(x1) x2 = series.getMaxX() y2 = regression.predict(x2) regressionseries = XYSeries(series.getKey()) regressionseries.add(float(x1),float(y1)) regressionseries.add(float(x2),float(y2)) regressioncollection.addSeries(regressionseries) print series.getKey(),":" print " R: %8.4f" % regression.getR() print " R-squared: %8.4f" % regression.getRSquare() print " Significance: %8.4f" % regression.getSignificance() print plot.setDataset(1,regressioncollection) regressionRenderer = XYLineAndShapeRenderer(True,False) plot.setRenderer(1,regressionRenderer) plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD); colors = [0xec0000,0x58b911,0x6886ea,0xedd612,0xa93bb9,0xffb71b,0xe200df,0x1de2b6,0xdc91db,0x383838,0xb09344,0x4ea958,0xd78c9e,0x64008d,0xb0c95b] mainRenderer = plot.getRenderer(0) for i in range(collection.getSeriesCount()): try: mainRenderer.setSeriesPaint(i,Color(colors[i])) regressionRenderer.setSeriesPaint(i,Color(colors[i])) except IndexError: # Finite # of colors in the color array; beyond that let jfreechart pick break ''' # Jump through some hoops to ensure regressions are same color as scatters for each series. # Initially: doesn't work because series are not indexed the same. And I don't see a way # to get the actual series from the renderer in order to compare names or something. mainRenderer = plot.getRenderer(0) print "Renderer is",type(mainRenderer) index = 0 paint = mainRenderer.lookupSeriesPaint(index) print "Paint is",type(paint) while (paint is not None): print "Setting paint." regressionRenderer.setSeriesPaint(index,paint) index += 1 paint = mainRenderer.getSeriesPaint(index) ''' return plot
for j in range(0, i):#Generate 20 values for each serie d2 = d + Math.random() * (d1 - d) arraylist.add(Double(d2)) return arraylist #Create the default dataset for the BoxAndWhisker def createDataSet(): dataset = DefaultBoxAndWhiskerCategoryDataset() for i in range(0, 3):#Generate 3 series for each category for j in range(0,5):#Generate 5 Categories list = createValueList(0.0, 20.0, 20) dataset.add(list, "Series " + str(i), "Category " + str(j))#Add a list of value for 1 rowKey (Serie), 1 columnKey (Category) return dataset boxandwhiskercategorydataset=createDataSet() jfreechart = ChartFactory.createBoxAndWhiskerChart('Box and Whisker Chart Demo 1', 'Category', 'Value', boxandwhiskercategorydataset, True) categoryplot = jfreechart.getPlot() jfreechart.setBackgroundPaint(Color.white) categoryplot.setBackgroundPaint(Color.lightGray) categoryplot.setDomainGridlinePaint(Color.white) categoryplot.setDomainGridlinesVisible(True) categoryplot.setRangeGridlinePaint(Color.white) numberaxis = categoryplot.getRangeAxis() numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()) #Plot the Chart into an ImagePlus bi = jfreechart.createBufferedImage(600, 400) imp = ImagePlus('Chart Test', bi) imp.show()
from org.apache.batik.dom import GenericDOMImplementation from org.apache.batik.svggen import SVGGraphics2D #Define the dataset dataset = DefaultCategoryDataset() dataset.addValue(25, 'S1', 'Slide XXX')#Value, serie name, X label dataset.addValue(40, 'S1', 'Slide YYY') dataset.addValue(60, 'S1', 'Slide ZZZ') ############## # Plot Chart # ############## #chart = ChartFactory.createLineChart(None,'Slide','% Brown Area',dataset,PlotOrientation.VERTICAL,False,True,False) chart = ChartFactory.createBarChart("% Brown Area per Slide", 'Slide', '% Brown Area', dataset, PlotOrientation.VERTICAL, False,True,False) # set the background color for the chart... chart.setBackgroundPaint(Color.WHITE) plot = chart.getPlot() plot.setBackgroundPaint(Color.WHITE) plot.setRangeGridlinesVisible(False) plot.setAxisOffset(RectangleInsets.ZERO_INSETS) #customise the range axis... rangeAxis = plot.getRangeAxis() rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()) rangeAxis.setAutoRangeIncludesZero(True) #Set the Min Max value of the y axis rangeAxis.setRange(0, 100) #Create a custom BarRenderer
from ij import IJ from org.jfree.chart import ChartFactory, ChartPanel from org.jfree.data.statistics import HistogramDataset, HistogramType from javax.swing import JFrame from java.awt import Color imp = IJ.getImage() pixels = imp.getProcessor().convertToFloat().getPixels() # Data and parameter of the histogram values = list(pixels) n_bins = 256 # number of histogram bins # Construct the histogram from the pixel data hist = HistogramDataset() hist.setType(HistogramType.RELATIVE_FREQUENCY) hist.addSeries("my data", values, n_bins) # Create a JFreeChart histogram chart = ChartFactory.createHistogram("My histogram", "the bins", "counts", hist) # Adjust series color chart.getXYPlot().getRendererForDataset(hist).setSeriesPaint(0, Color.blue) # Show the histogram in an interactive window # where the right-click menu enables saving to PNG or SVG, and adjusting properties frame = JFrame("Histogram window") frame.getContentPane().add(ChartPanel(chart)) frame.pack() frame.setVisible(True)
#--------------------Plot with jfreeChart environment----------- values = xhistovector bins = 20 dataset = HistogramDataset() dataset.setType( HistogramType.FREQUENCY ) #other options: RELATIVE_FREQUENCY, SCALE_AREA_TO_1 dataset.addSeries( "Node count", values, bins) chart = ChartFactory.createHistogram( "Node Count Histogram", "Bins", "Node count", dataset, PlotOrientation.VERTICAL, True, # showLegend True, # toolTips True,) # urls # Save it as a PNG: ChartUtilities.saveChartAsPNG( File("/Users/berthola/Desktop/Histotest/foo.png"), chart, 800, 600) from org.jfree.chart import ChartPanel from javax.swing import JFrame
from java.io import File from java.awt import Dimension values = [ random.randint(0,50) for x in xrange(1,100) ] bins = 20 dataset = HistogramDataset() dataset.setType( HistogramType.RELATIVE_FREQUENCY ) dataset.addSeries( "Random Stuff", values, bins) chart = ChartFactory.createHistogram( "Example JFreeChart histogram", "This is the x axis", "This is the y axis", dataset, PlotOrientation.VERTICAL, True, # showLegend True, # toolTips True,) # urls # Save it as a PNG: ChartUtilities.saveChartAsPNG( File("/tmp/foo.png"), chart, 800, 600) from org.jfree.chart import ChartPanel from javax.swing import JFrame
def histogram(*data,**kwargs): ''' Creates a histogram. Takes the output from getData (a list of Facts), or alternately the same parameters that getData() takes. Takes an optional 'numBins=k' argument, where k specifies the number of bins. Returns the plot object in case you want to customize the graph in some way. Takes an optional 'title' argument. ''' # TODO: offset labels. from org.jfree.data.category import DefaultCategoryDataset from org.jfree.chart import ChartFactory,ChartFrame from org.jfree.chart.plot import PlotOrientation from java.lang import Float # Were we passed a dataset or parameters for obtaining a dataset? if len(data) == 3: station,date,element = data histogram(getData(station,date,element),**kwargs) return else: data = data[0] # unwrap from tuple # Find min and max; decide on number of bins numBins=kwargs.get('numBins',16) datamin,datamax = _getminmax(data) binsize = abs((datamax - datamin) / (Decimal(numBins)*Decimal("0.999"))) # divide by .999; otherwise there's always a final bin with one member, the max if binsize == 0: raise Exception("Cannot create histogram; all values are equal to "+str(datamin)) title = kwargs.get('title',"Histogram") # Create bins based on value. stations = {} for d in data: if d.value in missingValues: continue binkey = round(float(datamin + binsize * int((d.value - datamin) / binsize)),2) name = d.station.getNameString()+", "+d.element.name bin = stations.setdefault(name,{}).setdefault(binkey,0) stations[name][binkey] = bin + 1 # Create dataset from bins dataset = DefaultCategoryDataset() for station in stations: # Ensure that bins exist even if they're empty i = datamin while i < datamax: stations[station].setdefault(round(float(i),2),0) i += binsize #print "Number of bins:",len(stations[station]) for bin in sorted(stations[station]): #print "bin:",bin,type(bin) dataset.addValue(stations[station][bin],station,Float(bin)) # Create chart from dataset chart = ChartFactory.createBarChart( "", # chart title "Bin", # domain axis label "Number of occurrences", # range axis label dataset, # data PlotOrientation.VERTICAL, # orientation True, # include legend True, # tooltips? False # URLs? ) plot = chart.getPlot() plot.getRenderer().setShadowVisible(False) frame = ChartFrame(title, chart); frame.pack(); frame.setVisible(True); return plot
print "Create empty dataset" dataset = DefaultStatisticalCategoryDataset() # dataset.add(Mean, StdDev, "Series", "Condition") print "Add elements to dataset" dataset.add(15.0, 2.4, "Row 1", "Column 1") dataset.add(15.0, 4.4, "Row 1", "Column 2") dataset.add(13.0, 2.1, "Row 1", "Column 3") dataset.add(7.0, 1.3, "Row 1", "Column 4") dataset.add(2.0, 2.4, "Row 2", "Column 1") dataset.add(18.0, 4.4, "Row 2", "Column 2") dataset.add(28.0, 2.1, "Row 2", "Column 3") dataset.add(17.0, 1.3, "Row 2", "Column 4") print "Create LineChart" chart = ChartFactory.createLineChart(None, "Treatment", "Measurement", dataset, PlotOrientation.VERTICAL, False, True, False) # set the background color for the chart... chart.setBackgroundPaint(Color.white) plot = chart.getPlot() plot.setBackgroundPaint(Color.white) plot.setRangeGridlinesVisible(False) plot.setAxisOffset(RectangleInsets.ZERO_INSETS) # make a buffered image, create imageplu and show bi = chart.createBufferedImage(600, 400) imp = ImagePlus("Chart Test", bi) imp.show()
print "Create empty dataset" dataset = DefaultStatisticalCategoryDataset() # dataset.add(Mean, StdDev, "Series", "Condition") print "Add elements to dataset" dataset.add(15.0, 2.4, "Row 1", "Column 1") dataset.add(15.0, 4.4, "Row 1", "Column 2") dataset.add(13.0, 2.1, "Row 1", "Column 3") dataset.add(7.0, 1.3, "Row 1", "Column 4") dataset.add(2.0, 2.4, "Row 2", "Column 1") dataset.add(18.0, 4.4, "Row 2", "Column 2") dataset.add(28.0, 2.1, "Row 2", "Column 3") dataset.add(17.0, 1.3, "Row 2", "Column 4") print "Create LineChart" chart = ChartFactory.createLineChart(None, "Treatment", "Measurement", dataset, PlotOrientation.VERTICAL, False, True, False) # set the background color for the chart... chart.setBackgroundPaint(Color.white); plot = chart.getPlot() plot.setBackgroundPaint(Color.white) plot.setRangeGridlinesVisible(False) plot.setAxisOffset(RectangleInsets.ZERO_INSETS) # make a buffered image, create imageplu and show bi = chart.createBufferedImage(600, 400) imp = ImagePlus("Chart Test", bi) imp.show()
#imgB = WindowManager.getImage("26.93/25.90:test_file") imgA = WindowManager.getImage("12.12:test_file") imgB = WindowManager.getImage("13.01/12.12:test_file") ui = UI.getInstance() rois = ui.getRoiManager().getAllROIs() print "Create empty dataset" dataset = DefaultXYDataset() print "Create ScatterPlot" chart = ChartFactory.createScatterPlot( "Scatter Plot", imgA.getTitle(), imgB.getTitle(), dataset, PlotOrientation.VERTICAL, True, False, False); # random test data # #print "Add series to dataset" #npoints = 1000 #data = [[],[]] #for i in range(npoints): # data[0].append(i) # data[1].append(2*i+random.gauss(0,50)) # # this trick is needed to go from a 2D python array # to a 2D java array of doubles, see: # http://fiji.sc/wiki/index.php/Jython_Scripting#Creating_multi-dimensional_native_java_arrays #twoDimArr = array(data, Class.forName('[D'))