|
38 | 38 | safe_first_element) |
39 | 39 | from matplotlib.container import BarContainer, ErrorbarContainer, StemContainer |
40 | 40 | from matplotlib.axes._base import _AxesBase, _process_plot_format |
| 41 | +from matplotlib.axes._secondary_axes import Secondary_Axis |
41 | 42 |
|
42 | 43 | _log = logging.getLogger(__name__) |
43 | 44 |
|
@@ -639,6 +640,159 @@ def indicate_inset_zoom(self, inset_ax, **kwargs): |
639 | 640 |
|
640 | 641 | return rectpatch, connects |
641 | 642 |
|
| 643 | + def secondary_xaxis(self, location, *, conversion=None, |
| 644 | + otherargs=None, **kwargs): |
| 645 | + """ |
| 646 | + Add a second x-axis to this axes. |
| 647 | +
|
| 648 | + For example if we want to have a second scale for the data plotted on |
| 649 | + the xaxis. |
| 650 | +
|
| 651 | + Warnings |
| 652 | + -------- |
| 653 | +
|
| 654 | + This method is experimental as of 3.1, and the API may change. |
| 655 | +
|
| 656 | + Parameters |
| 657 | + ---------- |
| 658 | + location : string or scalar |
| 659 | + The position to put the secondary axis. Strings can be 'top' or |
| 660 | + 'bottom', scalar can be a float indicating the relative position |
| 661 | + on the axes to put the new axes (0 being the bottom, and 1.0 being |
| 662 | + the top.) |
| 663 | +
|
| 664 | + conversion : scalar, two-tuple of scalars, string, or Transform |
| 665 | + If a scalar or a two-tuple of scalar, the secondary axis is |
| 666 | + converted via a linear conversion with slope given by the first |
| 667 | + and offset given by the second. i.e. ``conversion = [2, 1]`` |
| 668 | + element for a parent axis between 0 and 1 gives a secondary axis |
| 669 | + between 1 and 3. |
| 670 | +
|
| 671 | + If a string, if can be one of "linear", "power", and "inverted". |
| 672 | + If "linear", the value of ``otherargs`` should be a float or |
| 673 | + two-tuple as above. If "inverted" the values in the secondary axis |
| 674 | + are inverted and multiplied by the value supplied by ``oterargs``. |
| 675 | + If "power", then the original values are transformed by |
| 676 | + ``newx = otherargs[1] * oldx ** otherargs[0]``. |
| 677 | +
|
| 678 | + Finally, the user can supply a subclass of `.transforms.Transform` |
| 679 | + to arbitrarily transform between the parent axes and the |
| 680 | + secondary axes. |
| 681 | + See :doc:`/gallery/subplots_axes_and_figures/secondary_axis.py` |
| 682 | + for an example of making such a transform. |
| 683 | +
|
| 684 | +
|
| 685 | + Other Parameters |
| 686 | + ---------------- |
| 687 | + **kwargs : `~matplotlib.axes.Axes` properties. |
| 688 | + Other miscellaneous axes parameters. |
| 689 | +
|
| 690 | + Returns |
| 691 | + ------- |
| 692 | + ax : axes._secondary_axes.Secondary_Axis |
| 693 | +
|
| 694 | + Examples |
| 695 | + -------- |
| 696 | +
|
| 697 | + Add a secondary axes that shows both wavelength for the main |
| 698 | + axes that shows wavenumber. |
| 699 | +
|
| 700 | + .. plot:: |
| 701 | +
|
| 702 | + fig, ax = plt.subplots() |
| 703 | + ax.loglog(range(1, 360, 5), range(1, 360, 5)) |
| 704 | + ax.set_xlabel('wavenumber [cpkm]') |
| 705 | + secax = ax.secondary_xaxis('top', conversion='inverted', |
| 706 | + otherargs=1.) |
| 707 | + secax.set_xlabel('wavelength [km]') |
| 708 | +
|
| 709 | +
|
| 710 | + """ |
| 711 | + if (location in ['top', 'bottom'] or isinstance(location, Number)): |
| 712 | + secondary_ax = Secondary_Axis(self, 'x', location, |
| 713 | + conversion, otherargs=otherargs, |
| 714 | + **kwargs) |
| 715 | + self.add_child_axes(secondary_ax) |
| 716 | + return secondary_ax |
| 717 | + else: |
| 718 | + raise ValueError('secondary_xaxis location must be either ' |
| 719 | + '"top" or "bottom"') |
| 720 | + |
| 721 | + def secondary_yaxis(self, location, *, conversion=None, |
| 722 | + otherargs=None, **kwargs): |
| 723 | + """ |
| 724 | + Add a second y-axis to this axes. |
| 725 | +
|
| 726 | + For example if we want to have a second scale for the data plotted on |
| 727 | + the xaxis. |
| 728 | +
|
| 729 | + Warnings |
| 730 | + -------- |
| 731 | +
|
| 732 | + This method is experimental as of 3.1, and the API may change. |
| 733 | +
|
| 734 | + Parameters |
| 735 | + ---------- |
| 736 | + location : string or scalar |
| 737 | + The position to put the secondary axis. Strings can be 'left' or |
| 738 | + 'right', scalar can be a float indicating the relative position |
| 739 | + on the axes to put the new axes (0 being the left, and 1.0 being |
| 740 | + the right.) |
| 741 | +
|
| 742 | + conversion : scalar, two-tuple of scalars, string, or Transform |
| 743 | + If a scalar or a two-tuple of scalar, the secondary axis is |
| 744 | + converted via a linear conversion with slope given by the first |
| 745 | + and offset given by the second. i.e. ``conversion = [2, 1]`` |
| 746 | + element for a parent axis between 0 and 1 gives a secondary axis |
| 747 | + between 1 and 3. |
| 748 | +
|
| 749 | + If a string, if can be one of "linear", "power", and "inverted". |
| 750 | + If "linear", the value of ``otherargs`` should be a float or |
| 751 | + two-tuple as above. If "inverted" the values in the secondary axis |
| 752 | + are inverted and multiplied by the value supplied by ``oterargs``. |
| 753 | + If "power", then the original values are transformed by |
| 754 | + ``newy = otherargs[1] * oldy ** otherargs[0]``. |
| 755 | +
|
| 756 | + Finally, the user can supply a subclass of `.transforms.Transform` |
| 757 | + to arbitrarily transform between the parent axes and the |
| 758 | + secondary axes. |
| 759 | + See :doc:`/gallery/subplots_axes_and_figures/secondary_axis.py` |
| 760 | + for an example of making such a transform. |
| 761 | +
|
| 762 | +
|
| 763 | + Other Parameters |
| 764 | + ---------------- |
| 765 | + **kwargs : `~matplotlib.axes.Axes` properties. |
| 766 | + Other miscellaneous axes parameters. |
| 767 | +
|
| 768 | + Returns |
| 769 | + ------- |
| 770 | + ax : axes._secondary_axes.Secondary_Axis |
| 771 | +
|
| 772 | + Examples |
| 773 | + -------- |
| 774 | +
|
| 775 | + Add a secondary axes that converts from radians to degrees |
| 776 | +
|
| 777 | + .. plot:: |
| 778 | +
|
| 779 | + fig, ax = plt.subplots() |
| 780 | + ax.plot(range(1, 360, 5), range(1, 360, 5)) |
| 781 | + ax.set_ylabel('degrees') |
| 782 | + secax = ax.secondary_yaxis('right', conversion=[np.pi / 180]) |
| 783 | + secax.set_ylabel('radians') |
| 784 | +
|
| 785 | + """ |
| 786 | + if location in ['left', 'right'] or isinstance(location, Number): |
| 787 | + secondary_ax = Secondary_Axis(self, 'y', location, |
| 788 | + conversion, otherargs=otherargs, |
| 789 | + **kwargs) |
| 790 | + self.add_child_axes(secondary_ax) |
| 791 | + return secondary_ax |
| 792 | + else: |
| 793 | + raise ValueError('secondary_yaxis location must be either ' |
| 794 | + '"left" or "right"') |
| 795 | + |
642 | 796 | def text(self, x, y, s, fontdict=None, withdash=False, **kwargs): |
643 | 797 | """ |
644 | 798 | Add text to the axes. |
|
0 commit comments