First time using jupyter & plotly, so I'm really struggling
This is my code
# import packages
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np
import seaborn as sns
import plotly.plotly as py
import plotly.tools as tls
from plotly.graph_objs import *
py.sign_in("x", "y")
tls.set_credentials_file(username='x', api_key='y')
tls.get_credentials_file()
fig1 = plt.figure()
%matplotlib notebook
# load data
revels_data = pd.read_csv("directory.data.txt")
rd = revels_data
# grouped bar plot
grouped = rd.groupby(["Packet number", "Flavour"])["Contents"].sum().unstack().fillna(0)
grouped.plot(kind="bar", stacked=True, color=["#a2653e", "#a6814c", "#fd5956", "#fd8d49", "#9c6da5", "#c9ae74"])
# title and axis labels
plt.title("NUMBER OF REVELS PER PACKET", weight="bold")
plt.ylabel("Contents")
plt.xlabel("Packet")
# plot median line
median = 19
plt.axhline(median, color='grey', linestyle='dashed', linewidth=0.5)
# legend properties
plt.legend(loc=2, prop={"size":7})
# extend y axis to fit legend in
axes = plt.gca()
axes.set_ylim([0,30])
# show plot
plt.show()
which produces the plot:
This displays in jupyter fine.
Now, what I want to do it add hover functionality essentially exactly like this example:
https://plot.ly/matplotlib/bar-charts/#stacked-bar-chart-with-labels
where hovering the mouse over each bar shows the flavour distribution per pack.
i.e: individual tabs for: total: a, orange: b, toffee: c, etc. appear when hovering over each packet's bar in their respective flavour's colour.
I have had a fiddle for about 3 hours but I am getting no where.
Please help, thanks!
EDIT:
# import packages
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np
import seaborn as sns
import plotly.plotly as py
import plotly.tools as tls
from plotly.graph_objs import *
py.sign_in("x", "y")
tls.set_credentials_file(username='x', api_key='y')
tls.get_credentials_file()
%matplotlib notebook
# load data
revels_data = pd.read_csv(".txt")
rd = revels_data
fig1 = plt.figure()
ax = mpl_fig.add_subplot
# grouped bar plot
grouped = rd.groupby(["Packet number", "Flavour"])["Contents"].sum().unstack().fillna(0)
ax.grouped.plot(kind="bar", stacked=True, color=["#a2653e", "#a6814c", "#fd5956", "#fd8d49", "#9c6da5", "#c9ae74"])
# title and axis labels
plt.title("NUMBER OF REVELS PER PACKET", weight="bold")
plt.ylabel("Contents")
plt.xlabel("Packet")
# plot median line
median = 19
plt.axhline(median, color='grey', linestyle='dashed', linewidth=0.5)
# legend properties
plt.legend(loc=2, prop={"size":7})
# extend y axis to fit legend in
axes = plt.gca()
axes.set_ylim([0,30])
# show plot
plotly_fig = tls.mpl_to_plotly(fig1)
py.plot()
error: AttributeError: 'function' object has no attribute 'grouped'


"directory.data.txt"- Maximilian Peters