Python: кривая с плавным изменением цвета вдоль его длины [дубликат]

Другой необычный подход, который работает для гетеро- и однородных списков целых чисел:

def unusual_flatten(some_list: list) -> list:
    cleaned_list = str(some_list).replace(("["), "").replace("]", "").split(",")
    return [int(item) for item in cleaned_list]

Применение в списке примеров ...

l = [[1, 2, 3], [4, 5, 6], [7], [8, 9], 10]

unusual_flatten(l)

Результат:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

10
задан Tom Stephens 20 April 2012 в 20:52
поделиться

1 ответ

Второй пример - тот, который вы хотите ... Я отредактировал его в соответствии с вашим примером, но, что более важно, прочитал мои комментарии, чтобы понять, что происходит:

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())
10
ответ дан Ethan Coon 28 August 2018 в 17:13
поделиться
Другие вопросы по тегам:

Похожие вопросы: