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

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()

Related

Over lapping of timeseries on x-axis

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
load_file=pd.read_excel(r'E:\CCNC\CCNCCodes\Modulated
output\plot_oriented_ss_data.xlsx',header=0)
load_file.columns
s=load_file.loc[0:49,['Timeseries','ccn_0.1']] s
s1=s
s['Timeseries'] = s['Timeseries'].astype(str)
plt.plot(s1[0:49]['Timeseries'],s1[0:5762]['ccn_0.1'],color='b')
plt.grid()
plt.show()
Please tell me where do I exact need to make the change to avoid the overlapping of time series in x-axis.
Instead of converting your 'Timeseries' to str, you should convert them to datetime using:
s['Timeseries'] = pd.to_datetime(s['Timeseries'])

pandas_datareader cant print date on x axis

In following plot date is not showing on x axis what do to show date time stamp
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import pandas_datareader as web
start=datetime.datetime(2014,1,8)
end =datetime.datetime(2019,1,8)
Tesla=web.DataReader('TSLA',"iex",start,end)
Ford=web.DataReader('F',"iex",start,end)
Tesla["open"].plot(label="Tesla",title="opening Price",figsize=(16,8))
plt.legend()
plt.show()
Your dataframe index is created as dtype object or string, let's convert index to DatetimeIndex using pd.to_datetime:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import pandas_datareader as web
start=datetime.datetime(2014,1,8)
end =datetime.datetime(2019,1,8)
Tesla=web.DataReader('TSLA',"iex",start,end)
Ford=web.DataReader('F',"iex",start,end)
#Change Telsa index to datetime dtype
Tesla.index = pd.to_datetime(Tesla.index)
#Let's do Fords too
Ford.index = pd.to_datetime(Ford.index)
Tesla["open"].plot(label="Tesla",title="opening Price",figsize=(16,8))
plt.legend()
plt.show()
Output:

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()

Read time series csv file to plot with matplotlib

I'm trying to plot a time series from the csv file.
eg. datalog.csv contains:
19:06:17.188,12.2
19:06:22.360,3.72
19:06:27.348,72
19:06:32.482,72
19:06:37.515,74
19:06:47.660,72
tried some thing like below:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
time, impressions = np.loadtxt("datalog_new.csv", unpack=True,
converters={ 0: mdates.strptime2num('%H:%M:%S.%f')})
plt.plot_date(x=time, y=impressions)
plt.show()
but could not parse the time, mdates.strptime2num('%H:%M:%S.%f')
Any suggestions are greatly appreciated.
You have to use bytespdate2num function to read csv file (because you read the file in binary mode):
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.dates import bytespdate2num
time, impressions = np.loadtxt("datalog_new.csv",
unpack=True, delimiter=',', converters={0: bytespdate2num('%H:%M:%S.%f')})
plt.plot_date(x=time, y=impressions)
plt.show()

Plotting timestampt data from CSV using matplotlib

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)

Categories