Skip to content

Commit

Permalink
ENH: marking right y-axis columns
Browse files Browse the repository at this point in the history
  • Loading branch information
changhiskhan committed Jul 18, 2012
1 parent 7b189c1 commit ce8fd02
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
25 changes: 25 additions & 0 deletions doc/source/visualization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,31 @@ To plot data on a secondary y-axis, use the ``secondary_y`` keyword:
df.B.plot(secondary_y=True, style='g')
Selective Plotting on Secondary Y-axis
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To plot some columns in a DataFrame, give the column names to the `secondary_y`
keyword:

.. ipython:: python
plt.figure()
@savefig frame_plot_secondary_y.png width=4.5in
df.plot(secondary_y=['A', 'B'])
Note that the columns plotted on the secondary y-axis is automatically marked
with "(right)" in the legend. To turn off the automatic marking, use the
`mark_right=False` keyword:

.. ipython:: python
plt.figure()
@savefig frame_plot_secondary_y.png width=4.5in
df.plot(secondary_y=['A', 'B'], mark_right=False)
Targeting different subplots
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
17 changes: 14 additions & 3 deletions pandas/tools/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@ def _post_plot_logic(self):
class LinePlot(MPLPlot):

def __init__(self, data, **kwargs):
self.mark_right = kwargs.pop('mark_right', True)
MPLPlot.__init__(self, data, **kwargs)

def _index_freq(self):
Expand Down Expand Up @@ -884,7 +885,10 @@ def _make_plot(self):

newline = plotf(ax, x, y, style, label=label, **kwds)[0]
lines.append(newline)
labels.append(label)
leg_label = label
if self.mark_right and self.on_right(i):
leg_label += ' (right)'
labels.append(leg_label)
ax.grid(self.grid)

self._make_legend(lines, labels)
Expand All @@ -900,6 +904,11 @@ def _make_ts_plot(self, data, **kwargs):
lines = []
labels = []

def to_leg_label(label, i):
if self.mark_right and self.on_right(i):
return label + ' (right)'
return label

if isinstance(data, Series):
ax = self._get_ax(0) #self.axes[0]
style = self.style or ''
Expand All @@ -911,7 +920,8 @@ def _make_ts_plot(self, data, **kwargs):
**kwargs)
ax.grid(self.grid)
lines.append(newlines[0])
labels.append(label)
leg_label = to_leg_label(label, 0)
labels.append(leg_label)
else:
for i, col in enumerate(data.columns):
label = com._stringify(col)
Expand All @@ -925,7 +935,8 @@ def _make_ts_plot(self, data, **kwargs):
style=style, **kwds)

lines.append(newlines[0])
labels.append(label)
leg_label = to_leg_label(label, i)
labels.append(leg_label)
ax.grid(self.grid)

self._make_legend(lines, labels)
Expand Down
14 changes: 14 additions & 0 deletions pandas/tseries/tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,12 +744,26 @@ def test_secondary_legend(self):
ax = df.plot(secondary_y=['A', 'B'])
leg = ax.get_legend()
self.assert_(len(leg.get_lines()) == 4)
self.assert_(leg.get_texts()[0].get_text() == 'A (right)')
self.assert_(leg.get_texts()[1].get_text() == 'B (right)')
self.assert_(leg.get_texts()[2].get_text() == 'C')
self.assert_(leg.get_texts()[3].get_text() == 'D')
self.assert_(ax.right_ax.get_legend() is None)
colors = set()
for line in leg.get_lines():
colors.add(line.get_color())
self.assert_(len(colors) == 4)

plt.clf()
ax = fig.add_subplot(211)
ax = df.plot(secondary_y=['A', 'C'], mark_right=False)
leg = ax.get_legend()
self.assert_(len(leg.get_lines()) == 4)
self.assert_(leg.get_texts()[0].get_text() == 'A')
self.assert_(leg.get_texts()[1].get_text() == 'B')
self.assert_(leg.get_texts()[2].get_text() == 'C')
self.assert_(leg.get_texts()[3].get_text() == 'D')

plt.clf()
ax = fig.add_subplot(211)
df = tm.makeTimeDataFrame()
Expand Down

0 comments on commit ce8fd02

Please sign in to comment.