| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -40,6 +40,26 @@ def rad2deg(x): | |||
| 40 | 40 | secax.set_xlabel('angle [rad]') | |
| 41 | 41 | plt.show() | |
| 42 | 42 | ||
| 43 | + # %% | ||
| 44 | + # Normally, secondary axes are placed at either the 'top' or 'bottom' | ||
| 45 | + # of the plot. We can also pass a custom transform to the axis and | ||
| 46 | + # draw it relative to that transform. Here, we draw an axis at Y = 0 | ||
| 47 | + # in our data. | ||
| 48 | + | ||
| 49 | + fig, ax = plt.subplots(layout='constrained') | ||
| 50 | + x = np.arange(0, 10) | ||
| 51 | + np.random.seed(34) | ||
| 52 | + y = np.random.randn(len(x)) | ||
| 53 | + ax.plot(x, y) | ||
| 54 | + ax.set_xlabel('X') | ||
| 55 | + ax.set_ylabel('Y') | ||
| 56 | + ax.set_title('Random data') | ||
| 57 | + | ||
| 58 | + # Pass ax.transData as a transform to place the axis relative to our data | ||
| 59 | + secax = ax.secondary_xaxis(0, transform=ax.transData) | ||
| 60 | + secax.set_xlabel('Axis at Y = 0') | ||
| 61 | + plt.show() | ||
| 62 | + | ||
| 43 | 63 | # %% | |
| 44 | 64 | # Here is the case of converting from wavenumber to wavelength in a | |
| 45 | 65 | # log-log scale. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -581,6 +581,18 @@ def invert(x): | |||
| 581 | 581 | secax = ax.secondary_xaxis('top', functions=(invert, invert)) | |
| 582 | 582 | secax.set_xlabel('Period [s]') | |
| 583 | 583 | plt.show() | |
| 584 | + | ||
| 585 | + To add a secondary axis relative to your data, you can pass a transform | ||
| 586 | + to the new axis. | ||
| 587 | + | ||
| 588 | + .. plot:: | ||
| 589 | + | ||
| 590 | + fig, ax = plt.subplots() | ||
| 591 | + ax.plot(range(0, 5), range(-1, 4)) | ||
| 592 | + | ||
| 593 | + # Pass 'ax.transData' as a transform to place the axis | ||
| 594 | + # relative to your data at y=0 | ||
| 595 | + secax = ax.secondary_xaxis(0, transform=ax.transData) | ||
| 584 | 596 | """ | |
| 585 | 597 | if not (location in ['top', 'bottom'] or isinstance(location, Real)): | |
| 586 | 598 | raise ValueError('secondary_xaxis location must be either ' | |
| Back | FazBrowse Home | New Git URL |
0 commit comments