Plotting timestampt data from CSV using matplotlib - python

I am trying to plot data from a csv file using matplotlib. There is 1 column against a timestamp:
26-08-2016 00:01 0.062964691
26-08-2016 00:11 0.047209214
26-08-2016 00:21 0.047237823
I have only been able to create a simple plot using only integers using the code below, which doesn't work when the y data is a timestamp. What do I need to add?
This may seem simple, but I am pressed for time :/
thanks!
from matplotlib import pyplot as plt
from matplotlib import style
import numpy as np
import datetime as dt
x,y = np.loadtxt('I112-1.csv',
unpack=True,
delimiter = ',')
plt.plot(x,y)
plt.title('Title')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()

Here's my example for this problem:
import pandas as pd
from io import StringIO
from datetime import datetime
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
data_file = StringIO("""
time,value
26-08-2016 00:01,0.062964691
26-08-2016 00:11,0.047209214
26-08-2016 00:21,0.047237823""")
df = pd.read_table(data_file,delimiter=",")
df['datetime']= df.time.map(lambda l: datetime.strptime(l, '%d-%m-%Y %H:%M'))
ax = df.set_index("datetime",drop=False)[['value','datetime']].plot(title="Title",yticks=df.value)

Related

DateFormatter is bringing 1970 as year not the original year in the dataset

I am trying to plot time series data. But x axis ticks are not coming the way it should. I wanted to out mont and year as x axis ticks. here is my code
from matplotlib.dates import DateFormatter
import matplotlib.dates as mdates
fig,ax = plt.subplots()
df_month.loc['2017', "Volume"].plot.bar(color='blue', ax=ax)
ax.set_ylabel("Volume")
ax.set_title("Volume")
date_form = DateFormatter("%y-%m")
ax.xaxis.set_major_formatter(date_form)
plt.xticks(rotation=45)
plt.show()
The output looks like this
What am I doing wrong? Please help.
My dataset looks like this:
Here is df_month data:
The following gives the right x-axis labels.
Import modules
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter
import matplotlib.dates as mdates
Example data
df_month = pd.DataFrame({'Date':['2006-01-03', '2006-02-04', '2006-02-08'], 'Volume':[24232729, 20553479, 20500000]}) # '2006-01-03', '2006-01-04'
df_month['Date'] = pd.to_datetime(df_month['Date'])
Plotting
fig,ax = plt.subplots()
ax.set_ylabel("Volume")
ax.set_title("Volume")
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))
ax.bar(df_month['Date'], df_month['Volume'])
plt.xticks(df_month['Date'], rotation=90)
plt.show()

candlestick plot from pandas dataframe, replace index by dates

