2012-04-20 15 views
9

में प्राकृतिक समय पैरामीटर को कैप्चर करने के लिए लाइन के अलग-अलग रंग, मैं दो सरणी (उदाहरण के लिए ax.plot(x,y)) में डेटा से प्लॉट की गई रेखा के रंग को बदलने की कोशिश कर रहा हूं। सूचकांक x और y में सूचकांक के रूप में भिन्न होना चाहिए। मैं अनिवार्य रूप से सरणी x और y में डेटा के प्राकृतिक 'समय' पैरामीटर को पकड़ने की कोशिश कर रहा हूं।matplotlib: डेटा

fig = pyplot.figure() 
ax = fig.add_subplot(111) 
x = myXdata 
y = myYdata 

# length of x and y is 100 
ax.plot(x,y,color=[i/100,0,0]) # where i is the index into x (and y) 

रंग गहरे लाल करने के लिए और पर चमकदार लाल में काला से अलग के साथ एक लाइन का निर्माण करने के:

एक आदर्श दुनिया में, मैं कुछ की तरह चाहते हैं।

मैं examples कि एक समारोह स्पष्ट रूप से कुछ 'समय' सरणी द्वारा parameterized की साजिश रचने के लिए अच्छी तरह से काम देखा है, लेकिन मैं इसे कच्चे डेटा के साथ काम करने के लिए नहीं मिल सकता है ...

उत्तर

10

दूसरे उदाहरण एक आप है चाहते हैं ... मैं इसे अपने उदाहरण फिट करने के लिए संपादित किया है, लेकिन अधिक महत्वपूर्ण बात यह मेरी टिप्पणी पढ़ने को समझने के लिए क्या हो रहा है:

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

x = myXdata 
y = myYdata 
t = np.linspace(0,1,x.shape[0]) # your "time" variable 

# set up a list of (x,y) points 
points = np.array([x,y]).transpose().reshape(-1,1,2) 
print points.shape # Out: (len(x),1,2) 

# set up a list of segments 
segs = np.concatenate([points[:-1],points[1:]],axis=1) 
print segs.shape # Out: (len(x)-1, 2, 2) 
        # see what we've done here -- we've mapped our (x,y) 
        # points to an array of segment start/end coordinates. 
        # segs[i,0,:] == segs[i-1,1,:] 

# make the collection of segments 
lc = LineCollection(segs, cmap=plt.get_cmap('jet')) 
lc.set_array(t) # color the segments by our parameter 

# plot the collection 
plt.gca().add_collection(lc) # add the collection to the plot 
plt.xlim(x.min(), x.max()) # line collections don't auto-scale the plot 
plt.ylim(y.min(), y.max()) 
+0

उनका कहना है क्या आकृति बदलें और संयोजन के साथ क्या हो रहा है के लिए धन्यवाद। यह अच्छी तरह से काम कर रहा है। –

+0

यदि आप लाइन सेगमेंट के बीच एक आसान संक्रमण चाहते हैं तो आप 'segs = np.concatenate ([अंक [: - 2], अंक [1: -1], अंक [2:]], अक्ष = 1)' इसके बजाय कर सकते हैं। – shockburner