nipy
nipy

Reputation: 5508

Modification of horizontal bar plot in Pandas

I have put together a plot using the Pandas plot functionality but would appreciate help finishing it with the follows elements (as shown on desired output plot image):

  1. A vertical line at 0 on x axis.
  2. 10 point increment on the x axis.
  3. I would like the OpenToLast bar data to be more prominent so was looking to fade the other stacked bars into the background if this is possible?

Data:

Please see DataFrame.to_dict() output here.

This is how I am getting the existing plot:

auction[['OpenToLast','OpenToMaxHigh','OpenToMaxLow']].head(20).plot(kind='barh',
                        figsize=(7,10),
                        fontsize=10,
                        colormap ='winter',                                        
                        stacked = True,                                
                        legend = True)

Current Plot:

enter image description here

Desired Output:

enter image description here

Upvotes: 2

Views: 1060

Answers (2)

bunji
bunji

Reputation: 5223

Try the following:

It turns out the trickiest part is the coloring but drawing the lines and updating the ticks is relatively straightforward (see the end of the code)

import numpy as np

# get the RGBA values from your chosen colormap ('winter')
winter = matplotlib.cm.winter 
winter = winter(range(winter.N))

# select N elements from winter depending on the number of columns in your
# dataframe (make sure they are spaced evenly from the colormap so they are as 
# distinct as possible)
winter = winter[np.linspace(0,len(winter)-1,auction.shape[1],dtype=int)]

# set the alpha value for the two rightmost columns 
winter[1:,3] = 0.2   # 0.2 is a suggestion but feel free to play around with this value

new_winter = matplotlib.colors.ListedColormap(winter) # convert array back to a colormap   

# plot with the new colormap
the_plot = auction[['OpenToLast','OpenToMaxHigh','OpenToMaxLow']].head(20).plot(kind='barh',
                        figsize=(7,10),
                        fontsize=10,
                        colormap = new_winter,                                        
                        stacked = True,                                
                        legend = True)

the_plot.axvline(0,0,1) # vertical line at 0 on the x axis
start,end = the_plot.get_xlim() # find current span of the x axis
the_plot.xaxis.set_ticks(np.arange(start,end,10)) # reset the ticks on the x axis with increments of 10

enter image description here

Upvotes: 2

nipy
nipy

Reputation: 5508

I didn't realise that I could use the Pandas plot commands directly with Matplotlib API. I have now copied the code from above and modified to add the additional elements in Matplotlib.

It would be nice to add a gradient to the bars, if anyone knows how to do this, but I will mark this question as answered:

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

cols = ['OpenToLast','OpenToMaxHigh','OpenToMaxLow']
colors = {'OpenToLast':'b', 'OpenToMaxHigh' : '#b885ea', 'OpenToMaxLow': '#8587ea'}

axnum = auction[cols].head(20).plot(kind='barh',
                        figsize=(7,10),
                        fontsize=10,
                        color=[colors[i] for i in cols],                                       
                        stacked = True,                                
                        legend = True)

axnum.xaxis.set_major_locator(ticker.MultipleLocator(10))
plt.axvline(0, color='b')

enter image description here

Upvotes: 0

Related Questions