0

I have a dataFrame called Graph, I have attached my result graph below

    Date    Central_SMA  Bottom_Central_SMA Top_Central_SMA
0   2020-06-02  97.891667   97.7125         98.070833
1   2020-06-03  98.833333   98.9250         98.741667
2   2020-06-04  98.516667   98.4625         98.570833
3   2020-06-05  98.175000   98.0000         98.350000
4   2020-06-08  98.633333   98.5000         98.766667

Below code will calculate the graph but width of x axis ie not constant for perticular day. How can I make the spacing constant for a given day ?

Graph.reset_index(inplace=True)
Graph['Date'] = Graph['Date'].apply(date2num)
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(Graph.Date, Graph['Central_SMA'], label='Central SMA',ls='steps')
ax3.plot(Graph.Date, Graph['Top_Central_SMA'], label='Top SMA',ls='steps')

import matplotlib
matplotlib.rc('figure', figsize=[100,20])
plt.show()

enter image description here

2
  • 1
    With given data, it looks fine with this program. Could you elaborate on what is width of x axis is not constant for perticular day? Commented Aug 23, 2020 at 11:09
  • As you see in DataFrame there is missing data from 5th to 8th. Graph takes those data and projects it on missing dates. I dont want it to happen. It makes difficult for analysis Commented Aug 23, 2020 at 11:52

1 Answer 1

1

One approach is to set the date as index. Then, execute reindex() with all the dates from the first to the last. This will fill in 'NaN' ("not a number" or "not available") for the data corresponding to the missing dates. NaN values typically create an empty spot in a plotted data.

I'm not aware of a ls='steps' option in matplotlib's plot(), but there is a similar step() which creates step plots. (On top of matplotlib, pandas and seaborn also built some interfaces to create this and many other types of plots.)

By the way, when plotting multiple graphs on the same spot, it often works best when the same ax is reused. plt.subplots() is a handy way to create the figure and the subplots (default 1 row, 1 column) with one call. Many options can be set, among which the figsize.

import pandas as pd
from matplotlib import pyplot as plt
from matplotlib import dates as mdates
from io import StringIO

data_str = '''    Date    Central_SMA  Bottom_Central_SMA Top_Central_SMA
0   2020-06-02  97.891667   97.7125         98.070833
1   2020-06-03  98.833333   98.9250         98.741667
2   2020-06-04  98.516667   98.4625         98.570833
3   2020-06-05  98.175000   98.0000         98.350000
4   2020-06-08  98.633333   98.5000         98.766667'''
Graph = pd.read_csv(StringIO(data_str), delim_whitespace=True)
Graph['Date'] = pd.to_datetime(Graph['Date'])  # just making sure the 'Date' really is in pandas date format
Graph.set_index('Date', inplace=True)
Graph = Graph.reindex(index=pd.date_range(start=Graph.index[0], end=Graph.index[-1], freq='D'))

fig, ax = plt.subplots(figsize=(12, 5))

ax.step(Graph.index, Graph['Central_SMA'], label='Central SMA')
ax.step(Graph.index, Graph['Top_Central_SMA'], label='Top SMA')
ax.xaxis_date()
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
ax.legend()
plt.show()

resulting plot

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.