deepseek-v3-0324· DeepSeek LMArena Human Preference 140K
How do I get my matplotlib plot to show ticks in whole numbers?
- User
How do I get my matplotlib plot to show ticks in whole numbers?
- Assistant
How to Show Whole Number Ticks in Matplotlib
To ensure your matplotlib plot displays ticks as whole numbers, you can use one of these approaches:
1. Using
MaxNLocator(recommended)import matplotlib.pyplot as plt from matplotlib.ticker import MaxNLocator fig, ax = plt.subplots() ax.plot([1, 2, 3, 4], [1, 4, 9, 16]) # Force integer ticks on both axes ax.xaxis.set_major_locator(MaxNLocator(integer=True)) ax.yaxis.set_major_locator(MaxNLocator(integer=True)) plt.show()2. Using
MultipleLocatorfrom matplotlib.ticker import MultipleLocator ax.xaxis.set_major_locator(MultipleLocator(1)) # Set ticks every 1 unit ax.yaxis.set_major_locator(MultipleLocator(1))3. Setting tick values manually
ax.set_xticks([1, 2, 3, 4]) # Explicitly set whole number ticks ax.set_yticks([0, 5, 10, 15, 20])4. For 3D plots
from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d') # Set integer ticks for all three axes ax.xaxis.set_major_locator(MaxNLocator(integer=True)) ax.yaxis.set_major_locator(MaxNLocator(integer=True)) ax.zaxis.set_major_locator(MaxNLocator(integer=True))The
MaxNLocatorapproach is generally the most flexible as it automatically determines appropriate whole number tick positions based on your data range.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.