简体   繁体   中英

Python and Matplotlib: How to plot a set of line segments of which only extremes are known?

I have this problem that I've been trying to solve for a couple of days now, but it seems the more I try the farther I become from solution.

I have a code that does a number of things, and finally it produces two 19x2 arrays (both with dtype = float64 ), one for the coordinates of the left extremes of the segments, and one for the right extremes:

l_ext =  (19, 2)
[[0.3        2.18327756]
 [0.8        0.78647796]
 [1.3        1.0077252 ]
 [1.8        2.40453182]   
 [2.3        2.18328295]
 [2.8        0.7864866 ]
 [3.3        1.00775437]
 [3.8        2.40455773]
 [4.3        2.18328834]
 [4.8        0.78649524]
 [5.3        1.00778353]
 [5.8        2.40458364]
 [6.3        2.18329373]
 [6.8        0.78650388]
 [7.3        1.00781269]
 [7.8        2.40460955]
 [8.3        2.18329912]
 [8.8        0.78651252]
 [9.3        1.00784186]]

r_ext =  (19, 2)
[[0.7        1.00769604]
 [1.2        0.78646933]
 [1.7        2.18327218]
 [2.2        2.40448   ]
 [2.7        1.00766689]
 [3.2        0.78646069]
 [3.7        2.18326679]
 [4.2        2.40445409]
 [4.7        1.00763773]
 [5.2        0.78645206]
 [5.7        2.18326141]
 [6.2        2.40442819]
 [6.7        1.00760858]
 [7.2        0.78644343]
 [7.7        2.18325603]
 [8.2        2.40440228]
 [8.7        1.00757942]
 [9.2        0.7864348 ]
 [9.7        2.18325065]]

Finally I joined the arrays in a single one, obtaining:

bl_ext =  (2, 19, 2)
[[[0.3        2.18327756]
  [0.8        0.78647796]
  [1.3        1.0077252 ]
  [1.8        2.40453182]
  [2.3        2.18328295]
  [2.8        0.7864866 ]
  [3.3        1.00775437]
  [3.8        2.40455773]
  [4.3        2.18328834]
  [4.8        0.78649524]
  [5.3        1.00778353]
  [5.8        2.40458364]
  [6.3        2.18329373]
  [6.8        0.78650388]
  [7.3        1.00781269]
  [7.8        2.40460955]
  [8.3        2.18329912]
  [8.8        0.78651252]
  [9.3        1.00784186]]

 [[0.7        1.00769604]
  [1.2        0.78646933]
  [1.7        2.18327218]
  [2.2        2.40448   ]
  [2.7        1.00766689]
  [3.2        0.78646069]
  [3.7        2.18326679]
  [4.2        2.40445409]
  [4.7        1.00763773]
  [5.2        0.78645206]
  [5.7        2.18326141]
  [6.2        2.40442819]
  [6.7        1.00760858]
  [7.2        0.78644343]
  [7.7        2.18325603]
  [8.2        2.40440228]
  [8.7        1.00757942]
  [9.2        0.7864348 ]
  [9.7        2.18325065]]]

Then I tried to plot the segments using:

x = np.arange[0,10]
y = np.array(x+1)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(np.arange[0,10],)
blines = mc.LineCollection    
ax.add_collection(blines)
plt.show()

But what I get is an error message that I can't figure out:

TypeError: Cannot cast array data from dtype('<U2') to dtype('float64') according to the rule 'safe'

Can anyone help?

Using the data of your example, I would do the following:

import matplotlib.pyplot as plt

l_ext = [
[0.3,        2.18327756],
[0.8,        0.78647796],
[1.3,        1.0077252 ],
[1.8,        2.40453182],
[2.3,        2.18328295],
[2.8,        0.7864866 ],
[3.3,        1.00775437],
[3.8,        2.40455773],
[4.3,        2.18328834],
[4.8,        0.78649524],
[5.3,        1.00778353],
[5.8,        2.40458364],
[6.3,        2.18329373],
[6.8,        0.78650388],
[7.3,        1.00781269],
[7.8,        2.40460955],
[8.3,        2.18329912],
[8.8,        0.78651252],
[9.3,        1.00784186]]

r_ext = [
[0.7,        1.00769604],
[1.2,        0.78646933],
[1.7,        2.18327218],
[2.2,        2.40448   ],
[2.7,        1.00766689],
[3.2,        0.78646069],
[3.7,        2.18326679],
[4.2,        2.40445409],
[4.7,        1.00763773],
[5.2,        0.78645206],
[5.7,        2.18326141],
[6.2,        2.40442819],
[6.7,        1.00760858],
[7.2,        0.78644343],
[7.7,        2.18325603],
[8.2,        2.40440228],
[8.7,        1.00757942],
[9.2,        0.7864348 ],
[9.7,        2.18325065]]


for left, right in zip(l_ext, r_ext):
    print left, right
    plt.plot( [left[0],right[0]], [left[1],right[1]], 'k')
plt.show()

which produces the following plot:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM