I'm trying to plot four figure labels with two decimal places - e.g '1475.88' onto my X-Axis. As you can see the label has been shorted by Matplotlib to the scientific format 1.478e3.
How do I display the full figure, and at a defined spacing.
Code below:
with plt.style.context('seaborn-whitegrid'):
# Plot the SVP data
plt.figure(figsize=(8, 8))
plt.plot(speed_x_clean, depth_y_clean)
plt.plot(smoothed_speed, smoothed_depth)
plt.scatter(float(speed_extrapolated), float(depth_extrapolated),marker='X', color='#ff007f')
# Add a legend, labels and titla
plt.gca().invert_yaxis()
plt.legend(('Raw SVP', 'Smoothed SVP'), loc='best')
plt.title('SVP - ROV '+ '['+ time_now + ']')
plt.xlabel('Sound Velocity [m/s]')
plt.ylabel('Depth [m]')
# plt.grid(color='grey', linestyle='--', linewidth=0.25, grid_animated=True)
ax = plt.axes()
plt.gca().xaxis.set_major_locator(plt.AutoLocator())
plt.xticks(rotation=0)
plt.show()
Labels with two Decimal Places
Using a ticker FuncFormatter any user defined format can be achieved.
@ticker.FuncFormatter
def major_formatter(val, pos):
return "%.2f" % val
Labels at a Defined Spacing
With set_xticks and set_yticks the number of labels can be set at a defined spacing.
Self-Contained Example
A completely self-contained example could look like this (simple sine wave):
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as ticker
@ticker.FuncFormatter
def major_formatter(val, pos):
return "%.2f" % val
def graph():
x = np.arange(0.0, 1501, 50)
y = np.sin(2 * np.pi * x / 1500)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.xaxis.set_major_formatter(major_formatter)
ax.yaxis.set_major_formatter(major_formatter)
x_ticks = np.arange(0, 1501, 500)
y_ticks = np.arange(-1.0, +1.01, 0.5)
ax.set_xticks(x_ticks)
ax.set_yticks(y_ticks)
ax.grid(which='both')
plt.show()
if __name__ == '__main__':
graph()
Output
Here the output of the example program: it has four figure labels with two decimal places on the x-axis: