Brennan
Brennan

Reputation: 429

Python matplotlib, adding single custom tickmark on axis

I am trying to label the intersection of two lines in a plot I have made. The code/MWE is:

import matplotlib.pyplot as plt
import numpy as np

#ignore my gross code, first time ever using Python :-)

#parameters
d = 0.02
s = 0.50 #absurd, but dynamics robust to 1>s>0
A = 0.90
u = 0.90

#variables
kt = np.arange(0, 50, 1)
invest = (1 - np.exp(-d*kt))*kt
output = A*u*kt
saving = s*output

#plot
plt.plot(kt, invest, 'r', label='Investment')
plt.plot(kt, output, 'b', label='Output')
plt.plot(kt, saving, label='Saving')
plt.xlabel('$K_t$')
plt.ylabel('$Y_t$, $S_t$, $I_t$')
plt.legend(loc="upper left")

#Steady State; changes with parameters
Kbar = np.log(1-s*A*u)/-d
x, y = [Kbar, Kbar], [0, s*A*u*Kbar]
plt.plot(x, y, 'k--')

#custom axes (no top and right)
ax = plt.gca()
right_side = ax.spines["right"]
right_side.set_visible(False)
top_side = ax.spines["top"]
top_side.set_visible(False)
#ax.grid(True)   #uncomment for gridlines
plt.xlim(xmin=0) #no margins; preference
plt.ylim(ymin=0)

plt.show()

which creates: enter image description here

I am trying to create a little label at the bottom of the dotted black line that says "$K^*$". I want it to coincide with Kbar so that, like the black line, it moves along with the parameters. Any tips or suggestions here?

Upvotes: 0

Views: 164

Answers (2)

r-beginners
r-beginners

Reputation: 35115

I don't quite understand what you mean by "under the black dotted line", but you can already use the coordinate data of the dotted line to annotate it. I put it above the intersection point, but if you want to put it near the x-axis, you can set y=0.

plt.text(max(x), max(y)+1.5, '$K^*$', transform=ax.transData)

enter image description here

Upvotes: 1

Bing Wang
Bing Wang

Reputation: 1598

baseTicks=list(plt.xticks()[0]) #for better control, replace with a range or arange
ax.set_xticks(baseTicks+[np.log(1-A*u*s)/(-d)]) 
ax.set_xticklabels(baseTicks+['$K^*$'])

Upvotes: 1

Related Questions