Database graph does not show python matplotlib

Asked

Viewed 856 times

1

The sketch of the graph appears, but empty , with no data.

import sqlite3
import matplotlib.pyplot as plt 
import matplotlib.dates as mdates


connection = sqlite3.connect('escola.db')
c = connection.cursor()

sql= "SELECT DATE_EXTRACTION, NOTAS FROM ESCOLA where turma_id='T1'"

x = []
y = []

conversor = mdates.strpdate2num('%m-%d-%Y %H:%M:%S') 


def read_data (wordUsed):
    for row in c.execute(sql):
        x.append(conversor(row[1]))
        y.append(row[2])

fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1, axisbg= 'white')
plt.ylim(0,50000)
plt.xlim(2014, 2015)
plt.plot_date(x, y, fmt='b-', label= 'values', linewidth=2)
plt.legend(['values'])
plt.grid(True)
plt.show()

Message:

136: MatplotlibDeprecationWarning: The axisbg attribute was deprecated in version 2.0. Use facecolor instead.
  warnings.warn(message, mplDeprecation, stacklevel=1)

2 answers

5


Your code has some potential problems:

  1. The data variables x and y are only filled within the function read_data, that is never called. So the data is empty and the graph shows nothing. Run the function before creating the graph!
  2. Even if the function was called, it manipulates local variables and not the variables x and y global. Declare them again within the function as global, or else (I think better) make the function return its values.
  3. Potentially the fact that you force the boundaries of the x and y axes can also cause no data to be displayed. When it happens like this, comment on the lines with plt.xlim and plt.ylim just to be sure.

Since you didn’t bother to make one Minimum, Complete and Verifiable Example that could be tested (I have no way to run your code, since I don’t have your database and I don’t even know what data is expected), I prepared a test that picks up the Yahoo quotes. The origin of the data is different, but the production of the graph is exactly the same. See the code:

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime

from matplotlib.finance import quotes_historical_yahoo_ochl
from matplotlib.dates import YearLocator, MonthLocator, DateFormatter

x = []
y = []

def read_data():

    # Apenas para teste
    # -------------------

    date1 = datetime.date(1995, 1, 1)
    date2 = datetime.date(2004, 4, 12)

    quotes = quotes_historical_yahoo_ochl('INTC', date1, date2)
    if len(quotes) == 0:
        raise SystemExit

    global x # CORREÇÃO: Declare x e y como globais!
    global y

    x = [q[0] for q in quotes]
    y = [q[1] for q in quotes]

read_data() # CORREÇÃO: Chame a função!!

fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1, axisbg= 'white')
#plt.ylim(0,50000) # DEPURAÇÃO: Comente para ver se isso também não contribui para o "problema"
#plt.xlim(2014, 2015)
plt.plot_date(x, y, fmt='b-', label= 'values', linewidth=2)
plt.legend(['values'])
plt.grid(True)
plt.show()

Upshot:

inserir a descrição da imagem aqui

  • I called the function and now it works, but I wanted to know the following: sql= "SELECT DATE_EXTRACTION, NOTES FROM SCHOOL Where turma_id='T1' NOTES=''C1"", I don’t know which way would be correct to select these two groups

  • When you say "groups" do you mean conditional clauses? It is not clear. If there are two clauses, perhaps one is missing and between them. Anyway, as this doubt is a new, different, question about the SQL clause assembly, you should open another question. It’s just that this site is not a forum.

  • Another thing: if any of the answers helped you, please consider mark one of them as accepted.

0

The error msg is very clear, the function axisbg was discontinued in version 2.0 and replaced by facecolor try to change that line:

ax1 = fig.add_subplot(1, 1, 1, axisbg= 'white')

for that

ax1 = fig.add_subplot(1, 1, 1, facecolor= 'white')
  • Thank you! I modified, but the error continued this was because I put trace between the dates and was bar , but thank you!

  • Despite the Warning need to even be considered, I think the problem is not that. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.