0

I have this weird thing with the scale of the axis showing out of the figure like: enter image description here

And what I want to have: enter image description here How can I move the scale to the other side of the axis?

x=range(len(ticks))
plt.plot(x,phase1,'r^-',label='$\Delta \phi(U1,I1)$')
plt.plot(x,phase2,'go-',label='$\Delta \phi(U2,I2)$')
plt.plot(x,phase3,'b*-',label='$\Delta \phi(U3,I3)$')
plt.xticks(x,ticks,rotation=45)
plt.xlabel('Messung')
plt.ylabel('$\Delta \phi [^\circ]$')
plt.legend()
plt.show()
Yingqiang Gao
  • 939
  • 4
  • 16
  • 29

2 Answers2

2

The tick_params of your axis can be used to control axes label and ticks location. Set direction to in so that they point into the graph.

And here is a great example if you want different y-axis ranges and colours too.

from matplotlib import pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.tick_params(direction='in', length=6, width=2, colors='r', right=True, labelright='on')

plt.show()

enter image description here

henneray
  • 449
  • 3
  • 10
1

You can use plt.tick_params() to adjust the behaviour of the ticks, documentation can be found here.

For your example you want the ticks to appear inside the figure. Therefore add

plt.tick_params(direction="in")

to your code. Example:

x=range(len(ticks))

plt.plot(x,phase1,'r^-',label='$\Delta \phi(U1,I1)$')
plt.plot(x,phase2,'go-',label='$\Delta \phi(U2,I2)$')
plt.plot(x,phase3,'b*-',label='$\Delta \phi(U3,I3)$')

plt.xticks(x,ticks,rotation=45)
plt.xlabel('Messung')
plt.ylabel('$\Delta \phi [^\circ]$')
plt.legend()

plt.tick_params(direction="in") # Set ticks inside the figure

plt.show()

You can get the ticks to appear on the top and right side of the figure too as shown in your second screenshot by adding:

plt.tick_params(direction="in",top="on",right="on")

If you wanted to make all figures in your Python script have this behaviour then you can add the following at the top of your script (this might be of interest):

import matplotlib

matplotlib.rcParams['xtick.direction'] = "in"
matplotlib.rcParams['ytick.direction'] = "in"

This will save you having to call plt.tick_params() for each figure, which is helpful if you generate lots of figures.

DavidG
  • 24,279
  • 14
  • 89
  • 82