Trouble plotting tuple type datetime versus values from a database file

Viewed 28

From a database file I read datetime and values column and I'm trying to plot by using matplotlib and plt.plot(x, y) where x is datetime and y is values column but I get the error:

TypeError: unhashable type: 'numpy.ndarray'

Here below how my data looks like:

enter image description here

Both data are tuple type:

enter image description here

I searched for the error but didn't help for my case. I can plot plt.plot(y) without any problem but when I try plt.plot(x, y) I get the error. How can this be remedied?

edit:

x is created as follows:

def getAllRows():
    try:
        connection = sqlite3.connect('C:/Users/user/Documents/Python Scripts/file.db')
        cursor = connection.cursor()
        print("Connected to SQLite")

        sqlite_select_query = 'SELECT DateTime FROM Parameters_1_3_1'
        cursor.execute(sqlite_select_query)
        records = cursor.fetchall()
        print("Total rows are:  ", len(records))
        print("Printing each row")
        cursor.close()
        
        return records;

    except sqlite3.Error as error:
        print("Failed to read data from table", error)
    finally:
        if connection:
            connection.close()
            print("The Sqlite connection is closed")

x = getAllRows()

In console x outputs as:

enter image description here

1 Answers

Now with the edit I can see your problem. Pyplot should be able to work with numpy's arrays just as well as with built-in lists. However, what I learned today is that a single element tuple requires a comma, therefore my example was not using tuples at all. type((1)) returns int whilst type((1,)) returns tuple.
Long story short, you need to unpack your tuples.
x = [x_[0] for x_ in x] did the job for me.

Related