jfreechartにデータの2つのベクトルリストをプロットする方法

GeekPumpkin

こんにちは私はアルゴリズムと関数を実現しようとしています。分子番号の1つが0になると、whileループ全体が終了し、JFreeChartを使用した次のチャート描画ステップに自動的に進みます。現在、「return」を使用した後、Eclipseはエラーを表示せず、mainメソッドを完全に処理します。しかし、チャートが表示されていません。何か手順がうまくいかないかどうか疑問に思います。異なる時間における異なる種の分子数を示したいと思います。それぞれの種は新しい行を表します。だから私は新しいチャートを作成します

static JPanel chartPanel;

コンストラクターで次のように設定します(オンラインチュートリアルに従ってください)

super("Line Chart of molecule numbers at different times");
     add(chartPanel, BorderLayout.CENTER);
        setSize(640, 480);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

mainメソッドで、入力および計算された数値を折れ線グラフで表示したい(nullポインター例外を恐れて別のクラスを作成しなかった)

public static void main(String args[]) throws Exception {

    //input the number of species
    System.out.println("Enter the number of species:");
    int n = sc.nextInt();

    //input the number of reactions
    System.out.println("Enter the number of reactions:");
    int m = sc.nextInt();



    //
    int[][]matrixPre = new int[m][n];
    enterMatrixDataPre(sc, matrixPre, m, n);
    printMatrixPre(matrixPre, m, n);

    //convert the 2d int to 2d double
    double [][] matrixPre2 = new double[m][n];
    for(int i = 0; i <m; i++)
    {
        for(int j = 0; j < n; j++)
            matrixPre2[i][j] = (double) matrixPre[i][j];
    }

    RealMatrix PreMatrix = new Array2DRowRealMatrix(matrixPre2);


    // remember to add space key when doing the typing
    int[][]matrixPost = new int[m][n];
    enterMatrixDataPost(sc, matrixPost, m, n);
    printMatrixPost(matrixPost, m, n);

    //convert the 2d int to 2d double
        double [][] matrixPost2 = new double[m][n];
        for(int i = 0; i <m; i++)
        {
            for(int j = 0; j < n; j++)
                matrixPost2[i][j] = (double) matrixPost[i][j];
        }

    RealMatrix PostMatrix = new Array2DRowRealMatrix(matrixPost2);


     //
    RealMatrix matrixSubtract = PreMatrix.subtract(PostMatrix);
    System.out.println("So the transpose matrix after subtraction is:\t"+matrixSubtract.transpose());

     //input the default maxium time of the whole reaction
    System.out.println("Enter the maxium time of the whole reaction:");
    double Tmax =sc.nextDouble();

    //input the name of all the species
    System.out.println("Enter the name of all species");
    String[] strs=new String[n];
    for(int i = 0; i< n; i++) {
        strs[i]=sc.nextLine();  
    }


    //input the rate constant of all the reactions(the coefficient), must be a double
    System.out.println("Enter the rate constant of each reaction:");
    Double[] rate=new Double[m];
    for(int r = 0; r< m; r++) {     
        rate[r]=sc.nextDouble();
    }


    //   
    Vector<Double> timeList = new Vector<Double>(0); 
    Vector<int[]> allStateList = new Vector<int[]>(0);
    timeList.add(newTime);

    //input the initial states of numbers of molecules
    System.out.println("Enter the initial molecule numbers of all species:");
    int[]state = new int[n];
    for (int i = 0; i< n; i++)
   {
       state[i]=sc.nextInt();
   }

   allStateList.add(state);

    while(newTime<Tmax) {

        for(int loopIndex =0; loopIndex<allStateList.size(); loopIndex++)  {            
   //  calculate the hazard for each reaction and the general hazard
    double[] h = new double[m];
    H = 0;
  try {
      for(int i =0; i<m; i++) {
          for(int j =0; j<n; j++) { 
          h[i]=rate[i]*CombinatoricsUtils.binomialCoefficientDouble(allStateList.get(loopIndex)[j],(int)(PreMatrix.getRowVector(i).toArray()[j]));
          H +=h[i];
          }
        }
      }

  catch(NumberIsTooLargeException exceptionn) {
      System.out.println("One of the species has been exhausted and there is no next firing");
      return;
  }
    System.out.println("So the general hazard is:"+H);


     // select a random reaction time
     Random random = new Random(); 
    double tau = (1*Math.log(1/random.nextDouble()))/H;
     System.out.println("So the random reaction time is"+tau);

    //put the newTime
     newTime = timeList.get(loopIndex)+tau;
     System.out.println("So the new reaction time is:" + newTime);
     timeList.add(newTime);

    //select a random reaction j
     Random random2 = new Random(); 
     int index =0;
     for (int i=0; i<m; i++) { 
        if(h[i]>random2.nextDouble()*H) 
            index =i;
  }
  System.out.println("So the next simulated event is:"+index);


      //Update the state
     double[] vectorDoubleArray = matrixSubtract.transpose().getColumnVector(index).toArray();
     int[] intArray = new int[n];
     for (int i=0; i<n; i++) {
              intArray[i] = (int) vectorDoubleArray[i];
     }
     int[] newState = new int[n];
     int newS =0;
     for (int p =0; p<n; p++){
     newS= intArray[p]+allStateList.get(loopIndex)[p];
           newState[p]=newS;           
         }
     System.out.println("Right now the molecule number of all species are:"+Arrays.toString(newState));
     allStateList.add(newState);
   }  

        //close the scanner 
     sc.close();
     }

}