This code gives plot of candlesticks with moving averages but the x-axis is in index, I need the x-axis in dates.
What changes are required?
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_finance import candlestick2_ohlc
#date format in data-> dd-mm-yyyy
nif = pd.read_csv('data.csv')
#nif['Date'] = pd.to_datetime(nif['Date'], format='%d-%m-%Y', utc=True)
mavg = nif['Close'].ewm(span=50).mean()
mavg1 = nif['Close'].ewm(span=13).mean()
fg, ax1 = plt.subplots()
cl = candlestick2_ohlc(ax=ax1,opens=nif['Open'],highs=nif['High'],lows=nif['Low'],closes=nif['Close'],width=0.4, colorup='#77d879', colordown='#db3f3f')
mavg.plot(ax=ax1,label='50_ema')
mavg1.plot(color='k',ax=ax1, label='13_ema')
plt.legend(loc=4)
plt.subplots_adjust(left=0.09, bottom=0.20, right=0.94, top=0.90, wspace=0.2, hspace=0)
plt.show()
Output:
I also had a lot of "fun" with this in the past... Here is one way of doing it using mdates:
import pandas as pd
import pandas_datareader.data as web
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib.finance import candlestick_ohlc
import matplotlib.dates as mdates
ticker = 'MCD'
start = dt.date(2014, 1, 1)
#Gathering the data
data = web.DataReader(ticker, 'yahoo', start)
#Calc moving average
data['MA10'] = data['Adj Close'].rolling(window=10).mean()
data['MA60'] = data['Adj Close'].rolling(window=60).mean()
data.reset_index(inplace=True)
data['Date']=mdates.date2num(data['Date'].astype(dt.date))
#Plot candlestick chart
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = fig.add_subplot(111)
ax3 = fig.add_subplot(111)
ax1.xaxis_date()
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
ax2.plot(data.Date, data['MA10'], label='MA_10')
ax3.plot(data.Date, data['MA60'], label='MA_60')
plt.ylabel("Price")
plt.title(ticker)
ax1.grid(True)
plt.legend(loc='best')
plt.xticks(rotation=45)
candlestick_ohlc(ax1, data.values, width=0.6, colorup='g', colordown='r')
plt.show()
Output:
Hope this helps.
Simple df:
Using plotly:
import plotly.figure_factory
fig = plotly.figure_factory.create_candlestick(df.open, df.high, df.low, df.close, dates=df.ts)
fig.show()
will automatically parse the ts column to be displayed correctly on x.
Clunky workaround here, derived from other post (if i can find again, will reference). Using a pandas df, plot by index and then reference xaxis tick labels to date strings for display. Am new to python / matplotlib, and this this solution is not so flexible, but it works basically. Also using a pd index for plotting removes the blank 'weekend' daily spaces on market price data.
Matplotlib xaxis index as dates
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_finance import candlestick2_ohlc
from mpl_finance import candlestick_ohlc
%matplotlib notebook # for Jupyter
# Format m/d/Y,Open,High,Low,Close,Adj Close,Volume
# csv data does not include NaN, or 'weekend' lines,
# only dates from which prices are recorded
DJIA = pd.read_csv('yourFILE.csv') #Format m/d/Y,Open,High,
Low,Close,Adj Close,Volume
print(DJIA.head())
fg, ax1 = plt.subplots()
cl =candlestick2_ohlc(ax=ax1,opens=DJIA['Open'],
highs=DJIA['High'],lows=DJIA['Low'],
closes=DJIA['Close'],width=0.4, colorup='#77d879',
colordown='#db3f3f')
ax1.set_xticks(np.arange(len(DJIA)))
ax1.set_xticklabels(DJIA['Date'], fontsize=6, rotation=-90)
plt.show()

get the date format on a Matplotlib plot's x-axis

I generate a plot using the following code:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
index=pd.date_range('2018-01-01',periods=200)
data=pd.Series(np.random.randn(200),index=index)
plt.figure()
plt.plot(data)
Which gives me a plot, looking as follows:
It looks like Matplotlib has decided to format the x-ticks as %Y-%m (source)
I am looking for a way to retrieve this date format. A function like ax.get_xtickformat(), which would then return %Y-%m. Which is the smartest way to do this?
There is no built-in way to obtain the date format used to label the axes. The reason is that this format is determined at drawtime and may even change as you zoom in or out of the plot.
However you may still determine the format yourself. This requires to draw the figure first, such that the ticklocations are fixed. Then you may query the formats used in the automatic formatting and select the one which would be chosen for the current view.
Note that the following assumes that an AutoDateFormatter or a formatter subclassing this is in use (which should be the case by default).
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
index=pd.date_range('2018-01-01',periods=200)
data=pd.Series(np.random.randn(200),index=index)
plt.figure()
plt.plot(data)
def get_fmt(axis):
axis.axes.figure.canvas.draw()
formatter = axis.get_major_formatter()
locator_unit_scale = float(formatter._locator._get_unit())
fmt = next((fmt for scale, fmt in sorted(formatter.scaled.items())
if scale >= locator_unit_scale),
formatter.defaultfmt)
return fmt
print(get_fmt(plt.gca().xaxis))
plt.show()
This prints %Y-%m.
If you want to edit the format of the date in myFmt = DateFormatter("%d-%m-%Y"):
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter
index=pd.date_range('2018-01-01',periods=200)
data=pd.Series(np.random.randn(200),index=index)
fig, ax = plt.subplots()
ax.plot(index, data)
myFmt = DateFormatter("%d-%m-%Y")
ax.xaxis.set_major_formatter(myFmt)
fig.autofmt_xdate()
plt.show()

How to display dates in matplotlib x-axis instead of sequence numbers

I am trying to develop a candlestick chart with matplotlib but for some reason, dates are not coming up in the x-axis. After searching in stackoverflow, I understood that the dates need to be converted to float numbers so i converted them as well but still it's not working. New to this python and matplotlib. ANy help would be greatly appreciated.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.finance import candlestick2_ohlc
import matplotlib.dates as dts
import matplotlib.ticker as mTicker
from datetime import datetime
my_file=pd.read_csv("C:\\path\\to\\file\\file.csv",sep=",",names=['Date','Open','High','Low','Close','AdjClose','Volume'],skiprows=1)
dateseries=[]
for i in my_file['Date']:
dateseries.append(dts.date2num(datetime.strptime(i,'%Y-%m-%d')))
print(dateseries)
fig,ax1=plt.subplots()
candlestick2_ohlc(ax1,my_file['Open'], my_file['High'],my_file['Low'], my_file['Close'], width=0.7,colorup='#008000', colordown='#FF0000')
plt.show()
Sample data:
Date,Open,High,Low,Close,Volume1,Volume2
2017-05-08,149.029999,153.699997,149.029999,153.009995,153.009995,48752400
2017-05-09,153.869995,154.880005,153.449997,153.990005,153.990005,39130400
2017-05-10,153.630005,153.940002,152.110001,153.259995,153.259995,25805700
In general, you are right about "the dates need to be converted to float numbers". Then to display dates on x-axis, you would need to "convert" them back. If you don't mind using candlestick_ohlc, that might be easier for setting the x-axis for your case here:
import io
import matplotlib.pyplot as plt
from matplotlib.finance import candlestick_ohlc
from matplotlib.dates import date2num, DayLocator, DateFormatter
import pandas as pd
s = """Date,Open,High,Low,Close,Volume1,Volume2
2017-05-08,149.029999,153.699997,149.029999,153.009995,153.009995,48752400
2017-05-09,153.869995,154.880005,153.449997,153.990005,153.990005,39130400
2017-05-10,153.630005,153.940002,152.110001,153.259995,153.259995,25805700"""
my_file = pd.read_table(io.StringIO(s), sep=',', header=0)
my_file['Date'] = date2num(pd.to_datetime(my_file['Date']).tolist())
fig, ax=plt.subplots()
candlestick_ohlc(ax, my_file.as_matrix())
ax.xaxis.set_major_locator(DayLocator())
ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
plt.show()

Load & Plot %Y-%m-%d %H:%M:%S from a file

I'm trying to make a simple XY plot using "plot_date" by loading a file with two dates and a value, but haven't yet had success. The idea is to have columns 0 and 1 to represent "time1", and columns 2 and 3 as "time2", and column 4 as "val".
Data input data looks like this:
2017-04-08 16:54:37 2017-04-08 16:55:08 1
2017-04-08 16:58:28 2017-04-08 16:58:33 1
2017-04-08 17:02:18 2017-04-08 17:02:24 1
Code
import matplotlib.pyplot as plt
from matplotlib.dates import strpdate2num
import numpy as np
import matplotlib.colors
import matplotlib.cm
from matplotlib.dates import date2num, DateFormatter
import datetime as dt
time1,time2,val = np.loadtxt(inputfile, usecols=(0,1,2,3,4), unpack=True, converters = {0,1: strpdate2num("%Y-%m-%d %H:%M:%S")})
Here I get an error:
>>> time1,time2,val = np.loadtxt(inputfile, usecols=(0,1,2,3,4), unpack=True, converters = {0,1: strpdate2num("%Y-%m-%d %H:%M:%S")})
File "<stdin>", line 1
time1,time2,val = np.loadtxt(inputfile, usecols=(0,1,2,3,4), unpack=True, converters = {0,1: strpdate2num("%Y-%m-%d %H:%M:%S")})
^
SyntaxError: invalid syntax
Any ideas on what I may be doing wrong? Suggestions on how to correct it?
Among other things, I'm hoping to subtract time2 from time1 and get the difference in seconds. Is this possible?
Finally, I'd like to plot the variables, something along the lines of:
fig, ax = plt.subplots()
ax.plot_date(time1, val, 'b-', color='b')
ax.plot_date(time2, val, 'b-', color='g')
You have to convert every column separately and then combine it:
import matplotlib.pyplot as plt
from matplotlib.dates import strpdate2num
import numpy as np
import matplotlib.colors
import matplotlib.cm
from matplotlib.dates import date2num, DateFormatter
import datetime as dt
time1,date1,time2,date2,val = np.loadtxt(inputfile, usecols=(0,1,2,3,4), unpack=True,
converters = {0: strpdate2num("%Y-%m-%d"), 1: strpdate2num("%H:%M:%S"),
2: strpdate2num("%Y-%m-%d"), 3: strpdate2num("%H:%M:%S")})
time1 += date1
time2 += date2
fig, ax = plt.subplots()
ax.plot_date(time1, val, 'b-', color='b')
ax.plot_date(time2, val, 'b-', color='g')
fig.autofmt_xdate()
plt.show()

Categories