Python Forum
How to customize each x-axis separately?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to customize each x-axis separately?
#1
Matplotlib is giving me fits. Here's the graphing portion of my code:

datetimes = pd.to_datetime(btstats['Date']) #need datetime column for btstats.Date to let plt do its thing with auto-scaling.
btstats['Date'] = datetimes

trade_dates_in_datetime = [datetime.strptime(i, "%Y-%m-%d") for i in trade_list]
marker_list = []

#Graph cumulative realized_pnl
fig, axs = plt.subplots(4,1) #no 0-indexing here
fig.set_size_inches(16, 12)
for i in range(len(btstats)):
    if btstats.loc[i,'Date'] in trade_dates_in_datetime:
        marker_list.append('d')
    else:
        marker_list.append('')
axs[0].plot(btstats['Date'],btstats['Cum.PnL'],marker='',color='black',linestyle='solid') #plots line only
for xp, yp, m in zip(btstats['Date'].tolist(), btstats['Cum.PnL'].tolist(), marker_list):
    axs[0].plot(xp,yp,m,color='orange',markersize=12) #plots markers only (or lack thereof)
axs[0].set_xlabel('Date', fontsize = 14) #in all cases, axs[0] rather than axs[0,0] since we specified above only one column.
axs[0].set_ylabel('Cumulative PnL', fontsize = 14)
plt.xticks(rotation = 0, fontsize = 14)
plt.yticks(fontsize = 14)
#plt.show() #now it'd be nice to get a heavy dot at the start of every new trade.

axs[1].plot(P_price_orig_all, marker='o', color='orange')
axs[1].set_xlabel('Trade #', fontsize = 16)
axs[1].set_ylabel('Original Pos. Price', fontsize = 14)
plt.xticks(np.arange(len(P_price_orig_all)), np.arange(1, len(P_price_orig_all)+1), fontsize = 14)  #********************
#plt.xticks(fontsize = 14)
plt.yticks(fontsize = 14)

axs[2].plot(P_price_orig_all, marker='o', color='orange')
axs[2].set_xlabel('Trade #', fontsize = 16)
axs[2].set_ylabel('Original Pos. Theta', fontsize = 14)
#plt.xticks(np.arange(len(P_theta_all)), np.arange(1, len(P_theta_all)+1), fontsize = 14)
plt.xticks(fontsize = 14)
plt.yticks(fontsize = 14)

axs[3].plot(P_theta_orig_price_all, marker='o', color='orange')
axs[3].set_xlabel('Trade #', fontsize = 16)
axs[3].set_ylabel('Orig. Theta / Pos. Price', fontsize = 14)
#plt.xticks(np.arange(len(P_theta_orig_price_all)), np.arange(1, len(P_theta_orig_price_all)+1), fontsize = 14)
plt.xticks(fontsize = 14)
plt.yticks(fontsize = 14)
plt.tight_layout()
plt.show()
I'm trying to do four graphs here. For the last three, the x-axis shows trade number. I currently have 55 trades and it defaults to major ticks with increments of 10. That is too much.

From an internet search, I was able to construct the highlighted (with asterisks) line. This only affects the x-axis on the fourth and final graph, though. Even if I include this line in the blocks for each of the last three graphs (two others are now commented out), it only affects the very bottom. Why is this? Does it have to do with setting up the graphs as subplots where one x-axis is used for all three? But then--I do get an x-axis for graphs 2-3 with increments of 10. And, the first graph with a datetime x-axis is just as I want.

Would it be better to avoid doing these graphs as subplots? Thanks!
Reply
#2
you need np.arange for y axis as well as x
example:
import numpy as np
import matplotlib.pyplot as plt

xdata = [0.0, 1.125, 4.678, 6.225, 12.777, 16.444] 
ydata = [0, 1, 2, 3, 4, 6]

plt.plot(xdata, ydata)

plt.xticks(np.arange(min(xdata), max(xdata)+1, 1.0))
plt.yticks(np.arange(min(ydata), max(ydata)+1.5, 1.5))

plt.show()
   
Reply
#3
(Mar-09-2022, 08:53 PM)Larz60+ Wrote: you need np.arange for y axis as well as x
example:
import numpy as np
import matplotlib.pyplot as plt

xdata = [0.0, 1.125, 4.678, 6.225, 12.777, 16.444] 
ydata = [0, 1, 2, 3, 4, 6]

plt.plot(xdata, ydata)

plt.xticks(np.arange(min(xdata), max(xdata)+1, 1.0))
plt.yticks(np.arange(min(ydata), max(ydata)+1.5, 1.5))

plt.show()

I'm getting graphs. the y-axes are scaled automatically, and they look fine. P_price_orig_all y-values looks good in axs[1]. P_theta_all (I had a typo) looks good in axs[2]. P_theta_orig_price_all looks good in axs[3]. Problem I'm having is with the x-axis. I don't know how to adjust the major tick label printed, and/or how to get the first x-value displayed to be 1 (even though it's the 0th item in the list per zero-indexing).
Reply
#4
It's difficult for me to see everything as I don't have enough to run your code:
don't have enough code, nor any data to run your code.
Mark17 likes this post
Reply
#5
(Mar-10-2022, 09:44 AM)Larz60+ Wrote: It's difficult for me to see everything as I don't have enough to run your code:
don't have enough code, nor any data to run your code.

Fair enough.

I actually have a number of unanswered questions about the workings of methods, functions, and attributes in mpl. I'm going to create a simplified example and try to "debug" them one by one to better understand it. If I have specific questions then I'll ask in another thread. Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  adding the customize dataset to BERT alicenguyen 2 1,060 Jul-06-2022, 08:06 AM
Last Post: Larz60+
  Sample labels from excel file in order to put them on x-axis and y-axis of a plot hobbyist 11 4,328 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  Customize Python Keywords for IDLE alanvers 0 1,997 Apr-03-2021, 10:56 AM
Last Post: alanvers
  Difference Between Figure Axis and Sub Plot Axis in MatplotLib JoeDainton123 2 2,467 Aug-21-2020, 10:17 PM
Last Post: JoeDainton123
  Is there a way i print odd and even numbers separately? spalisetty06 5 2,713 Jul-21-2020, 06:48 PM
Last Post: spalisetty06
  Customize spider chart swisha 2 2,063 Mar-17-2020, 01:15 AM
Last Post: swisha
  pytest-html report customize manoj 4 14,280 Nov-26-2019, 09:10 AM
Last Post: manojshetty

Forum Jump:

User Panel Messages

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