1
import matplotlib.pyplot as plt

x = [2.4, 3.2, 4.435, 5.65, 6.4, 7.333, 8.5, 9.2345]
y = [1.4356, "", 5.32245, 6.542, 7.567, .77558, "", ""]

y1 = [] 
for string in (y):
    if (string != ""):
        y1.append(string)

plt.plot(x, y1, 'b.', label="Sample data")
plt.show()

When I try to plot this I get Value Error "x and y must have same first dimension, but have shapes (8,) and (5,)"

I want to skip over these empty elements and plot just the 5 points but I don't know how to go about it.

3
  • because you skip the " (quote) so you change the size of the array Commented Apr 8, 2021 at 12:40
  • When I do include the empty elements, it shows up as 0. I don't want to plot the empty values as 0. I'm wondering if there's a way of ignoring the corresponding y component in order to just plot the 5 points that have both x and y values. Commented Apr 8, 2021 at 12:44
  • try to put instead np.nan -> from numpy as np Commented Apr 8, 2021 at 12:46

2 Answers 2

2

The plot command wants the x and y dimension arrays you give it to be the same size. But since y1 is simply y with some elements removed, the y1 list will have fewer elements than the list of x coordinates. An easy workaround is to simply create also an x1 list, where you add the x coordinates matching the y1 coordinates, and plotting using x1 and y1.

Here's an example solution:

import matplotlib.pyplot as plt

x = [2.4, 3.2, 4.435, 5.65, 6.4, 7.333, 8.5, 9.2345]
y = [1.4356, "", 5.32245, 6.542, 7.567, .77558, "", ""]

x1 = []
y1 = [] 
for index in range(len(y)):
    if (y[index] != ""):
        y1.append(y[index])
        x1.append(x[index])

plt.plot(x1, y1, 'b.', label="Sample data")
plt.show()

Note that the loop was changed to loop by index, rather than the value of items in the list, since this lets us easily take the matching pairs of values from both lists.

Sign up to request clarification or add additional context in comments.

6 Comments

I'm new to coding and had difficulty with this. Tysm!
No prob, no one is born a programmer, wish you luck with your project :)
@AmnesiaSmith I think Otto's answer is good and solved your problem. You should now accept his answer by clicking on the green V on the left side.
@Antonin Personally I will admit I found the answer of Beny Gj more fitting from a data handling perspective, since his solution doesn't lose any data points, while still solving the problem at hand. Albeit I would've modified it to feed the NaN values back into the initial y list, rather than making a new list.
@OttoHanski I tried this method with more y-values of missing data but it showed a dimensional error. Is there some way to bypass this without suing pandas?
|
1

here you can use with numpy to put NAN instead of quote

#!/user/bin/env python
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np

x = [2.4, 3.2, 4.435, 5.65, 6.4, 7.333, 8.5, 9.2345]
y = [1.4356, "", 5.32245, 6.542, 7.567, .77558, "", ""]

y1 = []
for string in (y):
    if string != "":
        string = np.NAN
    y1.append(string)


if __name__ == '__main__':

    plt.plot(x, y1, 'b.', label="Sample data")
    plt.show()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.