Skip to content

Use ScaleFormatter for plot scaling #447

@mhostetter

Description

@mhostetter
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter

# Example data
x = [1, 2, 3, 4, 5]
y = [1000, 2000, 3000, 4000, 5000]

# Define a formatter function
def scale_formatter(value, tick_number):
    return f'{value / 1000:.1f}'

# Create the plot
fig, ax = plt.subplots()
ax.plot(x, y)

# Apply the formatter to the y-axis
ax.yaxis.set_major_formatter(FuncFormatter(scale_formatter))

# Add labels
ax.set_xlabel("X-axis")
ax.set_ylabel("Y-axis (scaled)")

plt.show()

Metadata

Metadata

Assignees

No one assigned

    Labels

    plotRelated to plotting functions

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions