Placing data frame chart in tab

Viewed 43

This is my data frame

Date    Alarm   Count
0   2015-10-02  Adam    1
1   2015-01-02  David   4
2   2015-01-02  Adam    4
3   2015-12-02  Adam    0
4   2015-01-02  David   6
5   2015-08-02  Hardik  2
6   2015-03-02  David   4

Using this data frame I have created a stacked bar chart

df2 = pd.DataFrame ({i : [j for j in df['Count']] for i in df['Alarm']},index = [i for i in df['Date']])
df2.plot(kind = 'bar',stacked = True,figsize=(15, 5))

This is my output enter image description here

Now I want to insert this plot in a tkinter notebook tab and want the legend name as "Name". How could I do that?

1 Answers

Try this:

import pandas as pd
import matplotlib
matplotlib.use('TkAgg')
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from tkinter import *

window= Tk()

fig = Figure(figsize=(2,2))
a = fig.add_subplot(111)

df = pd.DataFrame({
    'Date': ['2015-10-02', '2015-01-02', '2015-01-02', '2015-12-02', '2015-01-02', '2015-08-02', '2015-03-02'],
    'Alarm': ['Adam', 'David', 'Adam', 'Adam', 'David', 'Hardik', 'David'],
    'Count': [1,4,4,0,6,2,4]})

df2 = pd.DataFrame ({i : [j for j in df['Count']] for i in df['Alarm']},index = [i for i in df['Date']])
df2.plot(kind = 'bar',stacked = True,figsize=(15, 5), ax=a)

a.set_title ("Name", fontsize=16)
a.set_xticklabels( df["Date"], rotation=0 )

canvas = FigureCanvasTkAgg(fig, master=window)
canvas.get_tk_widget().pack()
canvas.draw()

window.mainloop()

Output:

enter image description here

Related