PatrikHlobil / Pandas-Bokeh

Bokeh Plotting Backend for Pandas and GeoPandas

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to plot data from multipe dataframes to a single figure?

segelmache opened this issue · comments

Hi,

I'm trying to plot two histograms - one from Dataframe1, the other from Dataframe2 - as a "side-by-side" into one figure. Is this somehow possible as it is in bokeh?

Thanks and regards

Hi @segelmache ,

there is unfortunately not a direct way to so so. However, there is a nice workaround:

import pandas as pd
from numpy.random import normal, seed
import pandas_bokeh

pandas_bokeh.output_notebook()

# Create 2 Gaussian distribution in 2 dataframes:
seed(42)
df_1 = pd.DataFrame({"value": normal(loc=3, scale=2, size=1000)})
df_2 = pd.DataFrame({"value": normal(loc=1, scale=2, size=1000)})

# Add 2 new columns for aggregation
df_1["dataframe 1"] = df_1["value"]
df_2["dataframe 1"] = None
df_1["dataframe 2"] = None
df_2["dataframe 2"] = df_2["value"]

# Union both dataframes:
df = pd.concat([df_1, df_2])

# Plot Histograms of 2 DataFrames:
df.plot_bokeh.hist(y=["dataframe 1", "dataframe 2"])

image

Hope this helps,

best Patrik

Hi @segelmache,

I hope I answered your question and would close the issue. If you insist that this issue should be reopened, please mail me.

Best Patrik

I reopen this issue since I cannot manage to do so with multiple lines.

I've tried with the following code:

import pandas as pd
import numpy as np
import pandas_bokeh

np.random.seed(42)
df = pd.DataFrame({
    "Google": np.random.randn(1000) + 0.2,
    "Apple": np.random.randn(1000) + 0.17
},
                  index=pd.date_range('1/1/2000', periods=1000))
df = df.cumsum()
df = df + 50

ema = df.groupby(pd.Grouper(freq='1W')).mean().ewm(span=1, adjust=False).mean()

df.plot()
ema.plot()

but I got 2 figures.
If I add replace df.plot() by df.plot(show_figure=False), I don't get the first figure.
And I don't see how to use the workaround proposed in this issue.

I was expecting the same interface geolayers has, meaning:

figure = df.plot(show_figure=False)
ema.plot(figure=figure)

but unfortunately, it is not working with Line glyph

AttributeError: unexpected attribute 'figure' to Line, possible attributes are js_event_callbacks, js_property_callbacks, line_alpha, line_cap, line_color, line_dash, line_dash_offset, line_join, line_width, name, subscribed_events, syncable, tags, x or y

I have create PR #122 in order to make that possible