字符串linecolllection的五彩的线 [英] multicolored line with strings linecolllection
问题描述
我正在使用matplotlib中的linecollection来创建类似于此示例的多色线:
I'm using linecollection from matplotlib in order to create multi colored line similar to this example: http://matplotlib.org/examples/pylab_examples/multicolored_line.html
but instead of using x and y both float types, I was wondering if its possible to create a line where x axis would be strings lets say: x=['a','b','c','d','e','f','g','h','j','k']
. So every of those strings has a value for example y=np.arange(10)
. So is plotting a multicolored line that connects those xy points using linecollection possible?
Okay I managed to do this, the code below if you need. The data(gbpndupl) im plotting is simple Series where index column is publisher names and the other column are the numbers. I also uploaded the image of how it looks like.
from matplotlib.collections import LineCollection
plt.figure(figsize=(15, 5))
x=np.arange(40)
y=gbpndupl.iloc[:40]
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
lc = LineCollection(segments, cmap='plasma',norm=plt.Normalize(0, 10))
#norm can be changed to decide how fast color changes
lc.set_linewidth(3)
lc.set_array(x)
plt.xlim(min(x), max(x))
plt.ylim(min(y), max(y))
plt.gca().add_collection(lc)
labels=list(gbpndupl.iloc[:40].index)
plt.xticks(x, labels, rotation='vertical');
这篇关于字符串linecolllection的五彩的线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!