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

Skip to content

Commit 7044a87

Browse files
cubretodstansby
authored andcommitted
Made a function wrapper to two_scales.py
1 parent 9edcb03 commit 7044a87

File tree

1 file changed

+55
-18
lines changed

1 file changed

+55
-18
lines changed

examples/api/two_scales.py

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<<<<<<< HEAD
12
"""
23
===========================
34
Plots with different scales
@@ -13,28 +14,64 @@
1314
Such axes are generated by calling the `Axes.twinx` method. Likewise,
1415
`Axes.twiny` is available to generate axes that share a *y* axis but
1516
have different top and bottom scales.
16-
17-
The twinx and twiny methods are also exposed as pyplot functions.
18-
1917
"""
2018

2119
import numpy as np
2220
import matplotlib.pyplot as plt
2321

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
2564
t = np.arange(0.01, 10.0, 0.01)
2665
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

Comments
 (0)