10000 DOC: Tiny fixes, and possible overhaul, of the two scales example in the gallery by afvincent · Pull Request #10320 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

DOC: Tiny fixes, and possible overhaul, of the two scales example in the gallery #10320

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

Merged
merged 5 commits into from
Jan 25, 2018
Merged
Changes from 3 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
78 changes: 20 additions & 58 deletions examples/api/two_scales.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,64 +17,26 @@
import numpy as np
import matplotlib.pyplot as plt


def two_scales(ax1, time, data1, data2, c1, c2):
"""

Parameters
----------
ax : axis
Axis to put two scales on

time : array-like
x-axis values for both datasets

data1: array-like
Data for left hand scale

data2 : array-like
Data for right hand scale

c1 : color
Color for line 1

c2 : color
Color for line 2

Returns
-------
ax : axis
Original axis
ax2 : axis
New twin axis
"""
ax2 = ax1.twinx()

ax1.plot(time, data1, color=c1)
ax1.set_xlabel('time (s)')
ax1.set_ylabel('exp')

ax2.plot(time, data2, color=c2)
ax2.set_ylabel('sin')
return ax1, ax2


# Create some mock data
t = np.arange(0.01, 10.0, 0.01)
s1 = np.exp(t)
s2 = np.sin(2 * np.pi * t)

# Create axes
fig, ax = plt.subplots()
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')
data1 = np.exp(t)
data2 = np.sin(2 * np.pi * t)

# Create twin axes and plot the mock data onto them
Copy link
Member

Choose a reason for hiding this comment

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

This comment is kind of "small" for its contents. That is, it describes the next two or three blocks, but is just a regular comment. I feel like it should be broken up to each block, or made a bit more distinct visually.

fig, ax1 = plt.subplots()
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis

for ax, data, c in ((ax1, data1, "red"), (ax2, data2, "blue")):
ax.plot(t, data, color=c)
# Color the y-axis (both label and tick labels)
ax.yaxis.label.set_color(c)
for tl in ax.get_yticklabels():
tl.set_color(c)

# Label both axes
ax1.set_xlabel('time (s)')
ax1.set_ylabel('exp')
ax2.set_ylabel('sin') # NB: we already took care of the x-label with ax1

fig.tight_layout() # otherwise the right y-label is slightly clipped
plt.show()
0