2017-05-30 3 views

答えて

0

あなたはmulticolored lineを作成するためにLineCollectionを使用することができます。次に、x軸に依存せずにx軸に固定しておくために、x軸変換を使用することができます。実際の脊柱を不可視に設定し、clip_onをオフにすると、LineCollectionは軸スパインのように見えます。

import matplotlib.pyplot as plt 
from matplotlib.collections import LineCollection 
import numpy as np 

fig, ax = plt.subplots() 

colors=["b","r","lightgreen","gold"] 
x=[0,.25,.5,.75,1] 
y=[0,0,0,0,0] 
points = np.array([x, y]).T.reshape(-1, 1, 2) 
segments = np.concatenate([points[:-1], points[1:]], axis=1) 
lc = LineCollection(segments,colors=colors, linewidth=2, 
           transform=ax.get_xaxis_transform(), clip_on=False) 
ax.add_collection(lc) 
ax.spines["bottom"].set_visible(False) 
ax.set_xticks(x) 
plt.show() 

enter image description here

+0

恐ろしいです。 'transform = ax.get_xaxis_transform()'は何を記述することができますか?私は 'transform'プロパティ定義を見つけることができません。 – JeeYem

+0

[transform tutorial](http://matplotlib.org/users/transforms_tutorial.html)を読んでみてください。すべてのアーティストはトランスフォームプロパティを持っています。変換によって座標がキャンバスにマップされます。通常は、あまり気にしません。なぜなら、データ座標を使用し、アーティストがデフォルトでtransDataトランスフォームを使用するからです。ここでは、データ座標に応じてy方向に線の位置を持たずに、座標軸に固定したいと考えています。 'ax.get_xaxis_transform()'は、データ単位のx座標と軸単位のy座標を必要とする変換を与えます。 – ImportanceOfBeingErnest

関連する問題