5

値のリストに従って線の幅を変更できるようにしたいと考えています。たとえば、プロットする次のリストがあるとします。

a = [0.0、1.0、2.0、3.0、4.0]

次のリストを使用して線幅を設定できますか?

b = [1.0、1.5、3.0、2.0、1.0]

サポートされていないようですが、「何でも可能です」と言うので、経験豊富な人(noob here)に尋ねようと思いました。

ありがとう

4

1 に答える 1

11

基本的に、2 つのオプションがあります。

  1. を使用しLineCollectionます。この場合、線幅はポイント単位になり、線幅は各セグメントで一定になります。
  2. ポリゴンを使用します ( を使用すると最も簡単fill_betweenですが、複雑な曲線の場合は直接作成する必要がある場合があります)。この場合、線幅はデータ単位になり、線の各セグメント間で直線的に変化します。

両方の例を次に示します。

ライン コレクションの例


import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
np.random.seed(1977)

x = np.arange(10)
y = np.cos(x / np.pi)
width = 20 * np.random.random(x.shape)

# Create the line collection. Widths are in _points_!  A line collection
# consists of a series of segments, so we need to reformat the data slightly.
coords = zip(x, y)
lines = [(start, end) for start, end in zip(coords[:-1], coords[1:])]
lines = LineCollection(lines, linewidths=width)

fig, ax = plt.subplots()
ax.add_collection(lines)
ax.autoscale()
plt.show()

ここに画像の説明を入力

ポリゴンの例:


import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1977)

x = np.arange(10)
y = np.cos(x / np.pi)
width = 0.5 * np.random.random(x.shape)

fig, ax = plt.subplots()
ax.fill_between(x, y - width/2, y + width/2)
plt.show()

ここに画像の説明を入力

于 2013-11-08T15:49:02.063 に答える