serenade February 2016

Array element with a sequence

want to plot x_values_edited and speed. I have this error:

ValueError: setting an array element with a sequence

plt.plot(x_values_edited, speed, '.' )

My code and inputs are here:


x_values_Edited:

[datetime.datetime(2015, 1, 1, 0, 0), datetime.datetime(2015, 1, 1, 0, 1), datetime.datetime(2015, 1, 1, 0, 2), datetime.datetime(2015, 1, 1, 0, 3), datetime.datetime(2015, 1, 1, 0, 4), datetime.datetime(2015, 1, 1, 0, 5), datetime.datetime(2015, 1, 1, 0, 6), datetime.datetime(2015, 1, 1, 0, 7), datetime.datetime(2015,

speed:

[[58.5, 97.0, 56.0, 87.5, 95.0, 100.0, 104.0, 98.5, 101.0, 98.5, 86.5, 90.0, 97.5, 16.5, 103.0, 98.0, 48.0, 98.5, 93.5, 99.0, 101.5, 86.5, 108.5, 22.0, 92.5, 84.5, 103.5, 86.5, 103.0, 96.5, 91.5, 101.5, 86.0], [99.5, 101.0, 96.5, 94.5, 92.0, 89.5, 83.0, 94.0, 97.5, 103.5, 93.5, 101.0, 95.5, 89.0, 102.5, 89.0, 104.5, 102.0, 107.0, 96.5, 97.5, 97.5, 93.0, 95.0, 88.5, 89.0, 101.0, 97.0, 98.5, ]]

Answers


Pouria Hadjibagheri February 2016

Here is a working example. I had to generate the time since you haven't included the complete inputs in your question:

from matplotlib.pyplot import plot, show
from datetime import datetime, timedelta

def date_range(stop, start, step=1):
    """
    Time generator
    :param stop: Stop time
    :type stop: Start time
    :param start: Steps based upon which the time is to be calculated.
    :type start: datetime.datetime()
    :param step: datetime.datetime()
    :type step: int, float
    """
    delta_date = stop - start
    numerator = 0

    while numerator < delta_date.seconds:
        yield start + timedelta(0, numerator)
        numerator += step

speed = [
    58.5, 97.0, 56.0, 87.5, 95.0, 100.0, 104.0, 
    98.5, 101.0, 98.5, 86.5, 90.0, 97.5, 16.5, 
    103.0, 98.0, 48.0, 98.5, 93.5, 99.0, 101.5, 
    86.5, 108.5, 22.0, 92.5, 84.5, 103.5, 86.5, 
    103.0, 96.5, 91.5, 101.5, 86.0
]


speed_len = len(speed)

# Hypothetical start time
date_start = datetime(2014, 3, 15, 0)
# and finish time
date_end = datetime(2014, 3, 15, 0, int(speed_len/60), speed_len%60)

# Generate a time list: 1 second per item in |speed|
x_values_edited = [val for val in date_range(stop=date_end, start=date_start, step=1)]

# Scatter plot
plot(x_values_edited, speed, '.')

# Display the plot
show()

Shows this:

enter image description here


Pouria Hadjibagheri February 2016

Here is a quick fix.

from matplotlib.pyplot import scatter, show, xlim, ylim

from matplotlib.pyplot import scatter, show, xlim, ylim

for index, item in enumerate(speed):
    scatter([times[index]]*len(item), item, marker='.')

xlim(min(times), max(times))
ylim(min(min(speed)), max(max(speed)))
show()

Produces:

enter image description here

Sorry it took long. I had to do it in the underground!

Post Status

Asked in February 2016
Viewed 3,066 times
Voted 13
Answered 2 times

Search




Leave an answer