绘制直方图:如何使用数据库中存储的数据从头开始绘制直方图?
我有一些数据存储在数据库中,如下所示:
TableName:faults 表:
+------------+--------------+
| fault_type | total |
+------------+--------------+
| 1 | 1 |
| 2 | 3 |
| 3 | 8 |
| 4 | 2 |
.............................
我该如何从该表开始获得直方图?
I have some data stored in a database like this:
TableName: faults
Table:
+------------+--------------+
| fault_type | total |
+------------+--------------+
| 1 | 1 |
| 2 | 3 |
| 3 | 8 |
| 4 | 2 |
.............................
How am I supposed to get a histogram plot starting from this table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面的解决方案假设您有 MySQL、Python 和 GNUPlot。如果需要的话,具体细节可以进行微调。发布它以便它可以成为其他同行的基准。
第 1 步:决定图表的类型。
如果它是某种频率图,那么一个简单的 SQL 查询就可以解决问题:
如果您需要指定 bin 大小,则继续下一步步。
第 2 步:确保您能够使用 Python 连接到 MySQL。您可以使用 MySQLdb 导入来执行此操作。
之后,生成直方图数据的 python 代码如下(这是在 5 分钟内写完的,所以非常粗糙):
Step # 3:使用GNUPlot生成直方图。您可以使用以下脚本作为起点(生成 eps 图像文件):
将上述脚本保存到任意文件中,例如 example.script。继续下一步。
步骤 #4:使用 gnuplot 和上述输入脚本生成 eps 文件
没什么复杂的,但我认为这段代码中的一些位可以重复使用。再说一次,就像我说的,它并不完美,但你可以完成工作:)
鸣谢:
Ofri Raviv(帮助我解决
这篇文章中的 MySQL 查询:
获取直方图数据)
我自己(用于编写 python 和
gnuplot 脚本 :D)
The solution below assumes that you have MySQL, Python and GNUPlot. The specific details can be fine tuned if necessary. Posting it so that it could be a baseline for other peers.
Step #1: Decide the type of graph.
If it is a frequency plot of some kind, then a simple SQL query should do the trick:
If you need to specify bin sizes, then proceed to the next step.
Step #2: Make sure you are able to connect to MySQL using Python. You can use the MySQLdb import to do this.
After that, the python code to generate data for a histogram plot is the following (this was written precisely in 5 minutes so it is very crude):
Step #3: Use GNUPlot to generate the histogram. You can use the following script as a starting point (generates an eps image file):
Save the above script into some arbitrary file say, sample.script. Proceed to the next step.
Step #4: Use gnuplot with the above input script to generate an eps file
Nothing complicated but I figured a couple of bits from this code can be reused. Again, like I said, it is not perfect but you can get the job done :)
Credits:
Ofri Raviv (for helping me out with
the MySQL query in this post:
Getting data for histogram plot)
Myself (for writing the python and
gnuplot script :D)
这篇博客文章可能会对您有所帮助!它讨论了使用 gnuplot 进行统计并将结果绘制成直方图。
This blog article may help you! It talks about statistic using gnuplot and plot the result into histogram.