multicolored line with strings linecolllection

Alex T

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?

Alex T

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

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related