Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with the x-axis
#1
I'm trying to plot five price subplots in a 2x3 grid:
import pandas as pd
import matplotlib.pyplot as plt
import datetime

rawdate_start = '2007-01-01' #input('Please enter start date as YYYY-MM-DD: ')
rawdate_end = '2019-01-01' #input('Please enter end date as YYYY-MM-DD: ')
markets = ['ES', 'GC', 'CL', 'EC', 'US']
plt.figure(facecolor='lightgrey')
subplot_count = 1
for market in markets:
    columnname_0 = market+'_Close'
    columnname_1 = market+'_Daily_Rtn'
    columnname_2 = market+'_Cum_Rtn'
    columnname_3 = market+'_Value'
    market = pd.read_csv(r'C:\Users\drkle\{}(daily).csv'.format(market), parse_dates=["Date"], index_col="Date")
    market_cut = market.drop(['Open','High','Low','Vol','OI'],axis=1)
    market_cut = market_cut[rawdate_start:rawdate_end]
    market_cut.columns = [columnname_0]
    plt.subplot(3,2,subplot_count)
    plt.plot(columnname_0)
    subplot_count += 1
plt.show()
I thought matplotlib was supposed to recognize this as a datetime index (from Line 15) and print dates accordingly (from Lines 5, 6, 17) so they are legible. I'm getting five empty subplots with x-axis tick values from -0.05 to +0.05 (increments of 0.25). Why no dates? Thanks!
Reply
#2
You were actually plotting plt.plot("ES_Close"), plt.plot("GC_close")..., which gives empty plot. The date information was never in the plot.
Reply
#3
We solved this a different way in another thread, but for educational purposes I wanted to try and understand.
import pandas as pd
import matplotlib.pyplot as plt
import datetime

rawdate_start = '2007-01-01' #input('Please enter start date as YYYY-MM-DD: ')
rawdate_end = '2019-01-01' #input('Please enter end date as YYYY-MM-DD: ')
markets = ['ES', 'GC', 'CL', 'EC', 'US']
#plt.figure(facecolor='lightgrey')
subplot_count = 1
plt.subplot(3,2,subplot_count) #if this line goes here, then I get one blank graph followed by five correct graphs plotted alone (not as subplots)
for market in markets:
    columnname_0 = market+'_Close'
    columnname_1 = market+'_Daily_Rtn'
    columnname_2 = market+'_Cum_Rtn'
    columnname_3 = market+'_Value'
    df_name = market
    market = pd.read_csv(r'C:\Users\drkle\{}(daily).csv'.format(market), parse_dates=["Date"], index_col="Date")
    market_cut = market.drop(['Open','High','Low','Vol','OI'],axis=1)
    market_cut = market_cut[rawdate_start:rawdate_end]
    market_cut.columns = [columnname_0]
#    if subplot_count > 1:
 #       plt.subplot(3,2,subplot_count) #if this line goes here, then I get five blank axes/graphs with US graph (last one) correct.
    market_cut.plot(y=columnname_0,title=df_name+' Stock Price')
    subplot_count += 1
plt.show()
I tried toggling between placing the above line as Line 10 or 22. If I left line 10 (deleted 22), then I got one blank graph followed by the five correct ones (none as subplots, though). If I deleted 10 and left 22, then I got five blank graphs followed by the correct last graph (none as subplots, though).

Can you explain this? Thanks!
Reply
#4
plt.subplot(3,2,subplot_count) shouldn't be at line 22, it should be at line 10. You got 5 graphs because there was 5 elements in markets variable. The first subplot was empty because you set subplot_count = 1 at line 9. If you set subplot_count = 0, then the last subplot would be empty.
Reply
#5
(Dec-22-2020, 09:31 PM)MK_CodingSpace Wrote: plt.subplot(3,2,subplot_count) shouldn't be at line 22, it should be at line 10. You got 5 graphs because there was 5 elements in markets variable. The first subplot was empty because you set subplot_count = 1 at line 9. If you set subplot_count = 0, then the last subplot would be empty.

Why did they come up as separate graphs instead of subplots on the same screen?
MK_CodingSpace likes this post
Reply
#6
When you do subplot, it should be something like this
fig, axs = plt.subplots(2)
axs[0].plot(x, y)
axs[1].plot(x, -y)
Reply
#7
(Dec-22-2020, 09:58 PM)MK_CodingSpace Wrote: When you do subplot, it should be something like this
fig, axs = plt.subplots(2)
axs[0].plot(x, y)
axs[1].plot(x, -y)

I was using a state-based approach here. Your response discusses an object-oriented approach, which we used successfully in the other thread.

Assuming I stuck with this state-based approach, though, I don't understand why they didn't print as subplots. If the line I tried at 10 or 22 stays at 10, then there's nothing in the for loop to direct which subplot should be drawn. If that line goes at 22, though, then shouldn't it direct which subplot to draw? It doesn't. Why?
Reply
#8
fig, ax = plt.subplots() does not really plot the data. It specifies the axes the subplots will be drawn, so it should be outside of the for loop here. It is the ax.plot(x, y) that actually plots the data. You might want to check out some matplotlib subplot demo to see how it works. Good luck!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Floor division problem with plotting x-axis tick labels Mark17 5 2,047 Apr-03-2022, 01:48 PM
Last Post: Mark17
  Sample labels from excel file in order to put them on x-axis and y-axis of a plot hobbyist 11 4,235 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  Difference Between Figure Axis and Sub Plot Axis in MatplotLib JoeDainton123 2 2,426 Aug-21-2020, 10:17 PM
Last Post: JoeDainton123

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020