How do I stop my yerror error bars from going through zero (the x-axis?)

41 Views Asked by At

I tried to use plt.ylim(0,12) to stop my y-error bars from crossing below zero, but they still do. Any advice?

#House B
x2=[1, 2, 3]
y2=[6.19927899,1.00263735,0.09018145733]
#House C
x3=[1, 2, 3]
y3=[5.631459288,3.198409548,2.141106453]
#House D
x4=[1, 2, 3]
y4=[1.545221235,0.065499083,0]

axis_x= range(1,3)
axis_y=range(0,12)
plt.ylim((0,12))
plt.rcParams['figure.figsize'] = [6, 5]
#ax = plt.subplots()

plt.scatter(x2,y2,  zorder=10, clip_on=False, color='darkmagenta', alpha=0.8, s=100, label='House B')
plt.scatter(x3,y3,  zorder=10, clip_on=False, color='olivedrab', marker='v', alpha=0.8, s=100, label='House C')
plt.scatter(x4,y4,  zorder=10, clip_on=False, color='goldenrod', marker='D', alpha=0.8, s=100, label='House D')

#error bar values
#HouseB
y2error=[0.1445699413, 0.4590436542, 1.303785259]
#HouseC
y3error=[0.08168626561, 0.1449873167, 0.1714882825]
#HouseD
y4error=[0.1737233193, 1.1360461, 0]

#error bars
plt.errorbar(x2,y2,yerr=y2error, zorder=11, clip_on=False, color='darkmagenta', ecolor='k', elinewidth=0.8, capsize=8, capthick=0.8, barsabove=True, alpha=1)
plt.errorbar(x3, y3, yerr=y3error, zorder=11, clip_on=False, color='olivedrab', ecolor='k', elinewidth=0.8, capsize=8, capthick=0.8, barsabove=True, alpha=1)
plt.errorbar(x4,y4,yerr=y4error, zorder=11, clip_on=False, color='goldenrod', ecolor='k', elinewidth=0.8, capsize=8, capthick=0.8, barsabove=True, alpha=1)

plt.plot(x2, y2, color='darkmagenta', zorder=3, linewidth=1)
plt.plot(x3, y3, color='olivedrab', zorder=3, linewidth=1)
plt.plot(x4, y4, color='goldenrod', zorder=3, linewidth=1)

#graph title and axis titles
plt.xlabel('Isolation                   Hallway                      Living', fontsize=14)
plt.ylabel('Airborne Concentration (RNA copies/m³)', fontsize=14)
plt.title('', fontsize=12)

#hide x axis numerical values
frame1 = plt.title('', fontsize=12)
frame1.axes.xaxis.set_ticklabels([])

#add a legend
import numpy as np
import matplotlib.pyplot as plt
plt.legend(loc="upper right")

import matplotlib.pyplot as plt
plt.tick_params(
    axis='x',          # changes apply to the x-axis
    which='both',      # both major and minor ticks are affected
    bottom=False)      # ticks along the bottom edge are off
#plt.figure(figsize=(8,6))
plt.savefig("HomesdPCR3Homes.png", format="png", dpi=300, bbox_inches='tight')
plt.show()
plt.close()

I tried ylim but no luck. Basically, I want my yerror bars to truncate when they touch zero, so that it doesn't look messy with them passing through the x-axis at the bottom. The yerror bars are custom for each data point.

0

There are 0 best solutions below