すべての準備が完了したら、jfreechartを使用して2つのベクトルリストtimeListとallStateListの数を出力します。

String chartTitle = "Line chart of molecule numbers";
    String categoryAxisLabel = "time";
    String valueAxisLabel = "molecule numbers";

    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    int[] eachSpecieState = new int[allStateList.size()]; 
    for (int i =0; i<n; i++) {
        for (int j=0; j<allStateList.size(); j++) {
            eachSpecieState[i]=allStateList.get(j)[i];
        }
    }

    for (int i =0; i<m;i++) {
        String series[]= new String[m];
        series[i]=""+strs[i];
    for(int k =0; k<n;k++) {    
        for (int j=0; j<allStateList.size(); j++) {
            eachSpecieState[k]=allStateList.get(j)[k];
            dataset.addValue(eachSpecieState[k],series[i+1],""+timeList.get(j));
        }
    }   
}
    JFreeChart chart = ChartFactory.createLineChart(chartTitle,
            categoryAxisLabel, valueAxisLabel, dataset);

    chartPanel = new ChartPanel(chart);

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
           new StochasticProcess().setVisible(true);
        }
    });

ただし、グラフは表示されません。私はスイング言語があまり得意ではないので、誰かが何か問題があるかどうかを指摘するのを手伝ってくれますか?(コードの最後のプロットとチャートの部分)ありがとう〜

GeekPumpkin

そこで、この問題を解決するために、XYSeriesのリストを作成し、ループ内の各XYSeriesに新しい名前を追加しました。各XYSeriesに値を追加した後、別のループを使用してそれらをXYCollectionに追加します.1週間前に質問があり、最終結果を得るために多くの部分が変更されました。以下のコード全体の新しいバージョンを投稿して、例。

import java.util.*;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.lang.Math;

import org.apache.commons.math3.exception.NumberIsTooLargeException;
import org.apache.commons.math3.linear.Array2DRowRealMatrix;
import org.apache.commons.math3.linear.RealMatrix;
import org.apache.commons.math3.util.CombinatoricsUtils;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

主なメソッドの部分はここにあります:

public class StochasticSimulationProcess extends JFrame {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    static int matrixPre[][];
    static int matrixPost[][];
    static int m;// the number of reactions
    static int n;// the number of species
    static double[] h;// the hazard for each reaction
    static double[] rate;
    static int[] state;
    static double newTime;
    static int max;
    static double Tmax;// the overall reaction time
    Vector<Double> timeList;
    static double H;
    Vector<int[]> allStateList;
    static String[] strs;
    XYSeriesCollection dataset;


    public StochasticSimulationProcess(String s) {
        super(s);
        newTime = 0;

    }

    static Scanner sc = new Scanner(System.in);

    public static void main(String args[]) throws Exception {

        // input the number of species
        System.out.println("Enter the number of species:");
        int n = sc.nextInt();

        // input the number of reactions
        System.out.println("Enter the number of reactions:");
        int m = sc.nextInt();

        //
        int[][] matrixPre = new int[m][n];
        enterMatrixDataPre(sc, matrixPre, m, n);
        printMatrixPre(matrixPre, m, n);

        // convert the 2d int to 2d double
        double[][] matrixPre2 = new double[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++)
                matrixPre2[i][j] = (double) matrixPre[i][j];
        }

