Javafx绘制直方图

package Histogram;
/**
* Copyright (c) 2008, 2012 Oracle and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*/
import java.util.Vector;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;

/**
* A chart that displays rectangular bars with heights indicating data values
* for categories. Used for displaying information when at least one axis has
* discontinuous or discrete data.
*
* @see javafx.scene.chart.BarChart
* @see javafx.scene.chart.Chart
* @see javafx.scene.chart.Axis
* @see javafx.scene.chart.CategoryAxis
* @see javafx.scene.chart.NumberAxis
*
*/
public class V_Optimal extends Application {
private static int[] DataSet={1,3,4,7,2,8,3,6,3,6,8,2,1,6,3,5,3,4,7,2,6,7,2};
private static int BucketNum=3;

//定义变量,保存相应的分段数值
private static int a_sec;
private static int b_sec;
private static int c_sec;
private static int minValue;
private static int maxValue;
//定义变量,保存每段的频数
private static int a_num=0;
private static int b_num=0;
private static int c_num=0;

private void init(Stage primaryStage) {
Group root = new Group();
primaryStage.setScene(new Scene(root));
//x轴
String aname="["+ minValue +","+ (minValue + a_sec) + "]";
String bname="["+ (minValue + a_sec + 1)+ "," + (minValue + a_sec + b_sec) + "]";
String cname="["+ (minValue + a_sec + b_sec + 1) + "," + maxValue + "]";
//System.out.println(aname);
String[] section = {aname,bname,cname};
CategoryAxis xAxis = new CategoryAxis();
xAxis.setCategories(FXCollections.<String>observableArrayList(section));
//y轴
NumberAxis yAxis = new NumberAxis("频数", 0, 10, 1);
@SuppressWarnings({ "unchecked", "rawtypes" })
ObservableList<BarChart.Series> barChartData = FXCollections.observableArrayList(
new BarChart.Series("V_Optimal Histogram", FXCollections.observableArrayList(
new BarChart.Data(section[0], a_num),
new BarChart.Data(section[1], b_num),
new BarChart.Data(section[2], c_num)
))
);
@SuppressWarnings({ "rawtypes", "unchecked" })
BarChart chart = new BarChart(xAxis, yAxis, barChartData,0);
chart.setTitle("V最优化直方图");
root.getChildren().add(chart);

}

@Override public void start(Stage primaryStage) throws Exception {
init(primaryStage);
primaryStage.show();
}
public static void main(String[] args) {
//将数据从小到大排序
DataSet = bubleSort(DataSet);
print_array(bubleSort(DataSet));

// 求取数据区间段
minValue = min(DataSet);
maxValue = max(DataSet);
int section = maxValue - minValue;

// 划分每个桶的区间范围
Vector<Integer> aList = new Vector<Integer>();
Vector<Integer> bList = new Vector<Integer>();
Vector<Integer> cList = new Vector<Integer>();
Vector<Double> VarianceValueList = new Vector<Double>();

if (BucketNum > section + 1)
System.out.println("桶数目过多!");
else if (BucketNum < 1)
System.out.println("桶数目必须为正整数!");
else {
for (int a = 0; a <= section - 2; a++) {
for (int b = 1; b <= section - 1 - a; b++) {
Vector<Integer> First = new Vector<Integer>();
Vector<Integer> Second = new Vector<Integer>();
Vector<Integer> Third = new Vector<Integer>();
// 获取每次循环的3个区间集
for (int i = 0; i < DataSet.length; i++) {
if (DataSet[i] >= minValue
&& DataSet[i] <= minValue + a)
First.add(DataSet[i]);
else if (DataSet[i] > minValue + a
&& DataSet[i] <= minValue + a + b)
Second.add(DataSet[i]);
else
Third.add(DataSet[i]);
}
// 转换成3个定长数组
int FirstSection[] = TransferVectorToInt(First);
int SecondSection[] = TransferVectorToInt(Second);
int ThirdSection[] = TransferVectorToInt(Third);

// 求取此时分组的总带权方差
double valueVariance = Cal_Variance(FirstSection)
+ Cal_Variance(SecondSection)
+ Cal_Variance(ThirdSection);

// 保存此时的总带权方差和各分组a,b,c
VarianceValueList.add(valueVariance);
aList.add(a);
bList.add(b);
cList.add(section - a - b);
}
}
}
System.out.println("循环次数为:" + VarianceValueList.size());
// 寻找总带权方差的最小值,和那个分组的a,b,c
int loc = MinLocation(VarianceValueList);
a_sec = aList.get(loc);
b_sec = bList.get(loc);
c_sec = cList.get(loc);

//求出每个分组的频数
for(int ii=0;ii<DataSet.length;ii++){
if(DataSet[ii]>=minValue&&DataSet[ii]<=minValue+a_sec)
a_num++;
else if(DataSet[ii]>minValue+a_sec&&DataSet[ii]<=minValue + a_sec + b_sec)
b_num++;
else
c_num++;
}

// 结果输出:
print_array(TransferVectorToDouble(VarianceValueList));
System.out.println("最小带权方差为:" + VarianceValueList.get(loc));
System.out.println("最佳方案为第" + loc + "次循环!");
System.out.println("a_sec=" + a_sec + " b_sec=" + b_sec + " c_sec="
+ c_sec);
System.out.println("V优化直方图法最佳方案:" + "[" + minValue + ","
+ (minValue + a_sec) + "]" + " [" + (minValue + a_sec + 1)
+ "," + (minValue + a_sec + b_sec) + "]" + " ["
+ (minValue + a_sec + b_sec + 1) + "," + maxValue + "]");

// 进入绘图
launch(args);

}

/**
* 将一组数值中的最小值的位置找到*/
private static int MinLocation(Vector<Double> list){
int loc=0;
//double min=list.get(loc);
for(int i=1;i<list.size();i++){
if(list.get(i)<list.get(loc))
loc=i;
}
return loc;
}

/**
* 将Vector数组转换成常规定长数组*/
private static int[] TransferVectorToInt(Vector<Integer> list){
int[] array=new int[list.size()];
for(int i=0;i<list.size();i++){
array[i]=(int) list.elementAt(i);
}
return array;
}

private static double[] TransferVectorToDouble(Vector<Double> list){
double[] array=new double[list.size()];
for(int i=0;i<list.size();i++){
array[i]=(double) list.elementAt(i);
}
return array;
}

/**
* 计算一组数据的加权方差*/
@SuppressWarnings("unused")
private static double Cal_Variance(double array[]){
double sum=0;
for(int i=0;i<array.length;i++){
sum+=array[i];
}
double avg=sum/array.length;
double nss=0;//ss表示方差,nss表示带权方差
for(int j=0;j<array.length;j++){
nss+=(avg-array[j])*(avg-array[j]);
}
return nss;
}

private static double Cal_Variance(int array[]){
double sum=0;
for(int i=0;i<array.length;i++){
sum+=array[i];
}
double avg=sum/array.length;
double nss=0;//ss表示方差,nss表示带权方差
for(int j=0;j<array.length;j++){
nss+=(avg-array[j])*(avg-array[j]);
}
return nss;
}

/**
* 冒泡排序,从小到大*/
@SuppressWarnings("unused")
private static double[] bubleSort(double array[]){
int n=array.length;
double temp;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(array[i]>array[j]){
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
return array;
}

private static int[] bubleSort(int array[]){
int n=array.length;
int temp;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(array[i]>array[j]){
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
return array;
}

/**求取最小值*/
private static int min(int[] data){
int size=data.length;
int minValue=data[0];
for(int i=1;i<size;i++){
if(minValue>data[i])
minValue=data[i];
}
return minValue;

}

/**求取最大值*/
private static int max(int[] data){
int size=data.length;
int maxValue=data[0];
for(int i=1;i<size;i++){
if(maxValue<data[i])
maxValue=data[i];
}
return maxValue;

}

/**输出数组*/
private static void print_array(double arr[]){
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
System.out.println();
}

private static void print_array(int arr[]){
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
System.out.println();
}

}

时间: 2024-10-13 18:49:34

Javafx绘制直方图的相关文章

用Python绘制直方图

计算频数: 给定一个序列t: hist = {} for x in t: hist[x] = hist.get(x,0)+1 得到的结果是一个将值映射到其频数的字典.将其除以n即可把频数转换成频率,这称为归一化: n = float(len(t)) pmf = {} for x, freq in hist.items(): pmf[x] = freq/n 绘制直方图: Vals, freqs = hist.Render() rectangles = pyplot.bar(vals, freqs)

在图片上画矩形并高亮显示矩形区域、统计矩形区域中像素情况并绘制直方图

<学习OpenCV>中文版第4章第3题 提纲 题目要求 程序代码 结果图片 题目要求: ①允许用户在图片上选择一个矩形区域,当鼠标放开,高亮显示矩形区域 ②在另一个独立窗口中,使用绘图函数绘制一个图表,分别用蓝.绿和红色表示选中区域中各种颜色的像素在指定数值范围内的数量. 程序代码: 1 #include "stdafx.h" 2 #include <cv.h> 3 #include <highgui.h> 4 using namespace std

OpenCV2马拉松第8圈——绘制直方图

收入囊中 灰度直方图 彩色直方图 葵花宝典 直方图的理论还是非常丰富的,应用也很多,诸如: 直方图均衡化 直方图匹配(meanshift,camshift) 在这里,我先介绍基础,如何绘制图像的直方图. 拿灰度图像来说,直方图就是不同的灰度对应的个数,横轴(x)就是[0,256), 纵轴(y)就是对应的个数 如下图,分别是灰度直方图和彩色直方图 初识API C++: void calcHist(const Mat* images, int nimages, const int* channels

numpy和matplotlib绘制直方图

使用 Matplotlib Matplotlib 中有直方图绘制函数:matplotlib.pyplot.hist()它可以直接统计并绘制直方图.你应该使用函数 calcHist() 或 np.histogram()统计直方图. 1 使用pyplot.hist() 显示灰度图像直方图,代码如下: import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread('image/lufei.jpeg

opencv学习笔记9 直方图均衡化并绘制直方图

进行直方图均衡化并将直方图绘制出来,主要需要如下几个函数: 1.CVAPI(void) cvEqualizeHist( const CvArr* src, CvArr* dst ); 这个函数用起来十分简单,只需要传入源图像以及已初始化的目标图像即可. 第一个参数:const CvArr* src:待处理的源图像: 第二个参数:CvArr* dst:目标图像: 在cvEqualizeHist()中,原始图像及目标图像必须是单通道,大小相同的8位图像.对于彩色图像,必须先利用cvSplite()将

绘制直方图

# coding = utf - 8 # 直方图用于那些没有统计过的数据 from matplotlib import pyplot as plt a = [131,98,125,131,124,139,131,117,128,108,135,138,131,102, 97] # 计算组数 d = 3 # 组距 num_bins = (max(a) - min(a)) // d print(max(a), min(a), max(a) - min(a)) print(num_bins) # 设置

python pyqt绘制直方图

# -*- coding: utf-8 -*- """ In this example we draw two different kinds of histogram. """ from qtpy import QtWidgets, QtGui, QtCore from qtpy.QtWidgets import QApplication, QWidget import datetime as dt from vnpy.trader impor

Python开发【模块】:Pygal 绘制直方图

Pygal Pygal可用来生成可缩放的矢量图形文件,对于需要在尺寸不同的屏幕上显示的图表,这很有用,可以自动缩放,自适应观看者的屏幕 1.Pygal模块安装 ① Windows系统 # Pygal模块安装 $ pip3 install pygal ② Linux和OS X系统 # Pygal模块安装 $ pip install --user pygal==1.7 2.Pygal画廊-直方图 模拟掷骰子,分析最后的结果,生成图形 创建die.py筛子类文件: from random import

4.matplotlib绘制直方图

? ? # coding=utf-8 from matplotlib import pyplot as plt from matplotlib import font_manager a=[131, 98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115, 9