vy315
vy315

Reputation: 1

how to convert string to float while plot the date with Python matplotlib

enter image description here

I want to draw the close price (y-axis) and date (x-axis) with python, but the error shows that I need to convert date from string to float.

Here is coding:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as dates
import datetime

from pandas import DataFrame, Series

df = pd.read_csv('C:/Users/Vicky/Desktop/pythontest/T1706dailyrecord.csv')

df.columns = [1,2,3,4,5]
print(df)

plt.plot(df[1], df[3])

Upvotes: 0

Views: 1085

Answers (1)

jezrael
jezrael

Reputation: 862841

I think you need parameter parse_dates for convert column to datetime in read_csv:

df = pd.read_csv('C:/Users/Vicky/Desktop/pythontest/T1706dailyrecord.csv', parse_dates=[0])

Or:

df=pd.read_csv('C:/Users/Vicky/Desktop/pythontest/T1706dailyrecord.csv',parse_dates=['Date'])

Also df.columns = [1,2,3,4,5] is not necessary, for select use: df['Date'] and df['Close']:

plt.plot(df['Date'], df['Close'])

Also is possible use DataFrame.plot:

df.plot(x='Date', y='Close')

Upvotes: 1

Related Questions