sort graphing and fix value in python

Asked

Viewed 1,065 times

0

I’m trying to make a graph of the variation of the prices of the Itau stock however the result is with the values of disordered shares

insira o código aqui



  import matplotlib.pyplot as plt 

  dados = open("ITUB4.csv").readlines()

  x = []
  y = []


    for i in range(len(dados)):
       if i != 0:
          linha = dados[i].split(";")
         x.append(linha[0])
         x.sort()

         y.append(linha[1])
         y.sort()

    plt.plot(x, y)
    plt.show()

how can I make the values of 0-100 to be on the left side of the graph and the stock values on the line only

insira o código aqui
 Date;Close
  2019-02-25;36
  2019-02-26;36
  2019-02-27;35
  2019-02-28;35
  2019-03-01;34
  2019-03-06;34
  2019-03-07;35
  2019-03-08;35
  2019-03-11;36
  2019-03-12;37
  2019-03-13;37
  2019-03-14;36
  2019-03-15;36
  2019-03-18;36
  2019-03-19;36
  2019-03-20;35
  2019-03-21;34
  2019-03-22;33
  • If the goal is that they stay in the same order as the file, why are you ordering them with x.sort() and y.sort()?

1 answer

0


Hello, all right?

Note that you are sorting the list x and y every time you pass in the is, but it is not necessary to do this, because the data are already sorted by the file you presented above.

See this suggestion:

import csv
from datetime import datetime
import matplotlib.pyplot as plt

### Lendo o arquivo ###
with open("ITUB4.csv") as file:
    dados = csv.reader(file, delimiter=";")
    next(dados) # Pula a linha que contem o nome das colunas
    dados = list(dados) # Transforma em uma lista

### Percorrendo a lista dados ###
x = []
y = []
for d in dados:
    # Salvando os na lista x no formato datetime
    x.append(datetime.strptime(d[0], '%Y-%m-%d'))
    # Salvando os na lista y no formato float
    y.append(float(d[1]))


### Construindo o gráfico ###
plt.xlabel('Date')
plt.ylabel('Close')
plt.plot(x,y, 'r-o')
# Organiza o eixo x de modo a facilitar a visulização dos dados
plt.gcf().autofmt_xdate() 
plt.show()

The data Plot is as follows.

inserir a descrição da imagem aqui

Browser other questions tagged

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