        RealMatrix PreMatrix = new Array2DRowRealMatrix(matrixPre2);

        // remember to add space key when doing the typing
        int[][] matrixPost = new int[m][n];
        enterMatrixDataPost(sc, matrixPost, m, n);
        printMatrixPost(matrixPost, m, n);

        // convert the 2d int to 2d double
        double[][] matrixPost2 = new double[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++)
                matrixPost2[i][j] = (double) matrixPost[i][j];
        }

        RealMatrix PostMatrix = new Array2DRowRealMatrix(matrixPost2);

        //
        RealMatrix matrixSubtract = PreMatrix.subtract(PostMatrix);
        System.out.println("So the transpose matrix after subtraction is:\t" + matrixSubtract.transpose());

        // input the default maxium time of the whole reaction
        System.out.println("Enter the maxium time of the whole reaction:");
        double Tmax = sc.nextDouble();

        // input the name of all the species
        System.out.println("Enter the name of all species");
        String[] strs = new String[n];
        for (int i = 0; i < n; i++) {
            strs[i] = sc.nextLine();
        }

        // input the rate constant of all the reactions(the coefficient), must be a
        // double
        System.out.println("Enter the rate constant of each reaction:");
        Double[] rate = new Double[m];
        for (int r = 0; r < m; r++) {
            rate[r] = sc.nextDouble();
        }

        //
        Vector<Double> timeList = new Vector<Double>(0);
        Vector<int[]> allStateList = new Vector<int[]>(0);
        timeList.add(newTime);

        // input the initial states of numbers of molecules
        System.out.println("Enter the initial molecule numbers of all species:");
        int[] state = new int[n];
        for (int i = 0; i < n; i++) {
            state[i] = sc.nextInt();
        }

        allStateList.add(state);
        timeLoop:
        while (newTime < Tmax) {
            for (int loopIndex = 0; loopIndex < allStateList.size(); loopIndex++) {
                // calculate the hazard for each reaction and the general hazard
                double[] h = new double[m];
                H = 0;
                try {
                    for (int i = 0; i < m; i++) {
                        for (int j = 0; j < n; j++) {
                            h[i] = rate[i] * CombinatoricsUtils.binomialCoefficientDouble(
                                    allStateList.get(loopIndex)[j], (int) (PreMatrix.getRowVector(i).toArray()[j]));
                            H += h[i];
                        }
                    }
                }

                catch (NumberIsTooLargeException exceptionn) {
                    System.out.println("One of the species has been exhausted and there is no next firing");
                    break timeLoop;
                }
                System.out.println("So the general hazard is:" + H);

                // select a random reaction time
                Random random = new Random();
                double tau = (1 * Math.log(1 / random.nextDouble())) / H;
                System.out.println("So the random reaction time is" + tau);

                // put the newTime
                newTime = timeList.get(loopIndex) + tau;
                System.out.println("So the new reaction time is:" + newTime);
                timeList.add(newTime);

                // select a random reaction j
                Random random2 = new Random();
                int index = 0;
                double hi =0;
                for (int i = 0; i < m; i++) {
                    hi +=h[i];
                    if (hi >= random2.nextDouble() * H) {
                        index = i;
                        break;
                    }
                }
                System.out.println("So the next simulated event is:" + index);

                // Update the state
                double[] vectorDoubleArray = matrixSubtract.transpose().getColumnVector(index).toArray();
                int[] intArray = new int[n];
                for (int i = 0; i < n; i++) {
                    intArray[i] = (int) vectorDoubleArray[i];
                }
                int[] newState = new int[n];
                int newS = 0;
                for (int p = 0; p < n; p++) {
                    newS = intArray[p] + allStateList.get(loopIndex)[p];
                    newState[p] = newS;
                }
                System.out.println("Right now the molecule number of all species are:" + Arrays.toString(newState));
                allStateList.add(newState);
            }

            // close the scanner
            sc.close();
        }


        XYSeriesCollection dataset = new XYSeriesCollection();
        XYSeries[] species = new XYSeries[n];
        for (int j =0; j<n; j++) {
            species[j]=new XYSeries(""+strs[j]+Integer.toString(j));
        }


        for (int j =0; j<n; j++) {
            for (int k=0; k< allStateList.size();k++) {
            species[j].add(timeList.get(k).doubleValue(), allStateList.get(k)[j]);
        }
            dataset.addSeries(species[j]);
    }

        // plot out the graph
        System.out.println("TEST");

        JFreeChart chart = ChartFactory.createXYLineChart(
                "SSA Modeling", 
                "time", 
                "Number of Molecules", 
                dataset, 
                PlotOrientation.VERTICAL,
                true, 
                true, 
                false
        );

         ChartPanel chartPanel = new ChartPanel(chart);
            chartPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
            chartPanel.setBackground(Color.white);
           XYPlot plot = chart.getXYPlot();


            XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();

            for (int j =0; j<n;j++){
             renderer.setSeriesPaint(j, Color.RED);
             renderer.setSeriesStroke(j, new BasicStroke(2.0f));
            }


            plot.setRenderer(renderer);
            plot.setBackgroundPaint(Color.white);

            plot.setRangeGridlinesVisible(false);
            plot.setDomainGridlinesVisible(false);


           chart.getLegend().setFrame(BlockBorder.NONE);

            chart.setTitle(new TextTitle("Stochastic Simulation Algorithm",
                            new Font("Serif", Font.BOLD, 18)
                    )
            );

        SwingUtilities.invokeLater(() -> {
            StochasticSimulationProcess ex = new StochasticSimulationProcess("SSA");
            ex.setContentPane(chartPanel);
            ex.pack();
            ex.setLocationRelativeTo(null);
            ex.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
            ex.setVisible(true);

        });
    }

    public static void enterMatrixDataPre(Scanner sc, int[][] matrixPre, int m, int n) {
        System.out.println("Enter Matrix pre data");
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                matrixPre[i][j] = sc.nextInt();
            }
        }
    }

    public static void printMatrixPre(int[][] matrixPre, int m, int n) {
        System.out.println("Matrix pre is:");
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(matrixPre[i][j] + "\t");
            }
            System.out.println();
        }
    }

    public static void enterMatrixDataPost(Scanner sc, int[][] matrixPost, int m, int n) {
        System.out.println("Enter Matrix post data");
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                matrixPost[i][j] = sc.nextInt();
            }
        }
    }

    public static void printMatrixPost(int[][] matrixPost, int m, int n) {
        System.out.println("Matrix post is:");
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(matrixPost[i][j] + "\t");
            }
            System.out.println();
        }
    }
}

