Skip to content

Made a function wrapper to examples/api/two_scales.py #3582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 73 additions & 31 deletions examples/api/two_scales.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,86 @@
#!/usr/bin/env python
"""

Demonstrate how to do two plots on the same axes with different left
right scales.
import numpy as np
import matplotlib.pyplot as plt


The trick is to use *2 different axes*. Turn the axes rectangular
frame off on the 2nd axes to keep it from obscuring the first.
Manually set the tick locs and labels as desired. You can use
separate matplotlib.ticker formatters and locators as desired since
the two axes are independent.

This is achieved in the following example by calling the Axes.twinx()
method, which performs this work. See the source of twinx() in
axes.py for an example of how to do it for different x scales. (Hint:
use the xaxis instance and call tick_bottom and tick_top in place of
tick_left and tick_right.)
def two_scales(ax1, ax2, time, data1, data2, param1_dic , param2_dic):
"""

The twinx and twiny methods are also exposed as pyplot functions.

"""
Demonstrate how to do two plots on the same axes with different left
right scales.


The trick is to use *2 different axes*. Turn the axes rectangular
frame off on the 2nd axes to keep it from obscuring the first.
Manually set the tick locs and labels as desired. You can use
separate matplotlib.ticker formatters and locators as desired since
the two axes are independent.

This is achieved in the following example by calling the Axes.twinx()
method, which performs this work. See the source of twinx() in
axes.py for an example of how to do it for different x scales. (Hint:
use the xaxis instance and call tick_bottom and tick_top in place of
tick_left and tick_right.)

The twinx and twiny methods are also exposed as pyplot functions.

import numpy as np
import matplotlib.pyplot as plt
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should return the artists (plt.show() returns None)


fig, ax1 = plt.subplots()
#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 and tick labels match the line color.
ax1.set_ylabel('exp', color='b')
for tl in ax1.get_yticklabels():
tl.set_color('b')
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plt.show() should go here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi thomas

How are you? I was wondering if you could come for the Spark meetup at Bloomberg? Would love to have you. Also remember pydata during strata.

Cheers Guillermo

On Sep 27, 2014, at 4:42 PM, Thomas A Caswell notifications@github.com wrote:

In examples/api/two_scales.py:

-# Make the y-axis label and tick labels match the line color.
-ax1.set_ylabel('exp', color='b')
-for tl in ax1.get_yticklabels():

  • tl.set_color('b')
    +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)
plt.show() should go here.


Reply to this email directly or view it on GitHub.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cubreto Can you send me details at tcaswell@gmail.com?





ax2 = ax1.twinx()
s2 = np.sin(2*np.pi*t)
ax2.plot(t, s2, 'r.')
ax2.set_ylabel('sin', color='r')
for tl in ax2.get_yticklabels():
tl.set_color('r')
plt.show()