绘制直方图[30,17,3],[38,8,4],[ 35,8,7],[ 43,3,4],[ 28,12,10],[ 45,2,3]
时间: 2023-10-29 07:05:01 浏览: 26
以下是Python代码实现:
```python
import matplotlib.pyplot as plt
data = [[30, 17, 3], [38, 8, 4], [35, 8, 7], [43, 3, 4], [28, 12, 10], [45, 2, 3]] # 数据
labels = ['A', 'B', 'C', 'D', 'E', 'F'] # 标签
x = range(len(labels)) # x轴刻度位置
width = 0.25 # 条形图宽度
fig, ax = plt.subplots()
rects1 = ax.bar(x, [i[0] for i in data], width, label='数据1')
rects2 = ax.bar([i + width for i in x], [i[1] for i in data], width, label='数据2')
rects3 = ax.bar([i + 2*width for i in x], [i[2] for i in data], width, label='数据3')
ax.set_xticks([i + width for i in x])
ax.set_xticklabels(labels)
ax.legend()
plt.show()
```
结果如下图所示:
![histogram.png](https://i.loli.net/2021/06/18/5lSM7vK8jUWJ4hg.png)
相关问题
python3中绘制直方图
在Python3中,要绘制直方图可以使用matplotlib库的hist()函数。首先,我们需要导入matplotlib库和numpy库(用于生成随机数据)。
```python
import matplotlib.pyplot as plt
import numpy as np
```
然后,我们可以使用numpy库生成一些随机数据作为直方图的输入。这里假设我们生成了1000个0到100之间的随机数。
```python
data = np.random.randint(0, 100, 1000)
```
接着,我们使用matplotlib的hist()函数绘制直方图。该函数的参数包括输入的数据、直方图的区间数(bins)以及其他一些参数。
```python
plt.hist(data, bins=10, color='steelblue', edgecolor='k')
```
其中,bins参数表示将数据分成几个区间,默认为10个。color参数表示直方图的颜色,这里我们设置为steelblue。edgecolor参数表示直方图的边缘颜色,这里我们设置为黑色。
最后,我们可以添加一些标记、标题和网格线等装饰。例如,我们可以添加坐标轴标签、标题和网格线。
```python
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram')
plt.grid(True)
```
最后,我们使用plt.show()函数显示直方图。
```python
plt.show()
```
整个绘制直方图的过程就是这样,通过以上步骤,我们可以使用Python3绘制直方图。
Python绘制直方图
Python中可以使用matplotlib库来绘制直方图。下面是一个简单的示例代码:
```python
import matplotlib.pyplot as plt
# 数据
data = [1, 2, 3, 3, 4, 4, 4, 5, 5, 6, 7, 7, 8, 9]
# 绘制直方图
plt.hist(data, bins=5, edgecolor='black')
# 设置标题和坐标轴标签
plt.title("Histogram")
plt.xlabel("Value")
plt.ylabel("Frequency")
# 显示图形
plt.show()
```
这段代码首先导入了matplotlib.pyplot库,并创建了一个包含数据的列表。然后使用`plt.hist()`函数绘制直方图,其中`data`是数据列表,`bins`参数指定了直方图的柱子数量,`edgecolor`参数指定了柱子边框的颜色。
接下来,使用`plt.title()`、`plt.xlabel()`和`plt.ylabel()`函数设置了标题和坐标轴标签。
最后,使用`plt.show()`函数显示了绘制好的直方图。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)