'How to show box plot y-axis values in Matplotlib
I have made a function that draws two boxplots side by side based on some input values. The code works fine but I cannot see any numbers on the y axis, it just draws the plots with no reference scale. Does anyone know how to get the y axis values displayed?
Btw I am quite knew to python so I am sure I could have written the function better.
#Define a function where you put the results of the regression. Put in the low and upper
#confidence interval,
# and it will back-transform from square root and then create a boxplot diagram
def boxdata(conf_low,conf_high, conf_low_snus, conf_high_snus):
name = {}
confidence_high = (conf_high**2)
confidence_low = (conf_low**2)
whislo = (confidence_low-(1.5*(confidence_high-confidence_low)))
whishi = (confidence_high+(1.5*(confidence_high-confidence_low)))
confidence_high_snus = (conf_high_snus**2)
confidence_low_snus = (conf_low_snus**2)
whislo_snus = (confidence_low_snus-(1.5*(confidence_high_snus-confidence_low_snus)))
whishi_snus = (confidence_high_snus +(1.5*(confidence_high_snus-confidence_low_snus)))
data_1 = {
'label' : 'No',
'whislo': whislo, # Bottom whisker position calculated as q1 - 1.5*(q3-q1) in this
'q1' : confidence_low, # First quartile (25th percentile)
'med' : "", # Median (50th percentile)
'q3' : confidence_high, # Third quartile (75th percentile)
'whishi': whishi, # Top whisker position calculated as q3 + 1.5*(q3-q1)
'fliers': [0] # Outliers
}
data_2 = {
'label' : 'Yes',
'whislo': whislo_snus, # Bottom whisker position calculated as q1 - 1.5*(q3-q1) in
'q1' : confidence_low_snus, # First quartile (25th percentile)
'med' : "", # Median (50th percentile)
'q3' : confidence_high_snus, # Third quartile (75th percentile)
'whishi': whishi_snus, # Top whisker position calculated as q3 + 1.5*(q3-q1)
'fliers': [0] # Outliers
}
data_3 = [data_1, data_2] #Makes a list of values so that you can plot them together
fig, ax = plt.subplots() # Initiates an empty plot
ax.set(ylabel="Concentration") #Sets a label on the y axes
ax.set_ylim(ymin = 10, ymax = 100) #Define the limits of y axis (min/max)
ax.set_title("Concentration")
ax.bxp(data_3) #, showfliers=False, widths=0.5) # Draws the plot from the data provided
plt.savefig("boxplot.png")
plt.close()
boxdata(5.729958,6.306493,5.2241404,5.8006754)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
