Reputation: 11
I have a dataframe that shows my total_Sales, Total_cost & Total_profit over a period of time. My graph only plots the dates on which changes occur in my dataframe. I'm wondering if I can change that to show monthly data from the first transaction to today.
Date Ticker Total_Sales Total_Cost Profit
01/11/22 CELH 2000.00 0.0 2000.00
07/20/22 MMMB 2040.00 0.0 2040.00
07/28/22 MMMB 2080.00 0.0 2080.00
08/26/22 MMMB 2120.00 21.0 2099.00
09/13/22 AAPL 3920.00 721.0 3199.00
09/13/22 AAPL 5120.00 721.0 4399.00
09/27/22 AAPL 7820.00 721.0 7099.00
10/20/22 MMMB 7860.00 721.0 7139.00
11/01/22 MMMB 7900.00 721.0 7179.00
11/09/22 AAPL 8540.00 833.0 7707.00
11/14/22 AAPL 8540.07 833.0 7707.07
11/14/22 AAPL 8540.14 833.0 7707.14
11/14/22 AAPL 8540.21 833.0 7707.21
11/14/22 CELH 18540.21 1283.0 17257.21
11/15/22 AAPL 18620.21 1483.0 17137.21
11/16/22 AAPL 18920.21 2303.0 16617.21
*For clarification: The Total_Sales, Total_Cost & Profit are cumulative. So it's the total sales, cost & profit of a portfolio not of an individual stock transaction.
The graph that I'm getting from plotting this data by using the following code:
plt2.plot(x = 'Date', y=['Total_Sales', 'Total_Cost', 'Profit'], kind='line', linewidth=4) plt.show()
Where plt2 is my pd.DataFrame that holds the data shown in the table above.
Although this graph does display what I want it to display (the data plotted against dates), I would like it to for example start by showing the first of the year (01/01/22) and then the end will be today's date (or the end of the year 12/31/22) and then with an interval of showing every month or every other month and not the dates that transactions are made.
Is there a way to do this by using the matplotlib? Or do I need to change my DataFrame to include every first of the month and the corresponding sales/cost/profit? Is there an easy way for me to do this?
I tried to do this:
#plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y')) #plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=30))
Which gave me this graph which has the wrong dates on it:
Upvotes: 0
Views: 111
Reputation: 11
I think I was able to fix it by adding the following lines of code:
plt2['Date'] = pd.to_datetime(plt2['Date'])
Which resulted in the dataframe handling the date column as an actual date.
Then for the graph I added this code:
months = mdates.MonthLocator()
fig = plt.figure(figsize=(16,4))
ax = fig.add_subplot(111)
ax.plot(plt2['Date'], plt2['Total_Sales'], linewidth = 4)
ax.plot(plt2['Date'], plt2['Total_Cost'], linewidth = 4)
ax.plot(plt2['Date'], plt2['Profit'], linewidth = 4)
ax.set_xlabel('Date')
ax.set_ylabel('Price')
ax.xaxis.set_minor_locator(months)
Which resulted in the following graph:
Which I will still need to style, but these additions to my code basically turned my date column into readable date code. And so by creating a figure, the graph displayed the dates nicely which solved my issue.
Upvotes: 0