0

I want to customize my simple bar plot based on frequency of data. Context as follows:

I have a data set, where x-axis is referred to as var_change and y-axis as var_median and error-bars as var_error. The var_change and var_error are calculated at the bin size of 5-var_median. The code and the plot for these variables is shown below:

enter image description here

fig = plt.figure(figsize=(24,7))
ax = [plt.subplot(181)]
ax[i].barh(np.arange(0+2.5,99+2.5,5), var_change, alpha = 0.6, height = 5, 
           color='none', edgecolor='k', capsize=2.5, lw = 0.8)
ax[i].errorbar(var_change, np.arange(0+2.5,99+2.5,5), xerr = var_error, capsize=3, 
               fmt="r:o", ecolor = "black", alpha = 0.8)

I want to independently control both line-width (lw in barh), and alpha in errorbar, as well as color of individual bars in the bar plot below based on frequency of the data set. The frequency can be calculated using the code below:

#df_delta_pos is a data-frame
pos_count = []
for j in np.arange(0,99,5): 
    pos_count.append(df_delta_pos.where((df.var_median> TC_bins[j]) & 
                    (df.var_median<= TC_bins[j+5])).dropna()['var_change'].count())
#Here I would also normalize them if changing 'lw'. Code for normalization can be provided, if needed. 
#For now consider random numbers here.

The final plot should look something like below, but also with change in lw and alpha based on frequency.

enter image description here

1 Answer 1

1

Since you did not provide the data to the plot I cannot reproduce the figures. I try to answer your questions anyways.

  1. Controls in barh: Taking a look at the documentation of barh you can pass to color and lw also a list/array which must be of the same length as the data.
  2. Controls in errorbar: errorbar does not allow to specify a list for alpha. Your alternative is therefore to loop over each element in var_change and plot each element indiviually which then allows you to specify an alpha for each point.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.