gg349的答案效果很好,但將線切割成許多片段,這往往會造成不良的渲染。
這裏是生成連續的線時的寬度是均勻的可替代的示例:
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1)
xs = np.cos(np.linspace(0, 8 * np.pi, 200)) * np.linspace(0, 1, 200)
ys = np.sin(np.linspace(0, 8 * np.pi, 200)) * np.linspace(0, 1, 200)
widths = np.round(np.linspace(1, 5, len(xs)))
def plot_widths(xs, ys, widths, ax=None, color='b', xlim=None, ylim=None,
**kwargs):
if not (len(xs) == len(ys) == len(widths)):
raise ValueError('xs, ys, and widths must have identical lengths')
fig = None
if ax is None:
fig, ax = plt.subplots(1)
segmentx, segmenty = [xs[0]], [ys[0]]
current_width = widths[0]
for ii, (x, y, width) in enumerate(zip(xs, ys, widths)):
segmentx.append(x)
segmenty.append(y)
if (width != current_width) or (ii == (len(xs) - 1)):
ax.plot(segmentx, segmenty, linewidth=current_width, color=color,
**kwargs)
segmentx, segmenty = [x], [y]
current_width = width
if xlim is None:
xlim = [min(xs), max(xs)]
if ylim is None:
ylim = [min(ys), max(ys)]
ax.set_xlim(xlim)
ax.set_ylim(ylim)
return ax if fig is None else fig
plot_widths(xs, ys, widths)
plt.show()
不錯!因此,您將該線切割爲一系列部分,並使用「LineCollection」指定每個部分的屬性。 – Hamid
根本不是我想找的東西,但是這很酷,所以我投了贊成票:) –
學習曲線陡峭的matplotlib - 試圖找出如何適應這個圖形,其中'x'軸包含'時間戳「,顯然段預計'浮動',而不是'時間戳'......任何線索?這是否正是我正在尋找的,除了無法真正產生一個圖... – dwanderson