種を表すXYSeriesの数が定義されていないため、色をランダムに変更するには、値が0〜255の間であればランダムに組み合わせることができるため、ランダムセットhgbメソッドを選択します。したがって、コードの行を次のように変更します。

renderer.setSeriesPaint(j, Color.getHSBColor(0 + (float)(Math.random() * ((255 - 0) + 1)), 0 + (float)(Math.random() * ((255 - 0) + 1)), 0 + (float)(Math.random() * ((255 - 0) + 1))));

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

位置のインデックスに関してタプルの2つのリストを比較する方法

分類Dev

1つのプロットにスペクトルデータをプロットする

分類Dev

ggridgesに2つのカテゴリベクトルをプロットする

分類Dev

デジタル顕微鏡写真スクリプトで色相と明るさの方法を使用して、1つの画像に2Dベクトル場をプロットする方法は?

分類Dev

2つのリストから始めて、各リストの同じインデックスの要素をタプルのリストに配置する方法

分類Dev

プロトンCEPのデータベースを「リセット」する方法

分類Dev

PyQtGraphの1つのプロットに2つのリアルタイムデータをプロットする方法は?

分類Dev

ボタンクリックイベントで2つのソースデータをDataTablesにロードする方法

分類Dev

2つのLaravelEloquentクエリを1つのクエリにする方法(データベースに2回ではなく1回ヒットするため)

分類Dev

大きなデータベクトルをすべてのズームレベルでリアルタイムに正確にプロットする方法

分類Dev

イベントクラスのlmaxディスラプターにリストをプッシュする方法

分類Dev

Dockerデーモンのブートストラップ中に2つのデータディレクトリを構成する方法はありますか?

分類Dev

スピードアップのためにmatlabで2つのforループをベクトル化する方法

分類Dev

