From 7044a87398edfc51e9429401eb3fe8463f975381 Mon Sep 17 00:00:00 2001 From: Guillermo Breto Date: Sat, 27 Sep 2014 15:44:59 -0400 Subject: [PATCH 1/3] Made a function wrapper to two_scales.py --- examples/api/two_scales.py | 73 ++++++++++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 18 deletions(-) diff --git a/examples/api/two_scales.py b/examples/api/two_scales.py index 45b5fea722a6..728f528af413 100644 --- a/examples/api/two_scales.py +++ b/examples/api/two_scales.py @@ -1,3 +1,4 @@ +<<<<<<< HEAD """ =========================== Plots with different scales @@ -13,28 +14,64 @@ Such axes are generated by calling the `Axes.twinx` method. Likewise, `Axes.twiny` is available to generate axes that share a *y* axis but have different top and bottom scales. - -The twinx and twiny methods are also exposed as pyplot functions. - """ import numpy as np import matplotlib.pyplot as plt -fig, ax1 = plt.subplots() +def two_scales(ax1, ax2, time, data1, data2, param1_dic , param2_dic): + """ + + Parameters + ---------- + ax : (type of axis) + A description of axis + + data1: (first dataset) + A description of data1 + + data2 : (first dataset) + A description of data2 + + param_dic : This is a dictionary of the parameters of the style and color e.g. {line style: '-', text color = 'r'} + Returns + ------- + Overlays + data1 : (Plot first data set) + data2 : (Plot second data set) + + """ + def color_y_axes(ax, color): + """Color your axes.""" + for t in ax.get_yticklabels(): + t.set_color(color) + return None + + + ax1.plot(time, data1, param1_dic['color'] + param1_dic['style']) + ax1.set_xlabel('time (s)') + # Make the y-axis label and tick labels match the line color. + ax1.set_ylabel('exp', color=param1_dic['color']) + color_y_axes(ax1, param1_dic['color']) + + + ax2.plot(time, data2, param2_dic['color'] + param2_dic['style']) + ax2.set_ylabel('sin', color=param2_dic['color']) + color_y_axes(ax2, param2_dic['color']) + return plt.show() + +#Create some mock data t = np.arange(0.01, 10.0, 0.01) s1 = np.exp(t) -ax1.plot(t, s1, 'b-') -ax1.set_xlabel('time (s)') -# Make the y-axis label, ticks and tick labels match the line color. -ax1.set_ylabel('exp', color='b') -ax1.tick_params('y', colors='b') - -ax2 = ax1.twinx() -s2 = np.sin(2 * np.pi * t) -ax2.plot(t, s2, 'r.') -ax2.set_ylabel('sin', color='r') -ax2.tick_params('y', colors='r') - -fig.tight_layout() -plt.show() +s2 = np.sin(2*np.pi*t) + +#Specify your parameter dictionary +d1 = {'style': '-', 'color' : 'r'} +d2 = {'style': '.', 'color' :'b'} + +#create your axes +fig, ax = plt.subplots() +ax2 = ax.twinx() + +#Call the function +two_scales(ax, ax2, t, s1, s2, d1, d2) From c1eef4c88e23e80d3634103507418e4270764468 Mon Sep 17 00:00:00 2001 From: Guillermo Breto Date: Sat, 27 Sep 2014 16:30:32 -0400 Subject: [PATCH 2/3] cleaned the first line --- examples/api/two_scales.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/api/two_scales.py b/examples/api/two_scales.py index 728f528af413..d86fb19c4b88 100644 --- a/examples/api/two_scales.py +++ b/examples/api/two_scales.py @@ -1,4 +1,3 @@ -<<<<<<< HEAD """ =========================== Plots with different scales From 213403fd49b1fe2dde4db6e48bbde11b607dafd5 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Sat, 14 Jan 2017 21:45:12 +0000 Subject: [PATCH 3/3] Cleanup and simplify two_scales example --- examples/api/two_scales.py | 82 ++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 39 deletions(-) diff --git a/examples/api/two_scales.py b/examples/api/two_scales.py index d86fb19c4b88..ca5b4eddfbaa 100644 --- a/examples/api/two_scales.py +++ b/examples/api/two_scales.py @@ -14,63 +14,67 @@ `Axes.twiny` is available to generate axes that share a *y* axis but have different top and bottom scales. """ - import numpy as np import matplotlib.pyplot as plt -def two_scales(ax1, ax2, time, data1, data2, param1_dic , param2_dic): + +def two_scales(ax1, time, data1, data2, c1, c2): """ Parameters ---------- - ax : (type of axis) - A description of axis + ax : axis + Axis to put two scales on - data1: (first dataset) - A description of data1 + time : array-like + x-axis values for both datasets - data2 : (first dataset) - A description of data2 + data1: array-like + Data for left hand scale - param_dic : This is a dictionary of the parameters of the style and color e.g. {line style: '-', text color = 'r'} - Returns - ------- - Overlays - data1 : (Plot first data set) - data2 : (Plot second data set) + data2 : array-like + Data for right hand scale + + c1 : color + Color for line 1 - """ - def color_y_axes(ax, color): - """Color your axes.""" - for t in ax.get_yticklabels(): - t.set_color(color) - return None + c2 : color + Color for line 2 + Returns + ------- + ax : axis + Original axis + ax2 : axis + New twin axis + """ + ax2 = ax1.twinx() - ax1.plot(time, data1, param1_dic['color'] + param1_dic['style']) + ax1.plot(time, data1, color=c1) ax1.set_xlabel('time (s)') - # Make the y-axis label and tick labels match the line color. - ax1.set_ylabel('exp', color=param1_dic['color']) - color_y_axes(ax1, param1_dic['color']) + ax1.set_ylabel('exp') + ax2.plot(time, data2, color=c2) + ax2.set_ylabel('sin') + return ax1, ax2 - ax2.plot(time, data2, param2_dic['color'] + param2_dic['style']) - ax2.set_ylabel('sin', color=param2_dic['color']) - color_y_axes(ax2, param2_dic['color']) - return plt.show() -#Create some mock data +# Create some mock data t = np.arange(0.01, 10.0, 0.01) s1 = np.exp(t) -s2 = np.sin(2*np.pi*t) +s2 = np.sin(2 * np.pi * t) -#Specify your parameter dictionary -d1 = {'style': '-', 'color' : 'r'} -d2 = {'style': '.', 'color' :'b'} - -#create your axes +# Create axes fig, ax = plt.subplots() -ax2 = ax.twinx() - -#Call the function -two_scales(ax, ax2, t, s1, s2, d1, d2) +ax1, ax2 = two_scales(ax, t, s1, s2, 'r', 'b') + + +# Change color of each axis +def color_y_axis(ax, color): + """Color your axes.""" + for t in ax.get_yticklabels(): + t.set_color(color) + return None +color_y_axis(ax1, 'r') +color_y_axis(ax2, 'b') +plt.show()