Over lapping of timeseries on x-axis - python

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'])

Related

Need help changing x axis intervals on sns.lmplot

As you can see from the picture below, some of the data is cut off on the end. Does anyone know how to fix that? Also I want the intervals for weeks on the x axis to be (1,2,3...13) for weeks 1-13. Thanks.
Since you did not provide the data, I used an example below:
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(111)
df = pd.DataFrame({'Week':np.tile(np.arange(1,15),2),
'Score':np.random.uniform(np.repeat([0,1],14),
np.repeat([1,2],14),28),
'Win':np.repeat(['0','1'],14)
})
sns.lmplot returns a FacetGrid so you can set the axis ticks like this:
g = sns.lmplot(data=df,x='Week',y='Score',hue='Win')
g.set(xlim = (0.5,14.5))
g.set(xticks=range(14))

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:

Simple Graph Does Not Represent Data

This is a very straightforward question. I have and x axis of years and a y axis of numbers increasing linearly by 100. When plotting this with pandas and matplotlib I am given a graph that does not represent the data whatsoever. I need some help to figure this out because it is such a small amount of code:
The CSV is as follows:
A,B
2012,100
2013,200
2014,300
2015,400
2016,500
2017,600
2018,700
2012,800
2013,900
2014,1000
2015,1100
2016,1200
2017,1300
2018,1400
The Code:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
data = pd.read_csv("CSV/DSNY.csv")
data.set_index("A", inplace=True)
data.plot()
plt.show()
The graph this yields is:
It is clearly very inconsistent with the data - any suggestions?
The default behaviour of matplotlib/pandas is to draw a line between successive data points, and not to mark each data point with a symbol.
Fix: change data.plot() to data.plot(style='o'), or df.plot(marker='o', linewidth=0).
Result:
All you need is sort A before plotting.
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
data = pd.read_csv("CSV/DSNY.csv").reset_index()
data = data.sort_values('A')
data.set_index("A", inplace=True)
data.plot()
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()

Categories