0

I have a dataframe like this:

A   B   C   D   E   F   index1
44544   44544   44544   44544   44544   44544   250
0   0   0   0   761 738 500
0   0   0   0   0   13  750
0   0   0   0   1   3   1000
0   0   0   0   10  11  1250
0   0   2   0   16219   8028    1500
0   0   12560   9649    102 222 1750
0   0   26406   23089   115 56  2000

now I want to plot bar graph using radio buttons in matplotlib. I have tried the following code:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import RadioButtons

df5=pd.read_excel(r'C:\YOIGO\hi.xlsx')

l1=df5.columns[0:6].tolist()
fig, ax = plt.subplots()
l,  = ax.plot(np.array(df5.index1), np.array(df5.iloc[:,2]), lw=2, color='red')
plt.subplots_adjust(left=0.3)

t=tuple(l1)
print(t)
axcolor = 'lightgoldenrodyellow'
rax = plt.axes([0.05, 0.7, 0.15, 0.15], facecolor=axcolor)
radio = RadioButtons(rax, t)
d={}
for x in t:
    d[x]=np.array(df5[x])


def hzfunc(label):
    hzdict = d
    ydata = hzdict[label]
    l.set_ydata(ydata)
    plt.draw()
radio.on_clicked(hzfunc)
plt.show()

But the above code is giving me the normal graph and not bar graph. may I know how to convert this into bar graph plot??

Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75
amrutha
  • 193
  • 1
  • 11

1 Answers1

1

Your problem is that you are using plt.plot() which is meant to draw a line plot, when you wanted to use plt.bar() to create a bar plot.

However, plot() and bar() return different objects, respectively Line2D and BarCollection. Therefore you need to change the logic in your callback function:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import RadioButtons

d = """A   B   C   D   E   F   index1
44544   44544   44544   44544   44544   44544   250
0   0   0   0   761 738 500
0   0   0   0   0   13  750
0   0   0   0   1   3   1000
0   0   0   0   10  11  1250
0   0   2   0   16219   8028    1500
0   0   12560   9649    102 222 1750
0   0   26406   23089   115 56  2000
"""
df5=pd.read_table(StringIO(d), sep='\s+')

fig, ax = plt.subplots()
bars = ax.bar(np.array(df5.index1), height=np.array(df5['A']), color='red', width=200)
plt.subplots_adjust(left=0.3)

axcolor = 'lightgoldenrodyellow'
rax = plt.axes([0.05, 0.7, 0.15, 0.15], facecolor=axcolor)
radio = RadioButtons(rax, df5.columns[:-1])


def hzfunc(label):
    ydata = df5[label]
    for b,y in zip(bars,ydata):
        b.set_height(y)
    plt.draw()
radio.on_clicked(hzfunc)
Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75
  • Hi Asashi, thanks for the solution. this is what the output i want. But i have another query reg this. how can I get all the values of df5[A] to be displayed onto the x-axis and its corresponding bars value? can you please help me with this? because I have a 41 rows of df5[A] and dont get displayed onto the axis. – amrutha Oct 10 '18 at 09:13
  • I would suggest you ask a new question providing the full dataset for df5['A']. In particular, refer to [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – Diziet Asahi Oct 10 '18 at 09:25