0

It seems weird that I cant find an answer to this question since this seems like something that is essential for a lot of plots, but how can I create an x and y axis for my plot?

This is a workaround that I use so far:

def f(x): return np.sin(x)
x = np.linspace(-2, 2, 1000)
plt.plot(x, f(x))
plt.plot(x, np.zeros_like(x))
plt.plot(np.zeros_like(x), x)

which produces:

enter image description here

What I would like is something like this:

enter image description here

What could I do to reproduce that? What I would like especially are the x and y axis numbering that is currently outside the plot (with matplotlib) to be on the x and y axis inside the plot, like on the image. Additionally I believe there should be an easier method to plot the x and y axis, instead of doing the hack that I do.

I've looked at a lot of matplotlib examples and can't find any example that contains what I want...

gboffi
  • 22,939
  • 8
  • 54
  • 85
charelf
  • 3,103
  • 4
  • 29
  • 51
  • 9
    This is what you need to get started: https://matplotlib.org/gallery/ticks_and_spines/spine_placement_demo.html – Andrew Apr 03 '20 at 08:45
  • 1
    In case you are interested in tick labels like in the example you have shown (0, π/2, π, 3π/2, ...) then I suggest that you look at [this answer](https://stackoverflow.com/a/53586826/2749397) by [Scott Centoni](https://stackoverflow.com/users/2327973/scott-centoni). – gboffi Apr 10 '20 at 09:14
  • Nice, that is indeed even closer to what I as expecting! Thank you – charelf Apr 12 '20 at 08:10

0 Answers0