1

I have some y values in a string like 1124,2113,4563. Then I have extracted individual values from the strings like,

1124=> 1,1,2,4
2113=> 2,1,1,3
4563=> 4,5,6,3

These are all my y values and corresponding x values are 5 increments for each y like,

1124 =>(0,1),(5,1),(10,2),(15,4)
2113 =>(0,2),(5,1),(10,1),(15,3)
4563 =>(0,4),(5,5),(10,6),(15,3)

My target is to draw 3 different series of lines from the given values. I have 2000 strings and converted them to a list like following

list = [(0,1),(5,1),(10,2),(15,4),(0,2),(5,1),(10,1),(15,3),(0,4),(5,5),(10,6),(15,3)]

Now is it possible to draw the lines from this data or what do? expert advise needed. I have tried drawing with the following code

plt.scatter(*zip(*list))
plt.plot(*zip(*list))

this code continues to connect the last point to the next point thus I am not getting different lines. all lines are getting interconnected. Here is an image what I am getting

Saef Ullah
  • 13
  • 1
  • 4
  • If you don't put everything in the same list, you will at least be able to plot several `plot`s, one for each series. If you have many of such series, using a `LineCollection` [is the most effective](https://stackoverflow.com/a/54544965/4124317) way of plotting. – ImportanceOfBeingErnest Mar 01 '19 at 01:11
  • Thank you for for your help, but I could not get linecollection to my work. – Saef Ullah Mar 01 '19 at 02:04

2 Answers2

2

If you have many of such series, using a LineCollection is the most effective way of plotting many lines.

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

strings = ["1124", "2113", "4563"]
y = np.array([list(map(int,list(s))) for s in strings])
x = np.tile(np.arange(y.shape[1])*5, y.shape[0]).reshape(y.shape)
segs = np.stack((x,y), axis=-1)


fig, ax = plt.subplots()
ax.scatter(x.flatten(), y.flatten())
ax.add_collection(LineCollection(segs))
ax.autoscale()
plt.show()

enter image description here

Or, to have the lines in different colors,

fig, ax = plt.subplots()
ax.scatter(x.flatten(), y.flatten(), 
           c=np.repeat(np.arange(len(segs)),segs.shape[1]), cmap="plasma")
lc = LineCollection(segs, cmap="plasma")
lc.set_array(np.arange(len(segs)))
ax.add_collection(lc)
ax.autoscale()
plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Thanks a lot, it worked. Can you help me a little bit for drawing each line with different color and the output is much dense, need a more disperse out put. – Saef Ullah Mar 01 '19 at 03:53
  • Updated the answer concerning colors. As for the density of lines, I'm not sure what you mean. That is essentially given by the data, so you cannot change it. – ImportanceOfBeingErnest Mar 01 '19 at 12:17
0

Maybe be you need to split the whole list into different sub-list.

k=0;
for i in range(0,len(list),4):
    name='sub_list_'+str(k)
    sub_list=list[i:i+4]
    plt.scatter(*zip(*sub_list))
    plt.plot(*zip(*sub_list),label=name)
    plt.legend()
    k+=1
blueear
  • 273
  • 2
  • 4