Чтобы отображать только целочисленные значения на графике matplotlib, вы можете использовать следующие методы:
-
Использование функций
plt.xticksиplt.yticks:import matplotlib.pyplot as plt # Set the x-axis and y-axis tick locations as integers plt.xticks(range(min_value, max_value+1, 1), [int(x) for x in range(min_value, max_value+1, 1)]) plt.yticks(range(min_value, max_value+1, 1), [int(y) for y in range(min_value, max_value+1, 1)]) # Plot your data plt.plot(x_data, y_data) # Display the plot plt.show() -
Использование функции
plt.locator_params:import matplotlib.pyplot as plt from matplotlib.ticker import MaxNLocator fig, ax = plt.subplots() # Set the x-axis and y-axis tick locations as integers ax.xaxis.set_major_locator(MaxNLocator(integer=True)) ax.yaxis.set_major_locator(MaxNLocator(integer=True)) # Plot your data ax.plot(x_data, y_data) # Display the plot plt.show() -
Использование функции
plt.gca:import matplotlib.pyplot as plt fig, ax = plt.subplots() # Set the x-axis and y-axis tick locations as integers ax.xaxis.set_major_locator(plt.MaxNLocator(integer=True)) ax.yaxis.set_major_locator(plt.MaxNLocator(integer=True)) # Plot your data ax.plot(x_data, y_data) # Display the plot plt.show()
Эти методы гарантируют, что отметки по осям X и Y отображаются как целочисленные значения. Вы можете выбрать метод, который лучше всего соответствует вашим потребностям.