CutePoison
CutePoison

Reputation: 5355

Specify xmax in axhline based on the x-values

Consider the example below:

pca=[0.7,0.2,0.08,0.02]
cumsum=np.cumsum(pca)
ax = sns.lineplot(x =np.arange(len(pca)),y=cumsum)

x_hline = np.argwhere(cumsum>=0.95)[0] # = 3
#ISSUE: Missing some code here to calculate x_hline correctly#
#
#
#

ax.axhline(y=0.95,xmin=0,xmax=x_hline)

I want to draw an horizontal line which starts a 0 and ends when we have reached x-value by x_hline. Since xmax is the specified ratio of the width of the figure i.e between 0 and 1, it is not possible to parse x_hline. I tried to do

x_hline= np.argwhere(cumsum>=0.95)[0] #Assume it is "3"
x_hline= (1-1/x_hline)

and it works (most of the time, okay), but it is not really robust. Is there a way to get the exact ratio of the width, where an element/xtick is according to the ratio of the width (it this case, at what ratio-point is the third element plotted?)

EDIT: Rewritten the question

Upvotes: 0

Views: 2722

Answers (1)

Sameeresque
Sameeresque

Reputation: 2602

You can get more control in this fashion:

ax.plot((x1, x2), (y1, y2), 'k-',linewidth=4)

where x1=0, x2=x_hline, y1=y2=0.95

Upvotes: 2

Related Questions