【问题标题】:Plotting Histogram: How can I do it from scratch using data stored in a database?绘制直方图:如何使用存储在数据库中的数据从头开始?
【发布时间】:2009-11-20 00:22:07
【问题描述】:

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

表名:错误 表:

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

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

【问题讨论】:

    标签: python mysql graph gnuplot histogram


    【解决方案1】:

    以下解决方案假设您拥有 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
    

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

    学分:

    【讨论】:

      【解决方案2】:

      This blog article 可以帮到你!它讨论了使用 gnuplot 进行统计并将结果绘制成直方图。

      【讨论】:

        猜你喜欢
        • 2023-03-08
        • 1970-01-01
        • 2019-04-17
        • 2023-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多