0

Using code from David Dale (Time Wheel in python3 pandas), my data is fairly large but has a few hours that don't have data and subsequently the corresponding wedges are not shown in the time wheel. And so the wheel is missing wedges and it makes it look wrong even though it technically is not wrong.

I have searched the proposed questions when asking this question and tried to understand the code to alter it but cannot.

David Dale code copied from the link:

def pie_heatmap(table, cmap=cm.hot, vmin=None, vmax=None,inner_r=0.25, pie_args={}):
    n, m = table.shape
    vmin= table.min().min() if vmin is None else vmin
    vmax= table.max().max() if vmax is None else vmax
    centre_circle = plt.Circle((0,0),inner_r,edgecolor='black',facecolor='white',fill=True,linewidth=0.25)
    plt.gcf().gca().add_artist(centre_circle)
    norm = mpl.colors.Normalize(vmin=vmin, vmax=vmax)
    cmapper = cm.ScalarMappable(norm=norm, cmap=cmap)
    for i, (row_name, row) in enumerate(table.iterrows()):
        labels = None if i > 0 else table.columns
        wedges = plt.pie([1] * m,radius=inner_r+float(n-i)/n, colors=[cmapper.to_rgba(x) for x in row.values], 
            labels=labels, startangle=90, counterclock=False, wedgeprops={'linewidth':-1}, **pie_args)
        plt.setp(wedges[0], edgecolor='white',linewidth=1.5)
        wedges = plt.pie([1], radius=inner_r+float(n-i-1)/n, colors=['w'], labels=[row_name], startangle=-90, wedgeprops={'linewidth':0})
        plt.setp(wedges[0], edgecolor='white',linewidth=1.5)

The code works well - thanks Dave - but I need it to make the time wheel with 24 wedges regardless of whether data exists or not for that wedge. Thanks for any help!

Update: I wrote a script to make it work for me. data is my 7x24 data table (7 days by 24 hours each). hours is a list of the 24 hours of the day. ie, ['00:00','01:00','02:00','03:00' ... '23:00']. It is a list of strings because the data is strings when we get it.

blankhours=pandas.DataFrame(0,index=np.arange(0,24),columns=np.arange(1)) #to get 0s
shape=data.shape
if shape[1] < 24: #to access shape of the columns
    for h in hours:
        hour=0 #counter
        if h not in data.columns.values: #see if it is in what should be the complete list
            data.insert(hour,h,blankhours) #insert it in there since it wasn't in there
        hour+=1 #increment counter
data=data.sort_index(axis=1) #sort the final dataframe by column headers

Hopefully that helps someone...

bpfreefly
  • 15
  • 5
  • I believe the issue is with your data and not the the graphing code. You need to generate the missing hours in your data (even if the data in those rows are all NaN). – Ben Pap Apr 18 '19 at 20:35
  • Did you try passing `vmin=0, vmax=23`? – Quang Hoang Apr 18 '19 at 21:44
  • Yes. tried that and no joy. I saw pandas.crosstab (which I use in the main body not shown in the code here) has an argument, dropna=True. If you set that to False, it is supposed to include all categories regardless of whether it has data or not. But it is not working for me. I continue to not get all 24 wedges. hmmmm – bpfreefly Apr 19 '19 at 14:51
  • I found out that it is supposedly a known bug in python. (the dropna=False not working for pandas.crosstab). so, in case anyone comes here for the same thing, I wrote script to make it work for my purposes. I will try to insert the code in the original post since I cant do it here. – bpfreefly Apr 19 '19 at 19:32

0 Answers0