0

I want to:

  • A. See two y-axes (of two different graphs) with one x-axis.
  • B. See major gridlines for x-axis
  • c. See major and minor gridlines for y-axis of the primary plot

I can plot the graph perfectly with only one y-axis, BUT as soon as I uncomment "ax2 = ax1.twinx()", the minor gridlines of the primary axis disappear. Picture: The correct format with single plot, and the minor_grid-problem with two plots.

Thank you in advance!

def plot_graph(x, y1, label1, y2, label2, title):

fig, ax1 = plt.subplots()

# Plotting y-axis 1
ax1.set_xlabel('Time (s)')
ax1.set_ylabel(label1, color = "red")
ax1.grid(which='major',axis='both', color='black', linewidth=1)
ax1.grid(which='minor',axis='y',    color='gray',  linewidth=0.3)
ax1.tick_params(axis = 'y')
ax1.plot(x, y1, color = "red")

# Plotting secondary y-axis with the same x-axis 
ax2 = ax1.twinx()  # PROBLEM: this alone hides the ax1 minor grid
ax2.set_ylabel(label2, color = 'blue')   
ax2.plot(x,y2,color = 'blue')
ax2.tick_params(axis = 'y')

plt.minorticks_on()
plt.legend(loc='best')
plt.title(title)
plt.show()

return
Pheytr1x
  • 31
  • 3

2 Answers2

2

Problem solved.

  1. "plt.minorticks_on()" needs to be called before "ax2 = ax1.twinx()".
  2. "axis='both'" in ax1.grid() does not work. => call separately for x and y axes.

'''

plot_graph(x, y1, label1, y2, label2, title):

fig, ax1 = plt.subplots()

#Plotting y-axis 1
ax1.set_xlabel('Time (s)')
ax1.set_ylabel(label1, color="red")

ax1.grid(which='major',axis='x', color='black', linewidth=1)   # x major black
ax1.grid(which='minor',axis='x', color='gray', linewidth=0.3)  # x minor gray
ax1.grid(which='major',axis='y', color = 'k', linewidth=1)     # y major black
ax1.grid(which='minor',axis='y', color = 'gray',linewidth=0.3) # y minor gray (this was not showing) 

ax1.plot(x, y1, color = "red")

plt.minorticks_on() # NEW PLACE - SOLUTION

#Plotting secondary y-axis with the same x-axis 
ax2 = ax1.twinx() 
ax2.set_ylabel(label2, color = 'blue')   
ax2.plot(x,y2,color = 'blue')
ax2.tick_params(axis = 'y')

#plt.minorticks_on()   # OLD PLACE

plt.legend(loc='best')
plt.title(title)
plt.show(block=False)

return

''' Image: Correct output

Pheytr1x
  • 31
  • 3
0

The problem appears to be because of the axis='both' in the ax.grid lines. If you give it as both, it doesn't appear to like it and I am not sure why. I played around this appears to give what you need. Note that 3 of the grid lines are with ax1 and the other two are with ax2 Note:

  • I have some random numbers for x, y1, y2
  • I have given separate colors for X, y1/y2 major/minor lines so you can see. Use the colors as you need.

Code

x = []
y1 = []
y2 = []

for i in range(0,10):
    x.append(round(random.random()*160,2))
for i in range(0,10):
    y1.append(round(random.random()*3000,2))
for i in range(0,10):
    y2.append(round(random.random()*90,2))

fig, ax1 = plt.subplots()

# Plotting y-axis 1
ax1.set_xlabel('Time (s)')
ax1.set_ylabel('label1', color = "red")
ax1.grid(which='major', axis = 'x', color='g', linewidth=1)  #Green Vertical major
ax1.grid(which='major', axis = 'y', color='m', linewidth=1)  #Magenta Horizontal major
ax1.grid(which='minor', axis = 'x', color='y',  linewidth=0.3) #Yellow Vertical minor

ax1.tick_params(axis = 'y')
ax1.plot(x, y1, color = "red")

# Plotting secondary y-axis with the same x-axis 
ax2 = ax1.twinx()  # PROBLEM STILL?
ax2.set_ylabel('label2', color = 'blue')   
ax2.plot(x,y2,color = 'blue')
ax2.tick_params(axis = 'y')
ax2.grid(which='major', axis = 'y', color='k', linewidth=1)  #Black Horizontal major
ax2.grid(which='minor', axis = 'y', color='grey',  linewidth=0.3) #Grey Horizontal minor

plt.minorticks_on()
plt.legend(loc='best')
plt.title('title')
plt.show()

Graph

enter image description here

Redox
  • 9,321
  • 5
  • 9
  • 26
  • Still didn't show y-axis 1 minor grid, even though I added it there. BUT problem solved with your tip (don't use both) AND by moving plt.minorticks_on() to before twinx() – Pheytr1x Jun 21 '22 at 11:21
  • Good to hear your issue is resolved !! – Redox Jun 21 '22 at 13:44