Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to get rid of grid lines when plotting with Seaborn + Pandas with secondary_y?
When plotting with Pandas using secondary_y, grid lines are enabled by default. You can remove them by setting grid=False in the plot method.
Creating a DataFrame with Sample Data
First, let's create a DataFrame with two columns of data ?
import pandas as pd
import matplotlib.pyplot as plt
# Set figure parameters
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create sample data
data = pd.DataFrame({
"column1": [4, 6, 7, 1, 8],
"column2": [1, 5, 7, 8, 1]
})
print(data)
column1 column2 0 4 1 1 6 5 2 7 7 3 1 8 4 8 1
Plotting with Secondary Y-axis and No Grid
To remove grid lines when using secondary_y, set the grid parameter to False ?
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create sample data
data = pd.DataFrame({
"column1": [4, 6, 7, 1, 8],
"column2": [1, 5, 7, 8, 1]
})
# Plot with secondary y-axis and no grid
ax = data.plot(secondary_y=["column2"], grid=False)
plt.title("Plot without Grid Lines")
plt.show()
Alternative: Removing Grid After Plotting
You can also remove grid lines from both axes after creating the plot ?
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create sample data
data = pd.DataFrame({
"column1": [4, 6, 7, 1, 8],
"column2": [1, 5, 7, 8, 1]
})
# Plot with secondary y-axis
ax = data.plot(secondary_y=["column2"])
# Remove grid from both primary and secondary axes
ax.grid(False)
ax.right_ax.grid(False)
plt.title("Grid Removed After Plotting")
plt.show()
Key Parameters
| Parameter | Purpose | Usage |
|---|---|---|
secondary_y |
Specify columns for secondary y-axis | secondary_y=["column2"] |
grid |
Control grid visibility | grid=False |
ax.right_ax |
Access secondary y-axis | ax.right_ax.grid(False) |
Conclusion
Use grid=False in the plot method to remove grid lines when plotting with secondary y-axis. For more control, access both axes individually using ax.grid(False) and ax.right_ax.grid(False).
Advertisements