プロローグの 2 つのリストに基づいてインデックス値のリストを作成する方法

分類Dev

2つの別々のベクトルをプロットする

分類Dev

スタック内にすでにデータ値があるgeom_barスタックプロットに合計のラベルを含める方法

分類Dev

C ++ 2つのデータ型をベクトルに格納する方法

分類Dev

ドロップダウンリストの値をdjangoのデータベースに保存する方法

分類Dev

ノックアウトのネストされたforループでデータバインディング中に2つのデータを比較する方法

分類Dev

2つのログ間のデルタを追跡するログベースのメトリックを作成します

分類Dev

2つのデータベースを同時に接続するperlスクリプト

分類Dev

2つのベクトルを交互にインデックスにマージする方法は?

分類Dev

リスト/ペアのタプルを2つのリスト/タプルにアンパックする

分類Dev

リスト/ペアのタプルを2つのリスト/タプルにアンパックする

分類Dev

GIT:2つのリポジトリにblobの1つのデータベースを使用する方法

分類Dev

データベースからのphp結果を同じWebページのHTMLテーブルに表示する方法(1つのスクリプトで)

分類Dev

データベースプロジェクトはデータベースインスタンスに接続します-プロジェクト内の実際のデータベースのサブセットを管理する方法

分類Dev

データテーブルのスパークラインプロットにラベルを追加する

分類Dev

クロスデータベースセットアップでシリアル化されたモデルのlaravel通知を使用する方法

Related 関連記事

  1. 1

    位置のインデックスに関してタプルの2つのリストを比較する方法

  2. 2

    1つのプロットにスペクトルデータをプロットする

  3. 3

    ggridgesに2つのカテゴリベクトルをプロットする

  4. 4

    デジタル顕微鏡写真スクリプトで色相と明るさの方法を使用して、1つの画像に2Dベクトル場をプロットする方法は?

  5. 5

    2つのリストから始めて、各リストの同じインデックスの要素をタプルのリストに配置する方法

  6. 6

    プロトンCEPのデータベースを「リセット」する方法

  7. 7

    PyQtGraphの1つのプロットに2つのリアルタイムデータをプロットする方法は?

  8. 8

    ボタンクリックイベントで2つのソースデータをDataTablesにロードする方法

  9. 9

    2つのLaravelEloquentクエリを1つのクエリにする方法(データベースに2回ではなく1回ヒットするため)

  10. 10

    大きなデータベクトルをすべてのズームレベルでリアルタイムに正確にプロットする方法

  11. 11

    イベントクラスのlmaxディスラプターにリストをプッシュする方法

  12. 12

    Dockerデーモンのブートストラップ中に2つのデータディレクトリを構成する方法はありますか?

  13. 13

    スピードアップのためにmatlabで2つのforループをベクトル化する方法

  14. 14

    プロローグの 2 つのリストに基づいてインデックス値のリストを作成する方法

  15. 15

    2つの別々のベクトルをプロットする

  16. 16

    スタック内にすでにデータ値があるgeom_barスタックプロットに合計のラベルを含める方法

  17. 17

    C ++ 2つのデータ型をベクトルに格納する方法

  18. 18

    ドロップダウンリストの値をdjangoのデータベースに保存する方法

  19. 19

    ノックアウトのネストされたforループでデータバインディング中に2つのデータを比較する方法

  20. 20

    2つのログ間のデルタを追跡するログベースのメトリックを作成します

  21. 21

    2つのデータベースを同時に接続するperlスクリプト

  22. 22

    2つのベクトルを交互にインデックスにマージする方法は?

  23. 23

    リスト/ペアのタプルを2つのリスト/タプルにアンパックする

  24. 24

    リスト/ペアのタプルを2つのリスト/タプルにアンパックする

  25. 25

    GIT:2つのリポジトリにblobの1つのデータベースを使用する方法

  26. 26

    データベースからのphp結果を同じWebページのHTMLテーブルに表示する方法(1つのスクリプトで)

  27. 27

    データベースプロジェクトはデータベースインスタンスに接続します-プロジェクト内の実際のデータベースのサブセットを管理する方法

  28. 28

    データテーブルのスパークラインプロットにラベルを追加する

  29. 29

    クロスデータベースセットアップでシリアル化されたモデルのlaravel通知を使用する方法

ホットタグ

アーカイブ