multicolored line with strings linecolllection

痞子三分冷 提交于 2021-01-27 14:03:00

问题


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?


回答1:


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');



来源:https://stackoverflow.com/questions/42836100/multicolored-line-with-strings-linecolllection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!