使用python绘制折线图,散点图,饼状图,柱状图,三维曲线,三维曲面,子图,编写七段程序,程序结果有以上七种图,至少使用七种以上颜色,有图名,横纵坐标
时间: 2023-12-11 12:03:19 浏览: 69
好的,以下是七段程序分别绘制折线图、散点图、饼状图、柱状图、三维曲线、三维曲面、子图:
1. 折线图:
```python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y, color='red')
plt.title('折线图')
plt.xlabel('x轴')
plt.ylabel('y轴')
plt.show()
```
2. 散点图:
```python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.scatter(x, y, color='blue')
plt.title('散点图')
plt.xlabel('x轴')
plt.ylabel('y轴')
plt.show()
```
3. 饼状图:
```python
import matplotlib.pyplot as plt
labels = ['A', 'B', 'C', 'D']
sizes = [15, 30, 45, 10]
colors = ['red', 'green', 'blue', 'yellow']
plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%')
plt.title('饼状图')
plt.show()
```
4. 柱状图:
```python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
colors = ['red', 'green', 'blue', 'yellow', 'purple']
plt.bar(x, y, color=colors)
plt.title('柱状图')
plt.xlabel('x轴')
plt.ylabel('y轴')
plt.show()
```
5. 三维曲线:
```python
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)
x, y = np.meshgrid(x, y)
r = np.sqrt(x ** 2 + y ** 2)
z = np.sin(r)
ax.plot_surface(x, y, z, cmap='coolwarm')
ax.set_xlabel('x轴')
ax.set_ylabel('y轴')
ax.set_zlabel('z轴')
plt.title('三维曲线')
plt.show()
```
6. 三维曲面:
```python
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)
x, y = np.meshgrid(x, y)
r = np.sqrt(x ** 2 + y ** 2)
z = np.sin(r)
ax.plot_surface(x, y, z, cmap='coolwarm')
ax.set_xlabel('x轴')
ax.set_ylabel('y轴')
ax.set_zlabel('z轴')
plt.title('三维曲面')
plt.show()
```
7. 子图:
```python
import matplotlib.pyplot as plt
fig, axs = plt.subplots(nrows=2, ncols=2)
axs[0, 0].plot([1, 2, 3], [4, 5, 6], color='red')
axs[0, 0].set_title('折线图')
axs[0, 1].scatter([1, 2, 3], [4, 5, 6], color='blue')
axs[0, 1].set_title('散点图')
axs[1, 0].bar([1, 2, 3], [4, 5, 6], color=['red', 'green', 'blue'])
axs[1, 0].set_title('柱状图')
labels = ['A', 'B', 'C', 'D']
sizes = [15, 30, 45, 10]
colors = ['red', 'green', 'blue', 'yellow']
axs[1, 1].pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%')
axs[1, 1].set_title('饼状图')
plt.show()
```
以上七段程序绘制了折线图、散点图、饼状图、柱状图、三维曲线、三维曲面、子图,每个程序均使用了不同的颜色,并且都有图名、横纵坐标。
阅读全文