0

Is there some way to measure the width of the grid lines in terms of the units used in the y axis?

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl

t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)

fig, ax = plt.subplots(constrained_layout=True)

ax.yaxis.set_tick_params(which='minor', width=5)
ax.plot(t, s)
ax.yaxis.set_major_locator(mpl.ticker.MultipleLocator(0.50))
ax.yaxis.set_minor_locator(mpl.ticker.MultipleLocator(0.25))
ax.grid(linewidth=5, axis='y', which='both')
ax.set_ylim(0, 2.25)

plt.show()

Screenshot

Fredrik P
  • 682
  • 1
  • 8
  • 21
  • To what end? What're you trying to accomplish? `linewidth` is a property of `matplotlib.lines.Line2D` and defined by points. – Trenton McKinney Sep 20 '21 at 13:59
  • Does this answer your question? [Matplotlib line width based on axis, not on points](https://stackoverflow.com/q/43495016/7758804) or [Expand the line with specified width in data unit](https://stackoverflow.com/q/19394505/7758804) – Trenton McKinney Sep 20 '21 at 14:06
  • @TrentonMcKinney I want to compute how much I would need to adjust `ylim` in [this situation](https://stackoverflow.com/questions/68423172/show-entire-minor-gridline-in-matplotlib-figure/68423437#68423437). At first glance, your suggestions does not seem to answer my question. – Fredrik P Sep 20 '21 at 17:23
  • 1
    What about [Python/Matplotlib : convert Axis <=> Data coordinates systems](https://stackoverflow.com/q/29107800/7758804) & https://matplotlib.org/2.0.2/users/transforms_tutorial.html or [Convert from data coordinates to axes coordinates in matplotlib](https://stackoverflow.com/q/62004022/7758804) – Trenton McKinney Sep 20 '21 at 17:26
  • [convert pixel-coordinates to data-coordinates in matplotlib](https://stackoverflow.com/q/59794014/7758804) – Trenton McKinney Sep 20 '21 at 17:27
  • This search seems promising: [matplotlib how to translate points on a graph site:stackoverflow.com](https://www.google.com/search?q=matplotlib+how+to+translate+points+on+a+graph+site:stackoverflow.com&sxsrf=AOaemvICmu00DrFpTjguKrwnyNpJqmTT2g:1632158665902&sa=X&ved=2ahUKEwjbx_3riI7zAhXzIDQIHemyC_8QrQIoBHoECAgQBQ&biw=1920&bih=975&dpr=1) – Trenton McKinney Sep 20 '21 at 17:28

1 Answers1

0

Thanks to Trenton McKinney, here is my stab at the problem. I had to change the line where I set the linewidth so that it used a variable lw as I haven't been able to figure out how to get the linewidth from the ax object.

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl

t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)

fig, ax = plt.subplots(constrained_layout=True)

ax.yaxis.set_tick_params(which='minor', width=5)
ax.plot(t, s)
ax.yaxis.set_major_locator(mpl.ticker.MultipleLocator(0.50))
ax.yaxis.set_minor_locator(mpl.ticker.MultipleLocator(0.25))
# ax.grid(linewidth=5, axis='y', which='both')
lw = 5
ax.grid(linewidth=lw, axis='y', which='both')
ax.set_ylim(0, 2.25)

def get_DaUPP(fig, ax):
    # Points per inch
    PPI = 72

    # Display units per inch
    figsizeDispUnits = np.diff(fig.transFigure.transform([(0, 0), (1, 1)]), 1, 0)

    DiUPI = np.divide(figsizeDispUnits, fig.get_size_inches())

    # Display units per data unit
    axDispUnits = np.diff(ax.transData.transform([(ax.get_xlim()[0], ax.get_ylim()[0]), (ax.get_xlim()[1], ax.get_xlim()[1])]), 1, 0)
    axDataUnits = np.diff([(ax.get_xlim()[0], ax.get_ylim()[0]), (ax.get_xlim()[1], ax.get_xlim()[1])], 1, 0)
    DiUPDaU = np.divide(axDispUnits, axDataUnits)

    # Data units per inch
    DaUPI = np.divide(DiUPI, DiUPDaU)

    # Data unit per point
    DaUPP = DaUPI / PPI

    return DaUPP.flatten()

# Width of grid in yaxis units
lw * get_DaUPP(fig, ax)[1]
Fredrik P
  • 682
  • 1
  • 8
  • 21