编程制统计图怎么画出来

时间:2025-01-25 13:40:12 游戏攻略

编程制作统计图的方法取决于你使用的编程语言和库。以下是几种常见编程语言制作统计图的步骤和示例代码:

1. 使用Python的matplotlib库

Python的matplotlib库是一个非常强大的绘图库,适合制作各种统计图。

```python

import matplotlib.pyplot as plt

import numpy as np

添加图形属性

plt.xlabel('Age range')

plt.ylabel('Number')

plt.title('The statistics of face age dataset')

创建子图

a = plt.subplot(1, 1, 1)

设置坐标轴范围

plt.ylim=(10, 40000)

定义数据

x = [10, 20, 30, 40, 50, 60, 70]

x1 = [7, 17, 27, 37, 47, 57, 67]

x2 = [13, 23, 33, 43, 53, 63, 73]

Y1 = [41, 39, 13, 69, 39, 14, 7]

Y2 = [0, 15, 20, 105, 79, 37, 43]

Y3 = [0, 91, 404, 464, 521, 375, 553]

绘制柱状图

plt.bar(x1, Y1, facecolor='red', width=3, label='FG-NET')

plt.bar(x, Y2, facecolor='green', width=3, label='MORPH')

plt.bar(x2, Y3, width=3)

添加图例

plt.legend()

显示图形

plt.show()

```

2. 使用PHP的JqGraph库

JqGraph是一个适合PHP的图表库,可以制作多种类型的图表。

```php

// 示例代码(需要安装JqGraph库)

$graph = new jqGraph('chart1', '600px', '400px');

$graph->setTheme("simple");

$data1 = array(10, 20, 30, 40, 50, 60, 70);

$data2 = array(7, 17, 27, 37, 47, 57, 67);

$data3 = array(13, 23, 33, 43, 53, 63, 73);

$names1 = array('FG-NET');

$names2 = array('MORPH');

$names3 = array('Other');

$graph->addPlot('p1', 'bar', array($data1, $data2, $data3), array($names1, $names2, $names3));

$graph->setLegend('bottom');

$graph->draw();

```

3. 使用Java的JFreeChart库

JFreeChart是一个适合Java的图表库,可以制作多种类型的图表。