Python matplotlib绘制平滑折线图
时间: 2023-10-14 11:08:19 浏览: 56
好的,我可以帮你解决这个问题。你可以使用 matplotlib 库中的 SmoothedLineCollection 类来绘制平滑折线图。以下是一个简单的示例代码:
```python
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib.colors import ListedColormap
# Generate some random data
x = np.linspace(0, 2*np.pi, 1000)
y = np.sin(x)
# Create segments from the data
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
# Create a colormap
cmap = ListedColormap(["#0072BD"])
# Create a smoothed line collection
smooth_line = LineCollection(segments, cmap=cmap, linewidth=2)
smooth_line.set_array(np.linspace(0, 1, len(x)))
# Plot the smoothed line collection
fig, ax = plt.subplots()
ax.add_collection(smooth_line)
ax.autoscale()
ax.margins(0.1)
plt.show()
```
这段代码将生成一个包含平滑折线的图形,其中折线是根据 `x` 和 `y` 数组生成的。折线利用 `SmoothedLineCollection` 类来绘制,颜色采用了自定义的 `ListedColormap`,线宽度为 2。你可以根据你的数据自定义这些参数。