1

I am trying to plot the following code where data1, data2, data3 are vectors.

data1 = np.array(means1) 
print('data1=',data1)

data2 = np.array(ci_l) 
print('data2',data2)

data3 = np.array(ci_h) 
print('data3',data3)

x = data1
y = np.concatenate([data2[:,None],data3[:,None]], axis=1)

print('x=', x,'y=',y)

plt.plot(x, [i for (i,j) in y], 'rs', markersize = 4)
plt.plot(x, [j for (i,j) in y], 'bo', markersize = 4)
plt.show()

For each x points as you see in the code I have two y points. When I run the code I obtain the following output:

data1= [[22.8]
 [31.6]
 [27.4]
 [30.4]
 [30.6]]
data2 [[21.80474319]
 [30.60474319]
 [26.40474319]
 [29.40474319]
 [29.60474319]]
data3 [[23.79525681]
 [32.59525681]
 [28.39525681]
 [31.39525681]
 [31.59525681]]
x= [[22.8]
 [31.6]
 [27.4]
 [30.4]
 [30.6]] y= [[[21.80474319]
  [23.79525681]]

 [[30.60474319]
  [32.59525681]]

 [[26.40474319]
  [28.39525681]]

 [[29.40474319]
  [31.39525681]]

 [[29.60474319]
  [31.59525681]]]

and this figure:

enter image description here

My question is how to plot a line that connect each y pair? My problem is similar to this:

< Matplotlib how to draw vertical line between two Y points >

I try to add the following line as suggested to the code:

plt.plot((x,x),([i for (i,j) in y], [j for (i,j) in y]),c='black')

but I obtain the following error:

Traceback (most recent call last):
  File "/Users/oltiana/Desktop/datamining/chapter4.py", line 151, in <module>
    plt.plot((x,x),([i for (i,j) in y], [j for (i,j) in y]),c='black')
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/pyplot.py", line 3019, in plot
    return gca().plot(
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/axes/_axes.py", line 1605, in plot
    lines = [*self._get_lines(*args, data=data, **kwargs)]
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/axes/_base.py", line 315, in __call__
    yield from self._plot_args(this, kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/axes/_base.py", line 504, in _plot_args
    raise ValueError(f"x and y can be no greater than 2D, but have "
ValueError: x and y can be no greater than 2D, but have shapes (2, 5, 1) and (2, 5, 1)

I try to solve the problem using shape and reshape bout still does not work. Any suggestion would be helpful for me. Thank you!

2 Answers2

0

try writing

for x1,y1y2 in zip(x,y):
    
    plt.plot([x1,x1],y1y2,'k-') #'k-' to prevent automatic coloring

BEFORE

plt.plot(x, [i for (i,j) in y], 'ro', markersize = 4)
plt.plot(x, [j for (i,j) in y], 'bs', markersize = 4)

This will make a two-point plot for each pair of points. This will work visually, but may mess up automatic legends

Michael Sohnen
  • 953
  • 7
  • 15
0

I notice that your arrays of data all look like two-dimensional lists - each number is the only element in a list of its own! ([[22.8], [31.6], ...] instead of [22.8, 31.6, ...])

That's why you're getting the shape error. There are a few ways to fix this, but one easy way is to call .flatten() on each array. This reduces it to be one-dimensional, and your code will work fine with data like that.

data1 = np.array(means1).flatten()
data2 = np.array(ci_l).flatten()
data3 = np.array(ci_h).flatten()

...

correct plot

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ben Gillett
  • 376
  • 1
  • 9