Skip to content

SecondaryAxis.grid() fails silently #18941

Description

@fohrloop

This code is copy-pasted from this SO question.

Bug report

Bug summary

Creating a secondary axis and calling the .grid() method on it does not draw a grid. If it should not draw grid, it should throw an Exception.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np


def get_data_and_ticks():

    # generate dummy load duration curve
    dur = 2500
    y = np.random.normal(60, 30, dur + 1)
    y[::-1].sort()

    x = range(0, dur + 1)
    perticks = np.linspace(0, 1, 11)
    xticks = perticks * dur
    yticks = np.interp(xticks, range(0, dur + 1), y)

    return x, y, xticks, yticks


x, y, xticks, yticks = get_data_and_ticks()

# create figure object with axe object
fig, ax1 = plt.subplots(figsize=(10, 7))
ax1.plot(x, y)

# create second axis
color = "tab:blue"

ax2 = ax1.secondary_yaxis("right")
ax2.set_yticks(yticks)
ax2.set_yticklabels(labels=[f"{i*10}%" for i in range(11)])
ax2.tick_params(axis="y", color=color, labelcolor=color)

# draw grid lines on the secondaryaxis
# The problem is at this line
ax2.grid(color=color, which="major", linestyle="--")

plt.show()

Actual outcome

image

Expected outcome

Alternative A
draw a figure like this:

image

Alternative B
Raise an exception and tell the user that SecondaryAxis.grid() is not supported.

Matplotlib version

  • Operating system: Windows 10
  • Matplotlib version: 3.3.3
  • Matplotlib backend (print(matplotlib.get_backend())): TkAgg
  • Python version: 3.8.6 64-bit

Same applies if a SecondaryAxis is created for x (ax1.secondary_xaxis("top"))

Metadata

Metadata

Assignees

No one assigned

    Labels

    keepItems to be ignored by the “Stale” Github Action

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions