How to show grid line for Y axis in matplotlib

Ge Yan :

I have a couple of plot scripts that using subplots to generate figure and axis[i].grid(axis = 'y') to show grid only for y-axis. Other works well, but I have no idea why this script doesn't show the grid line. Could someone help me?

import matplotlib.pyplot as plt
import numpy as np
nel   = 8
npts  = 10 
h     = 1./nel

x     = np.zeros((npts+1, nel))
x_c   = np.zeros(nel)
for i in range(0, nel):
    x[:,i] = np.linspace(i*h, (i+1)*h, npts+1)
    x_c[i] = (i+0.5) * h

print(x)
print(x_c)
# 3rd order DGD basis
fig, axs = plt.subplots(2, 1, sharex = True, figsize = (6,10), facecolor = 'w')
plt.tight_layout()
fig.subplots_adjust(hspace = 0)

y = np.zeros((npts+1, nel)) # for 0th element
y[:,0] = (x[:,0] - x_c[1]) * (x[:,0] - x_c[2]) * (x[:,0] - x_c[3])/(-6*h**3)
y[:,1] = (x[:,1] - x_c[1]) * (x[:,1] - x_c[2]) * (x[:,1] - x_c[3])/(-6*h**3)
for j in range(0,nel):
    axs[0].plot(x[:,j], y[:,j], 'r-', linewidth=2)
    axs[0].set_xticks(np.arange(0, 1.01, h))
    axs[0].set_yticks(np.arange(0, 1.2, 1.0))
    axs[0].set_ylim(-0.5, 1.5)
    axs[0].grid(axis = 'y')

y = np.zeros((npts+1, nel)) # for 1st element
y[:,0] = (x[:,0] - x_c[0]) * (x[:,0] - x_c[2]) * (x[:,0] - x_c[3])/(2*h**3)
y[:,1] = (x[:,1] - x_c[0]) * (x[:,1] - x_c[2]) * (x[:,1] - x_c[3])/(2*h**3)
y[:,2] = (x[:,2] - x_c[2]) * (x[:,2] - x_c[3]) * (x[:,2] - x_c[4])/(-6*h**3)
for j in range(0,nel):
    axs[1].plot(x[:,j], y[:,j],'r-', linewidth=2)
    axs[1].set_xticks(np.arange(0, 1.01, h))
    axs[1].set_yticks(np.arange(0, 1.2, 1.0))
    axs[1].set_ylim(-0.5, 1.5)
    axs[1].grid(axis = 'y')

plt.show()
Anis R. :

Setting the axs[0].grid(axis = 'y') and other axis settings outside the for loop seems to make it work, just tested it in a Jupyter notebook, e.g.:

for j in range(0,nel):
    axs[0].plot(x[:,j], y[:,j], 'r-', linewidth=2)
    axs[0].set_xticks(np.arange(0, 1.01, h))
    
axs[0].set_yticks(np.arange(0, 1.2, 1.0))
axs[0].set_ylim(-0.5, 1.5)
axs[0].grid(axis = 'y')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to highlight single y-axis grid line

How to show all Y-Axis Labels in Matplotlib in TimeLine Chart?

How to make stacked line chart with different y-axis in matplotlib?

Add a label to y-axis to show the value of y for a horizontal line in matplotlib

How to draw a y-axis and x-axis line in the center of a 4x4 grid?

show origin axis (x,y) in matplotlib plot

How to fix y axis on matplotlib?

How can I color one Y-axis line different from the other grid-lines?

How to coustomise the Y-axis grid line css properties in c3js

How to hide/show an axes, not axis in matplotlib

How to show date and time on x axis in matplotlib

Matplotlib: keep grid lines behind the graph but the y and x axis above

Python Matplotlib - How to annotate y-axis values only on line plot?

How to remove axis ticks in matplotlib line chart?

Highchart show grid lines on y-axis for every point

Pandas/matplotlib line plot does not show x axis text labels

Matplotlib how to plot in two y axis

matplotlib subplots: how to freeze x and y axis?

How to format large value on Y axis in Matplotlib?

Python Matplotlib - how to specify values on y axis?

How to set y-axis to Thousands in matplotlib

How to change y-axis increments in Matplotlib

How to set maximum y axis limit in matplotlib

How to autoscale y-axis for bargraph in matplotlib?

How to align y and x axis using matplotlib

How to update y-axis in matplotlib

Python - Matplotlib: normalizing y-axis to show multiples of standard deviation

Add x=y (45 degree) line within matplotlib axis limits

Is there a way to mark the min and max of a line chart on its y axis in matplotlib?