Skip to content
Snippets Groups Projects
Commit 0b66fd2d authored by Stefan Knauff's avatar Stefan Knauff
Browse files

add method to plot multiple results next to each other

parent bd762ecf
No related branches found
No related tags found
No related merge requests found
This diff is collapsed.
...@@ -105,36 +105,62 @@ class Xgta: ...@@ -105,36 +105,62 @@ class Xgta:
]) ])
) )
return self.Result(q, search_term) return self.Result(self, q, search_term)
def plot_frequency_of(self, results=[], plot=[], shared_xaxes=True, shared_yaxes=True):
if plot:
absolut = [st for st in plot if not st.startswith('percent')]
percent = [st for st in plot if st.startswith('percent')]
n_rows = 2 if len(absolut) > 0 and len(percent) > 0 else 1
subplot_titles = []
for i in results:
for y in range(n_rows):
subplot_titles.append(i.search_term)
print(subplot_titles)
fig = make_subplots(
rows=n_rows*len(results),
cols=1,
shared_xaxes=shared_xaxes,
shared_yaxes=shared_yaxes,
vertical_spacing=0.07,
subplot_titles=subplot_titles,
)
for nth_result, result in enumerate(results):
for i in px.line(result.df, x="postdate", y=absolut).data:
fig.append_trace(i, row=nth_result*n_rows+1, col=1)
for i in px.line(result.df, x="postdate", y=percent).data:
fig.append_trace(i, row=nth_result*n_rows+n_rows, col=1)
if len(results) == 1:
fig.update_layout(title_text=results[0].search_term)
fig.update_layout(height=len(results)*500, hovermode='x unified')
fig.update_traces(
mode="lines",
hovertemplate = "%{y}"),
fig.show()
class Result: class Result:
def __init__(self, df, search_term): def __init__(self, outer_instance, df, search_term):
self.df = df self.df = df
self.search_term = search_term self.search_term = search_term
self.outer_instance = outer_instance
def plot_frequency_of(self, plot=[]): def plot_frequency_of(self, plot=[], shared_xaxes=True, shared_yaxes=False):
if plot: if plot:
self.outer_instance.plot_frequency_of([self], plot=plot, shared_xaxes=shared_xaxes, shared_yaxes=shared_yaxes)
absolut = [st for st in plot if not st.startswith('percent')]
percent = [st for st in plot if st.startswith('percent')]
n_rows = 2 if len(absolut) > 0 and len(percent) > 0 else 1
fig = make_subplots(rows=n_rows, cols=1, shared_xaxes=True,vertical_spacing=0.02)
for i in px.line(self.df, x="postdate", y=absolut).data:
fig.append_trace(i, row=1, col=1)
for i in px.line(self.df, x="postdate", y=percent).data:
fig.append_trace(i, row=n_rows, col=1)
fig.update_layout(title_text=self.search_term, height=600, hovermode='x unified')
fig.update_traces(
mode="lines",
hovertemplate = "%{y}"),
fig.show()
else: else:
print(""" print("""
Uncomment columns of interest: Uncomment columns of interest:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment