0

I am trying to plot the graph bellow using python, but I am getting an error.

enter image description here

The Python commands I am using are:

import pandas as pd
import matplotlib.pyplot as plt


data = pd.read_csv('data/filtro_bovespa_final.csv')

data.loc[(data['codigo'] == 'BBAS3') & (data['codigo'] == 'BBDC4')]

data.date = pd.to_datetime(data['date'],format='%Y%m%d')
data.set_index(['date','codigo'])
plt.plot(data.date,data.preco)
plt.show()

The error I am getting is:

I got this graph, but it is not what I need:

enter image description here

The csv file I am using: Bovespa

I need a graph that allows me to compare the price linked with both the codes (BBAS3 and BBDC4) as the first graph I showed.

What else should I do to get the graph I need?

7
  • 1
    What are you trying to do with data.plot and data.show. Do you mean plt.show()? Commented Oct 14, 2020 at 3:45
  • Judging from the error message, it seems to be data.show, what happens if I comment out? Commented Oct 14, 2020 at 3:49
  • I corrected it... but I got another error... I have updated the question. Commented Oct 14, 2020 at 3:54
  • Do the graphs match the data you've presented? Commented Oct 14, 2020 at 4:04
  • Yes... the data I presented is on the graph of the figure I uploaded here. I n eed to plot this same figure using python... however, it is not working. Hope you can help me. Commented Oct 14, 2020 at 4:06

1 Answer 1

2

To draw them by attribute, we use a pivot to turn the data frames into columns by attribute. I've also changed the extraction condition to OR.

import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv('./Data/filtro_bovespa_final.csv')
data.date = pd.to_datetime(data['date'],format='%Y%m%d')

data = data.loc[(data['codigo'] == 'BBAS3') | (data['codigo'] == 'BBDC4')]
data.set_index('date', inplace=True)
data = data.pivot(columns='codigo')
data.columns = ['BBAS3','BBDC4']

data.plot()

plt.show()

enter image description here

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

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.