用python中的matplotlib库,自己设定四组数据,根据数据画出画出第一组数据的折线图,第二组数据的柱状图,第三组数据的饼图以及第四组数据的雷达图,给出相应的代码
时间: 2024-03-05 22:51:40 浏览: 56
好的,以下是使用 `matplotlib` 库绘制四种图形的代码示例:
1. 折线图(Line Chart)
```python
import matplotlib.pyplot as plt
# 第一组数据
x = [1, 2, 3, 4, 5]
y = [10, 8, 6, 4, 2]
plt.plot(x, y)
plt.title('Line Chart')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.show()
```
2. 柱状图(Bar Chart)
```python
import matplotlib.pyplot as plt
# 第二组数据
labels = ['A', 'B', 'C', 'D', 'E']
values = [5, 3, 7, 2, 8]
plt.bar(labels, values)
plt.title('Bar Chart')
plt.xlabel('Labels')
plt.ylabel('Values')
plt.show()
```
3. 饼图(Pie Chart)
```python
import matplotlib.pyplot as plt
# 第三组数据
labels = ['A', 'B', 'C', 'D', 'E']
sizes = [15, 30, 10, 20, 25]
plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.title('Pie Chart')
plt.axis('equal')
plt.show()
```
4. 雷达图(Radar Chart)
```python
import matplotlib.pyplot as plt
import numpy as np
# 第四组数据
labels = ['A', 'B', 'C', 'D', 'E']
values = [5, 3, 7, 2, 8]
angles = np.linspace(0, 2*np.pi, len(labels), endpoint=False)
values = np.concatenate((values, [values[0]]))
angles = np.concatenate((angles, [angles[0]]))
fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
ax.plot(angles, values, 'o-', linewidth=2)
ax.fill(angles, values, alpha=0.25)
ax.set_thetagrids(angles * 180/np.pi, labels)
ax.set_title('Radar Chart')
ax.grid(True)
plt.show()
```
希望这些代码对你有所帮助!
阅读全文
相关推荐
















