Issue
Good evening. I am trying to plot a fuzzy number so an element "140" is connected with it's membership function. I tried to plot one more curve above data curve, but that seemed not a good idea. Any advices are appreciated. Thanks for your time. Here's the code and the desired plot:
import numpy as np
import fuzzylab
x = np.linspace(130, 150, 1000)
y = fuzzylab.trimf(x, [133.3, 140, 147])
plt.plot(x, y)
plt.show()
Solution
You can use the axvline
and axhline
to draw lines at specific coordinates.
# Manual way
max_y = y.max()
max_x = x[y.argmax()]
plt.plot(x, y)
plt.axvline(x=max_x, ymin=0, ymax=0.95, c='r', lw=3)
plt.axhline(y=max_y, xmin=0, xmax=0.5, c='r', lw=3)
plt.show()
Note that ymin
, ymax
, xmin
and xmax
are in Axes coordinates (from 0 to 1, see the documentation). Your example is not too bad, you can just fiddle with the numbers until you get something you like.
However, if you want this to be more generic, you'll need to use transforms, from data coordinates to Axes coordinates. You can do that like this:
# Generic way
max_y = y.max()
max_x = x[y.argmax()]
fig, ax = plt.subplots()
ax.plot(x, y)
# For some reason this was necessary, otherwise the limits would behave badly.
ax.set_xlim(ax.get_xlim())
x_prop, y_prop = ax.transLimits.transform((max_x, max_y))
ax.axvline(x=max_x, ymin=0, ymax=y_prop, c='r', lw=3)
ax.axhline(y=max_y, xmin=0, xmax=x_prop, c='r', lw=3)
Here, transLimits
transforms your data
coordinates to axes
coordinates. If you modify your y
variable to be a bit skewed, the red lines will match the position automatically.
Output plot:
Answered By - K.Cl
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.