|
14 | 14 | `Axes.twiny` is available to generate axes that share a *y* axis but
|
15 | 15 | have different top and bottom scales.
|
16 | 16 | """
|
17 |
| - |
18 | 17 | import numpy as np
|
19 | 18 | import matplotlib.pyplot as plt
|
20 | 19 |
|
21 |
| -def two_scales(ax1, ax2, time, data1, data2, param1_dic , param2_dic): |
| 20 | + |
| 21 | +def two_scales(ax1, time, data1, data2, c1, c2): |
22 | 22 | """
|
23 | 23 |
|
24 | 24 | Parameters
|
25 | 25 | ----------
|
26 |
| - ax : (type of axis) |
27 |
| - A description of axis |
| 26 | + ax : axis |
| 27 | + Axis to put two scales on |
28 | 28 |
|
29 |
| - data1: (first dataset) |
30 |
| - A description of data1 |
| 29 | + time : array-like |
| 30 | + x-axis values for both datasets |
31 | 31 |
|
32 |
| - data2 : (first dataset) |
33 |
| - A description of data2 |
| 32 | + data1: array-like |
| 33 | + Data for left hand scale |
34 | 34 |
|
35 |
| - param_dic : This is a dictionary of the parameters of the style and color e.g. {line style: '-', text color = 'r'} |
36 |
| - Returns |
37 |
| - ------- |
38 |
| - Overlays |
39 |
| - data1 : (Plot first data set) |
40 |
| - data2 : (Plot second data set) |
| 35 | + data2 : array-like |
| 36 | + Data for right hand scale |
| 37 | +
|
| 38 | + c1 : color |
| 39 | + Color for line 1 |
41 | 40 |
|
42 |
| - """ |
43 |
| - def color_y_axes(ax, color): |
44 |
| - """Color your axes.""" |
45 |
| - for t in ax.get_yticklabels(): |
46 |
| - t.set_color(color) |
47 |
| - return None |
| 41 | + c2 : color |
| 42 | + Color for line 2 |
48 | 43 |
|
| 44 | + Returns |
| 45 | + ------- |
| 46 | + ax : axis |
| 47 | + Original axis |
| 48 | + ax2 : axis |
| 49 | + New twin axis |
| 50 | + """ |
| 51 | + ax2 = ax1.twinx() |
49 | 52 |
|
50 |
| - ax1.plot(time, data1, param1_dic['color'] + param1_dic['style']) |
| 53 | + ax1.plot(time, data1, color=c1) |
51 | 54 | ax1.set_xlabel('time (s)')
|
52 |
| - # Make the y-axis label and tick labels match the line color. |
53 |
| - ax1.set_ylabel('exp', color=param1_dic['color']) |
54 |
| - color_y_axes(ax1, param1_dic['color']) |
| 55 | + ax1.set_ylabel('exp') |
55 | 56 |
|
| 57 | + ax2.plot(time, data2, color=c2) |
| 58 | + ax2.set_ylabel('sin') |
| 59 | + return ax1, ax2 |
56 | 60 |
|
57 |
| - ax2.plot(time, data2, param2_dic['color'] + param2_dic['style']) |
58 |
| - ax2.set_ylabel('sin', color=param2_dic['color']) |
59 |
| - color_y_axes(ax2, param2_dic['color']) |
60 |
| - return plt.show() |
61 | 61 |
|
62 |
| -#Create some mock data |
| 62 | +# Create some mock data |
63 | 63 | t = np.arange(0.01, 10.0, 0.01)
|
64 | 64 | s1 = np.exp(t)
|
65 |
| -s2 = np.sin(2*np.pi*t) |
| 65 | +s2 = np.sin(2 * np.pi * t) |
66 | 66 |
|
67 |
| -#Specify your parameter dictionary |
68 |
| -d1 = {'style': '-', 'color' : 'r'} |
69 |
| -d2 = {'style': '.', 'color' :'b'} |
70 |
| - |
71 |
| -#create your axes |
| 67 | +# Create axes |
72 | 68 | fig, ax = plt.subplots()
|
73 |
| -ax2 = ax.twinx() |
74 |
| - |
75 |
| -#Call the function |
76 |
| -two_scales(ax, ax2, t, s1, s2, d1, d2) |
| 69 | +ax1, ax2 = two_scales(ax, t, s1, s2, 'r', 'b') |
| 70 | + |
| 71 | + |
| 72 | +# Change color of each axis |
| 73 | +def color_y_axis(ax, color): |
| 74 | + """Color your axes.""" |
| 75 | + for t in ax.get_yticklabels(): |
| 76 | + t.set_color(color) |
| 77 | + return None |
| 78 | +color_y_axis(ax1, 'r') |
| 79 | +color_y_axis(ax2, 'b') |
| 80 | +plt.show() |
0 commit comments