1

I am trying to produce the following plot using the code:

import numpy as np
import matplotlib.pyplot as plt
from mpltools import annotation
import matplotlib.ticker as ticker

h = np.array([1.44, 0.72, 0.36, 0.18])

m2_velocity_2 = np.array([0.088373, 0.0440217, 0.0149557, 0.00429101])
m2_pressure_2 = np.array([1.62808, 0.741043, 0.244384, 0.069385])

fig = plt.figure(figsize=(10, 5))

ax1 = plt.subplot(121)
ax1.loglog(h, m2_velocity_2, '-*', label='weight = 2')
annotation.slope_marker((0.4, 0.012), (2, 1), ax=ax1)
ax1.xaxis.set_major_locator(ticker.MaxNLocator(nbins=3))
ax1.set_title("Velocity")
ax1.set_xlabel("Average particle spacing")
ax1.set_ylabel("RMS error")
ax1.grid()

ax2 = plt.subplot(122)
ax2.loglog(h, m2_pressure_2, '-*', label='weight = 2')
annotation.slope_marker((0.4, 0.20), (2, 1), ax=ax2)
ax2.set_title("Pressure")
ax2.set_xlabel("Average particle spacing")
ax2.set_ylabel("RMS error")
ax2.grid()

fig.tight_layout()
plt.show()

which produced a very tight chunk of ticks in the axis. I have tried methods from this and this but it doesn't seem to clean up the axis. Attached is the graph generated from the code above:

enter image description here

Any suggestions?

Quang Thinh Ha
  • 239
  • 2
  • 10
  • How about [`rotation=45`](https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.xticks.html?highlight=xtick%20rotation) instead of complicated manual ticker/label setting? – Mr. T Nov 05 '20 at 22:19
  • @Mr.T I'm trying to address what my reviewer said, without making more troubles so probably will save that for another time... – Quang Thinh Ha Nov 05 '20 at 23:06

1 Answers1

0

I agree with @Mr.T that the best way would be to just rotate the labels. Perhaps one alternative is to shift alternate labels down?

for xtl in ax1.xaxis.get_minorticklabels()[1::2]:
    xtl.set_position((xtl.get_position()[0], xtl.get_position()[1]-0.05))

It's far from ideal because your visible tick labels are a bit erratic but it's a quick fixenter image description here

Benedictanjw
  • 828
  • 1
  • 8
  • 19