The code below shows a single point on the graph of a function, which is supposed to be located at y=0
:
x = np.linspace(-3, +3, 100)
y = np.exp(-x**2) * (2 + np.sin(2*x) + np.sin(5*x))
fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(x, y, linestyle='-', linewidth=2.0, color="black")
ax.scatter(x = 0.90, y = 0, color = "blue")
plt.plot()
What would be the simplest way to put the blue point really on the x-axis? (and not slightly above as it is shown on the plot). Thanks for any hints on how to proceed.
The question is a bit unclear. To have the x-axis at y = 0
, you can set ax.set_ylim(ymin=0)
. To have the dot visible without being clipped by the border of the subplot, you can use ax.scatter(..., clip_on=False)
(many other plotting functions also accept clip_on=False
). By the way, plt.plot()
just plots an empty line, it isn't very useful.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3, +3, 100)
y = np.exp(-x ** 2) * (2 + np.sin(2 * x) + np.sin(5 * x))
fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(x, y, linestyle='-', linewidth=2.0, color="black")
ax.scatter(x=0.90, y=0, color="blue", clip_on=False)
ax.set_ylim(ymin=0)
plt.show()
Alternatively, if you just want to put the dot on the lower border of the plot, you can use the x-axis transform (without changing the ylim).
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3, +3, 100)
y = np.exp(-x ** 2) * (2 + np.sin(2 * x) + np.sin(5 * x))
fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(x, y, linestyle='-', linewidth=2.0, color="black")
ax.scatter(x=0.90, y=0, color="blue", clip_on=False, transform=ax.get_xaxis_transform())
plt.show()