1 | initial version |
A self-answer (the idea is taken from here):
# Let's create two lists with some data
curve1 = []
for x in range(0,10):
curve1.append((x,n(sin(x))))
curve2 = []
for x in range(0,100):
curve2.append((x/10,n(10*sin(x/10+1))))
import matplotlib.pyplot as plt
fig = plt.figure()
# Create the first Y-axis on the left by default and
# plot the first curve associated to the left Y-axis
ax1 = fig.add_subplot(111)
curve1_x = zip(*curve1)[0]
curve1_y = zip(*curve1)[1]
ax1.plot(curve1_x, curve1_y, '.-', color='red')
# Paint the tick labels on the left Y-axis in red
# to match the color of a curve
for tl in ax1.get_yticklabels():
tl.set_color('red')
# Create the second Y-axis on the right and
# plot the first curve associated to the right Y-axis
ax2 = ax1.twinx()
curve2_x = zip(*curve2)[0]
curve2_y = zip(*curve2)[1]
ax2.plot(curve2_x, curve2_y, '.-', color='blue')
# Paint the tick labels on the left Y-axis in blue
# to match the color of a curve
for tl in ax2.get_yticklabels():
tl.set_color('blue')
# Create the vertical and horizontal grid lines
ax1.xaxis.grid(color='grey', linestyle='--', linewidth=0.5)
ax1.yaxis.grid(color='grey', linestyle='--', linewidth=0.5)
# Save the figure (to see it in Sage)
plt.savefig('figure.png')
2 | No.2 Revision |
A self-answer (the idea is taken from here):
# Let's create two lists with some data
curve1 = []
for x in range(0,10):
curve1.append((x,n(sin(x))))
curve2 = []
for x in range(0,100):
curve2.append((x/10,n(10*sin(x/10+1))))
import matplotlib.pyplot as plt
fig = plt.figure()
# Create the first Y-axis on the left by default and
# plot the first curve associated to the left Y-axis
ax1 = fig.add_subplot(111)
curve1_x = zip(*curve1)[0]
curve1_y = zip(*curve1)[1]
ax1.plot(curve1_x, curve1_y, '.-', color='red')
# Paint the tick labels on the left Y-axis in red
# to match the color of a curve
for tl in ax1.get_yticklabels():
tl.set_color('red')
# Create the second Y-axis on the right and
# plot the first second curve associated to the right Y-axis
ax2 = ax1.twinx()
curve2_x = zip(*curve2)[0]
curve2_y = zip(*curve2)[1]
ax2.plot(curve2_x, curve2_y, '.-', color='blue')
# Paint the tick labels on the left Y-axis in blue
# to match the color of a curve
for tl in ax2.get_yticklabels():
tl.set_color('blue')
# Create the vertical and horizontal grid lines
ax1.xaxis.grid(color='grey', linestyle='--', linewidth=0.5)
ax1.yaxis.grid(color='grey', linestyle='--', linewidth=0.5)
# Save the figure (to see it in Sage)
plt.savefig('figure.png')