Yotam
Yotam

Reputation: 10695

displaying a data in a plot label of matplot lib

Is there a way to display a variable (specificaly, a linear fit paramter) in a plot I drow?

Also, is there a way to control the number of figures displayed?

I'm using matplotlib to generate the figures and optimize.curve_fit to do the curve fitting.

Upvotes: 1

Views: 388

Answers (1)

Joe Kington
Joe Kington

Reputation: 284890

Sure, just use text or annotate.

As a quick example:

import numpy as np
import matplotlib.pyplot as plt

# Generate data...
x = np.linspace(0, 30, 15)
y = 0.4 * x + 9 + np.random.random(x.size)

# Linear fit...
model = np.polyfit(x, y, deg=1)

plt.plot(x, y, 'bo', label='Observations')
plt.plot(x, model[0] * x + model[1], 'r-', label='Fitted Model')
plt.annotate(r'$y = {:.2f}x + {:.2f}$'.format(*model), xy=(0.1, 0.9), 
             xycoords='axes fraction', size=18)
plt.legend()

plt.show()

enter image description here

Upvotes: 3

Related Questions