MCPcopy Index your code
hub / github.com/matplotlib/matplotlib / secondary_xaxis

Method secondary_xaxis

lib/matplotlib/axes/_axes.py:555–606  ·  view source on GitHub ↗

Add a second x-axis to this `~.axes.Axes`. This axis is typically used to display a second x-scale for the data plotted on the Axes. %(_secax_docstring)s Examples -------- The main axis shows frequency, and the secondary axis shows period.

(self, location, functions=None, *, transform=None, **kwargs)

Source from the content-addressed store, hash-verified

553
554 @_docstring.interpd
555 def secondary_xaxis(self, location, functions=None, *, transform=None, **kwargs):
556 """
557 Add a second x-axis to this `~.axes.Axes`.
558
559 This axis is typically used to display a second x-scale for the data
560 plotted on the Axes.
561
562 %(_secax_docstring)s
563
564 Examples
565 --------
566 The main axis shows frequency, and the secondary axis shows period.
567
568 .. plot::
569
570 fig, ax = plt.subplots()
571 ax.loglog(range(1, 360, 5), range(1, 360, 5))
572 ax.set_xlabel('frequency [Hz]')
573
574 def invert(x):
575 # 1/x with special treatment of x == 0
576 x = np.array(x).astype(float)
577 near_zero = np.isclose(x, 0)
578 x[near_zero] = np.inf
579 x[~near_zero] = 1 / x[~near_zero]
580 return x
581
582 # the inverse of 1/x is itself
583 secax = ax.secondary_xaxis('top', functions=(invert, invert))
584 secax.set_xlabel('Period [s]')
585 plt.show()
586
587 To add a secondary axis relative to your data, you can pass a transform
588 to the new axis.
589
590 .. plot::
591
592 fig, ax = plt.subplots()
593 ax.plot(range(0, 5), range(-1, 4))
594
595 # Pass 'ax.transData' as a transform to place the axis
596 # relative to your data at y=0
597 secax = ax.secondary_xaxis(0, transform=ax.transData)
598 """
599 if not (location in ['top', 'bottom'] or isinstance(location, Real)):
600 raise ValueError('secondary_xaxis location must be either '
601 'a float or "top"/"bottom"')
602
603 secondary_ax = SecondaryAxis(self, 'x', location, functions,
604 transform, **kwargs)
605 self.add_child_axes(secondary_ax)
606 return secondary_ax
607
608 @_docstring.interpd
609 def secondary_yaxis(self, location, functions=None, *, transform=None, **kwargs):

Callers 11

test_inset_and_secondaryFunction · 0.80
test_secondary_failFunction · 0.80
test_secondary_resizeFunction · 0.80
test_secondary_minorlocFunction · 0.80
test_secondary_formatterFunction · 0.80
test_secondary_reprFunction · 0.80
quick_start.pyFile · 0.80
secondary_axis.pyFile · 0.80

Calls 2

SecondaryAxisClass · 0.90
add_child_axesMethod · 0.80

Tested by 8

test_inset_and_secondaryFunction · 0.64
test_secondary_failFunction · 0.64
test_secondary_resizeFunction · 0.64
test_secondary_minorlocFunction · 0.64
test_secondary_formatterFunction · 0.64
test_secondary_reprFunction · 0.64