Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 213403f

Browse files
committed
Cleanup and simplify two_scales example
1 parent c1eef4c commit 213403f

File tree

1 file changed

+43
-39
lines changed

1 file changed

+43
-39
lines changed

examples/api/two_scales.py

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,63 +14,67 @@
1414
`Axes.twiny` is available to generate axes that share a *y* axis but
1515
have different top and bottom scales.
1616
"""
17-
1817
import numpy as np
1918
import matplotlib.pyplot as plt
2019

21-
def two_scales(ax1, ax2, time, data1, data2, param1_dic , param2_dic):
20+
21+
def two_scales(ax1, time, data1, data2, c1, c2):
2222
"""
2323
2424
Parameters
2525
----------
26-
ax : (type of axis)
27-
A description of axis
26+
ax : axis
27+
Axis to put two scales on
2828
29-
data1: (first dataset)
30-
A description of data1
29+
time : array-like
30+
x-axis values for both datasets
3131
32-
data2 : (first dataset)
33-
A description of data2
32+
data1: array-like
33+
Data for left hand scale
3434
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
4140
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
4843
44+
Returns
45+
-------
46+
ax : axis
47+
Original axis
48+
ax2 : axis
49+
New twin axis
50+
"""
51+
ax2 = ax1.twinx()
4952

50-
ax1.plot(time, data1, param1_dic['color'] + param1_dic['style'])
53+
ax1.plot(time, data1, color=c1)
5154
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')
5556

57+
ax2.plot(time, data2, color=c2)
58+
ax2.set_ylabel('sin')
59+
return ax1, ax2
5660

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()
6161

62-
#Create some mock data
62+
# Create some mock data
6363
t = np.arange(0.01, 10.0, 0.01)
6464
s1 = np.exp(t)
65-
s2 = np.sin(2*np.pi*t)
65+
s2 = np.sin(2 * np.pi * t)
6666

67-
#Specify your parameter dictionary
68-
d1 = {'style': '-', 'color' : 'r'}
69-
d2 = {'style': '.', 'color' :'b'}
70-
71-
#create your axes
67+
# Create axes
7268
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

Comments
 (0)