Skip to content

Commit

Permalink
Smarter tick labeling. #3
Browse files Browse the repository at this point in the history
  • Loading branch information
onyxfish committed Sep 10, 2015
1 parent 03f00f6 commit 695eb06
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 27 deletions.
13 changes: 7 additions & 6 deletions fever/charts/bars.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import agate
from matplotlib import pyplot

from fever.base import Chart
from fever.charts.base import Chart
from fever.colors import Qualitative

class Bars(Chart):
Expand All @@ -29,7 +29,7 @@ def __init__(self, label_column_name, value_column_names):
def _show_legend(self):
return len(self._value_column_names) > 1

def _plot(self, table):
def _plot(self, table, axes):
label_column = table.columns[self._label_column_name]

if not isinstance(label_column, agate.TextColumn) and \
Expand All @@ -53,7 +53,7 @@ def _plot(self, table):
for j in positions:
series_positions.append(positions[j] + (i + 1) * bar_height)

plot_bars = pyplot.barh(
plot_bars = axes.barh(
series_positions,
value_column,
bar_height,
Expand All @@ -63,10 +63,11 @@ def _plot(self, table):

bars.extend(plot_bars)

pyplot.ylabel(self._label_column_name)
pyplot.yticks(series_positions, table.columns[self._label_column_name])
axes.set_ylabel(self._label_column_name)
axes.set_yticks(series_positions)
axes.set_yticklabels(table.columns[self._label_column_name])

if len(self._value_column_names) == 1:
pyplot.xlabel(self._value_column_names[0])
axes.set_xlabel(self._value_column_names[0])

return (bars, self._value_column_names)
6 changes: 3 additions & 3 deletions fever/charts/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Chart(object):
"""
Base class for a chart type.
"""
def _plot(self):
def _plot(self, table, axes):
"""
Subclasses implement this method to draw a single chart, regardless of
whether or not it is part of small multiples.
Expand Down Expand Up @@ -62,7 +62,7 @@ def run(self, source, filename=None, size=None, dpi=DEFAULT_DPI):
for i, (key, table) in enumerate(source.items()):
axes = pyplot.subplot(rows, columns, i + 1)

legend = self._plot(table)
legend = self._plot(table, axes)

pyplot.title(key)

Expand All @@ -88,7 +88,7 @@ def run(self, source, filename=None, size=None, dpi=DEFAULT_DPI):
pyplot.figure(figsize=size, dpi=dpi)
axes = pyplot.subplot(1, 1, 1)

legend = self._plot(source)
legend = self._plot(source, axes)

pyplot.grid(b=True, which='major', color='0.85', linestyle='-')
axes.set_axisbelow(True)
Expand Down
13 changes: 7 additions & 6 deletions fever/charts/columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import agate
from matplotlib import pyplot

from fever.base import Chart
from fever.charts.base import Chart
from fever.colors import Qualitative

class Columns(Chart):
Expand All @@ -29,7 +29,7 @@ def __init__(self, label_column_name, value_column_names):
def _show_legend(self):
return len(self._value_column_names) > 1

def _plot(self, table):
def _plot(self, table, axes):
label_column = table.columns[self._label_column_name]

if not isinstance(label_column, agate.TextColumn) and \
Expand All @@ -53,7 +53,7 @@ def _plot(self, table):
for j in positions:
series_positions.append(positions[j] + (i + 1) * bar_width)

plot_bars = pyplot.bar(
plot_bars = axes.bar(
series_positions,
value_column,
bar_width,
Expand All @@ -63,10 +63,11 @@ def _plot(self, table):

bars.extend(plot_bars)

pyplot.xlabel(self._label_column_name)
pyplot.xticks(series_positions, table.columns[self._label_column_name])
axes.set_xlabel(self._label_column_name)
axes.set_xticks(series_positions)
axes.set_xticklabels(table.columns[self._label_column_name])

if len(self._value_column_names) == 1:
pyplot.ylabel(self._value_column_names[0])
axes.set_ylabel(self._value_column_names[0])

return (bars, self._value_column_names)
10 changes: 5 additions & 5 deletions fever/charts/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import agate
from matplotlib import pyplot

from fever.base import Chart
from fever.charts.base import Chart
from fever.colors import Qualitative

class Lines(Chart):
Expand All @@ -28,7 +28,7 @@ def __init__(self, x_column_name, y_column_names):
def _show_legend(self):
return len(self._y_column_names) > 1

def _plot(self, table):
def _plot(self, table, axes):
colors = Qualitative()
lines = []

Expand All @@ -45,7 +45,7 @@ def _plot(self, table):
if not isinstance(y_column, agate.NumberColumn):
raise ValueError('Only NumberColumn is supported for line chart Y-axis.')

plot_lines = pyplot.plot(
plot_lines = axes.plot(
x_column,
y_column,
linewidth=2,
Expand All @@ -55,9 +55,9 @@ def _plot(self, table):

lines.extend(plot_lines)

pyplot.xlabel(self._x_column_name)
axes.set_xlabel(self._x_column_name)

if len(self._y_column_names) == 1:
pyplot.ylabel(self._y_column_names[0])
axes.set_ylabel(self._y_column_names[0])

return (lines, self._y_column_names)
10 changes: 5 additions & 5 deletions fever/charts/scatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import agate
from matplotlib import pyplot

from fever.base import Chart
from fever.charts.base import Chart

class Scatter(Chart):
"""
Expand All @@ -21,7 +21,7 @@ def __init__(self, x_column_name, y_column_name):
def _show_legend(self):
return False

def _plot(self, table):
def _plot(self, table, axes):
x_column = table.columns[self._x_column_name]
y_column = table.columns[self._y_column_name]

Expand All @@ -31,10 +31,10 @@ def _plot(self, table):
if not isinstance(y_column, agate.NumberColumn):
raise ValueError('Only NumberColumn is supported for scatter chart Y axis values.')

pyplot.scatter(
axes.scatter(
x_column,
y_column
)

pyplot.xlabel(self._x_column_name)
pyplot.ylabel(self._y_column_name)
axes.set_xlabel(self._x_column_name)
axes.set_ylabel(self._y_column_name)
2 changes: 0 additions & 2 deletions samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@
('noX', agate.Sum(), 'noX')
])

date_totals.pretty_print(5)

single_series = {
'line_chart_simple': fever.Lines('day', 'co2'),
'column_chart_simple': fever.Columns('day', 'co2'),
Expand Down

0 comments on commit 695eb06

Please sign in to comment.