Commit a294c3d6 authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Update the class Graph by adding the method bar, line and point_like.

parent 8b1ba72b
......@@ -236,54 +236,168 @@ class Graph(object):
mpl.rcParams['ytick.major.size'] = 8
mpl.rcParams['ytick.minor.size'] = 4
# plot the DataFrame without summary rows or columns
# select the tool to render the plot
if kind in ("bar", "barh"):
ax = (metric2d.df0
.set_index(config.group_field_y)
.transpose()
.plot(**config_graph))
func = Graph.bar_like
elif kind in ("area", "line"):
df1 = metric2d.df0.transpose()
df1.columns = df1.loc["line", :]
df1 = df1[df1.index != "line"]
func = Graph.line_like
del config_graph[u"width"]
elif kind in ("scatter"):
func = Graph.point_like
# produce the plot without summary rows or columns
self.ax = func(metric2d.df0,
group_field_x,
group_field_y,
metric_field_z,
config_graph)
@staticmethod
def bar_like(df,
group_field_x,
group_field_y,
metric_field_z,
config_graph):
"""Produce bar, barh plot
linewidth = config_graph[u"linewidth"]
config_graph[u"linewidth"] = (1 if linewidth == 0 else linewidth)
Args:
df (pandas.DataFrame):
group_field_x (str):
group_field_y (str):
metric_field_z (str):
config_graph (dict): keyword argument of the DataFrame.plot method
ax = df1.plot(**config_graph)
Returns:
matplotlib.axes or None
elif kind in ("scatter"):
df1 = metric2d.df0.transpose()
df1.columns = df1.loc["line", :]
df1 = (df1[df1.index != "line"]
.reset_index())
"""
ax = (df
.set_index(group_field_y)
.transpose()
.plot(**config_graph))
# tick and axis labels
xlabel, ylabel = group_field_x, metric_field_z
if config_graph[u"stacked"]:
ylabel += " (stacked)"
if config_graph[u"kind"] == "barh":
xlabel, ylabel = ylabel, xlabel
ticks_and_labels(ax, xlabel, ylabel)
del config_graph[u"width"]
del config_graph[u"colormap"]
# legend
legend = ax.get_legend()
if legend is not None:
ax.get_legend().set_title("")
return ax
@staticmethod
def line_like(df,
group_field_x,
group_field_y,
metric_field_z,
config_graph):
"""Produce area, line plot
Args:
df (pandas.DataFrame):
group_field_x (str):
group_field_y (str):
metric_field_z (str):
config_graph (dict): keyword argument of the DataFrame.plot method
Returns:
matplotlib.axes or None
# choose color from the Spectral colormap
colors = plt.cm.Spectral(np.linspace(0., 1., df1.columns.size))
"""
# clean the configuration of the graph
del config_graph[u"width"]
# protection
linewidth = config_graph[u"linewidth"]
config_graph[u"linewidth"] = (1 if linewidth == 0 else linewidth)
ax = None
for i, col in enumerate(df1.columns):
if col == group_field_x:
continue
# graph
df1 = df.transpose()
df1.columns = df1.loc[group_field_y, :]
df1 = df1[df1.index != group_field_y]
ax = df1.plot(ax=ax,
color=colors[i],
x=group_field_x,
y=col, **config_graph)
ax = df1.plot(**config_graph)
# tick and axis labels
xlabel, ylabel = group_field_x, metric_field_z
ylabel = \
(ylabel + " (stacked)" if config_graph[u"stacked"] else ylabel)
if kind == "barh":
xlabel, ylabel = ylabel, xlabel
if config_graph[u"stacked"]:
ylabel += " (stacked)"
if group_field_x == "year":
ax.xaxis.set_major_formatter(mpl.ticker.FormatStrFormatter("%i"))
if group_field_y == "year":
ax.yaxis.set_major_formatter(mpl.ticker.FormatStrFormatter("%i"))
ticks_and_labels(ax, xlabel, ylabel)
# legend
legend = ax.get_legend()
if legend is not None:
ax.get_legend().set_title("")
return ax
@staticmethod
def point_like(df,
group_field_x,
group_field_y,
metric_field_z,
config_graph):
"""Produce scatter plot
Args:
df (pandas.DataFrame):
group_field_x (str):
group_field_y (str):
metric_field_z (str):
config_graph (dict): keyword argument of the DataFrame.plot method
Returns:
matplotlib.axes or None
"""
# clean the configuration of the graph
del config_graph[u"colormap"]
del config_graph[u"stacked"]
del config_graph[u"width"]
# prepare the DataFrame
df1 = df.transpose()
df1.columns = df1.loc[group_field_y, :]
df1 = (df1[df1.index != group_field_y]
.reset_index())
# graph
# colorize the point, one color per columns
# colors come from the Spectral colormap
colors = plt.cm.Spectral(np.linspace(0., 1., df1.columns.size))
ax, columns = None, df1.columns
for i, col in enumerate(columns):
if col == group_field_x:
continue
ax = df1.plot(ax=ax,
color=colors[i],
label=columns[i],
x=group_field_x,
y=col,
**config_graph)
# tick and axis labels
xlabel, ylabel = group_field_x, metric_field_z
if group_field_x == "year":
ax.xaxis.set_major_formatter(mpl.ticker.FormatStrFormatter("%i"))
......@@ -298,8 +412,7 @@ class Graph(object):
if legend is not None:
ax.get_legend().set_title("")
# keep the plot for latter use
self.ax = ax
return ax
def _savefig(self, fmt):
"""Save the figure as a string.
......
......@@ -106,7 +106,8 @@
cfgPanelImg = {
border: false,
html: '<img src="data:image/svg+xml;utf8,{{=img}}" height=400></img>',
itemId: "graphPanel"
itemId: "graphPanel",
padding: "0 0 0 10"
};
//
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment