2

我有一些数据存储在这样的数据库中:

表名:故障表:

+------------+--------------+
| fault_type | total        |
+------------+--------------+
|    1       |            1 | 
|    2       |            3 | 
|    3       |            8 | 
|    4       |            2 | 
.............................

我应该如何从这张表开始得到直方图?

4

2 回答 2

2

下面的解决方案假设你有 MySQL、Python 和 GNUPlot。如有需要,具体细节可以微调。发布它,以便它可以成为其他同行的基准。

第 1 步:确定图表的类型。

如果它是某种频率图,那么一个简单的 SQL 查询就可以解决问题:

select total, count(total) from faults GROUP BY total;

如果您需要指定 bin 大小,请继续执行下一步。

第 2 步:确保您能够使用 Python 连接到 MySQL。您可以使用 MySQLdb 导入来执行此操作。

之后,为直方图生成数据的 python 代码如下(这是在 5 分钟内精确编写的,因此非常粗糙):

import MySQLdb

def DumpHistogramData(databaseHost, databaseName, databaseUsername, databasePassword, dataTableName, binsTableName, binSize, histogramDataFilename):
    #Open a file for writing into
    output = open("./" + histogramDataFilename, "w")

    #Connect to the database
    db = MySQLdb.connect(databaseHost, databaseUsername, databasePassword, databaseName)
    cursor = db.cursor()

    #Form the query
    sql = """select b.*, count(*) as total 
            FROM """ + binsTableName + """ b 
            LEFT OUTER JOIN """ + dataTableName + """ a 
            ON a.total between b.min AND b.max 
            group by b.min;"""
    cursor.execute(sql)

    #Get the result and print it into a file for further processing
    count = 0;
    while True:
        results = cursor.fetchmany(10000)
        if not results:
            break
        for result in results:
            #print >> output, str(result[0]) + "-" + str(result[1]) + "\t" + str(result[2])
    db.close()

def PrepareHistogramBins(databaseHost, databaseName, databaseUsername, databasePassword, binsTableName, maxValue, totalBins):

    #Connect to the database    
    db = MySQLdb.connect(databaseHost, databaseUsername, databasePassword, databaseName)
    cursor = db.cursor()

    #Check if the table was already created
    sql = """DROP TABLE IF EXISTS """ + binsTableName
    cursor.execute(sql)

    #Create the table
    sql = """CREATE TABLE """ + binsTableName + """(min int(11), max int(11));"""
    cursor.execute(sql)

    #Calculate the bin size
    binSize = maxValue/totalBins

    #Generate the bin sizes
    for i in range(0, maxValue, binSize):
        if i is 0:
            min = i
            max = i+binSize
        else:
            min = i+1
            max = i+binSize
        sql = """INSERT INTO """ + binsTableName + """(min, max) VALUES(""" + str(min) + """, """ + str(max) + """);"""
        cursor.execute(sql)
    db.close()
    return binSize

binSize = PrepareHistogramBins("localhost", "testing", "root", "", "bins", 5000, 100)
DumpHistogramData("localhost", "testing", "root", "", "faults", "bins", binSize, "histogram")

第 3 步:使用 GNUPlot 生成直方图。您可以使用以下脚本作为起点(生成 eps 图像文件):

set terminal postscript eps color lw 2 "Helvetica" 20
set output "output.eps"
set xlabel "XLABEL"
set ylabel "YLABEL"
set title "TITLE"
set style data histogram
set style histogram cluster gap 1
set style fill solid border -1
set boxwidth 0.9
set key autotitle columnheader
set xtics rotate by -45
plot "input" using 1:2 with linespoints ls 1

将上述脚本保存到任意文件中,例如 sample.script。继续下一步。

步骤#4:使用带有上述输入脚本的 gnuplot 生成一个 eps 文件

gnuplot sample.script

没什么复杂的,但我认为这段代码中的一些位可以重用。同样,就像我说的,它并不完美,但你可以完成工作:)

学分:

  • Ofri Raviv(在这篇文章中帮助我处理 MySQL 查询: Getting data for histogram plot

  • 我自己(用于编写 python 和 gnuplot 脚本:D)

于 2009-11-20T00:22:23.400 回答
0

这篇博客文章可能会对您有所帮助!它使用 gnuplot 讨论统计数据并将结果绘制成直方图。

于 2011-09-17T11:16:26.000 回答