|
| 1 | +<<<<<<< HEAD |
1 | 2 | """
|
2 | 3 | ===========================
|
3 | 4 | Plots with different scales
|
|
13 | 14 | Such axes are generated by calling the `Axes.twinx` method. Likewise,
|
14 | 15 | `Axes.twiny` is available to generate axes that share a *y* axis but
|
15 | 16 | have different top and bottom scales.
|
16 |
| -
|
17 |
| -The twinx and twiny methods are also exposed as pyplot functions. |
18 |
| -
|
19 | 17 | """
|
20 | 18 |
|
21 | 19 | import numpy as np
|
22 | 20 | import matplotlib.pyplot as plt
|
23 | 21 |
|
24 |
| -fig, ax1 = plt.subplots() |
| 22 | +def two_scales(ax1, ax2, time, data1, data2, param1_dic , param2_dic): |
| 23 | + """ |
| 24 | +
|
| 25 | + Parameters |
| 26 | + ---------- |
| 27 | + ax : (type of axis) |
| 28 | + A description of axis |
| 29 | +
|
| 30 | + data1: (first dataset) |
| 31 | + A description of data1 |
| 32 | +
|
| 33 | + data2 : (first dataset) |
| 34 | + A description of data2 |
| 35 | +
|
| 36 | + param_dic : This is a dictionary of the parameters of the style and color e.g. {line style: '-', text color = 'r'} |
| 37 | + Returns |
| 38 | + ------- |
| 39 | + Overlays |
| 40 | + data1 : (Plot first data set) |
| 41 | + data2 : (Plot second data set) |
| 42 | +
|
| 43 | + """ |
| 44 | + def color_y_axes(ax, color): |
| 45 | + """Color your axes.""" |
| 46 | + for t in ax.get_yticklabels(): |
| 47 | + t.set_color(color) |
| 48 | + return None |
| 49 | + |
| 50 | + |
| 51 | + ax1.plot(time, data1, param1_dic['color'] + param1_dic['style']) |
| 52 | + ax1.set_xlabel('time (s)') |
| 53 | + # Make the y-axis label and tick labels match the line color. |
| 54 | + ax1.set_ylabel('exp', color=param1_dic['color']) |
| 55 | + color_y_axes(ax1, param1_dic['color']) |
| 56 | + |
| 57 | + |
| 58 | + ax2.plot(time, data2, param2_dic['color'] + param2_dic['style']) |
| 59 | + ax2.set_ylabel('sin', color=param2_dic['color']) |
| 60 | + color_y_axes(ax2, param2_dic['color']) |
| 61 | + return plt.show() |
| 62 | + |
| 63 | +#Create some mock data |
25 | 64 | t = np.arange(0.01, 10.0, 0.01)
|
26 | 65 | s1 = np.exp(t)
|
27 |
| -ax1.plot(t, s1, 'b-') |
28 |
| -ax1.set_xlabel('time (s)') |
29 |
| -# Make the y-axis label, ticks and tick labels match the line color. |
30 |
| -ax1.set_ylabel('exp', color='b') |
31 |
| -ax1.tick_params('y', colors='b') |
32 |
| - |
33 |
| -ax2 = ax1.twinx() |
34 |
| -s2 = np.sin(2 * np.pi * t) |
35 |
| -ax2.plot(t, s2, 'r.') |
36 |
| -ax2.set_ylabel('sin', color='r') |
37 |
| -ax2.tick_params('y', colors='r') |
38 |
| - |
39 |
| -fig.tight_layout() |
40 |
| -plt.show() |
| 66 | +s2 = np.sin(2*np.pi*t) |
| 67 | + |
| 68 | +#Specify your parameter dictionary |
| 69 | +d1 = {'style': '-', 'color' : 'r'} |
| 70 | +d2 = {'style': '.', 'color' :'b'} |
| 71 | + |
| 72 | +#create your axes |
| 73 | +fig, ax = plt.subplots() |
| 74 | +ax2 = ax.twinx() |
| 75 | + |
| 76 | +#Call the function |
| 77 | +two_scales(ax, ax2, t, s1, s2, d1, d2) |
0 commit comments