Matplotlib:如何以摄氏度 (°C) 格式化温度

基于我们之前关于Matplotlib 自定义 SI 前缀单位刻度格式化器的文章,这是一个简单的代码片段,你可以使用它来格式化 matplotlib 绘图的 Y 轴:

celsius_formatter.py
import matplotlib.ticker as mtick
from matplotlib import pyplot as plt

def format_celsius(value, pos=None):
    return f'{value:.1f} °C'

plt.gca().yaxis.set_major_formatter(mtick.FuncFormatter(format_celsius))

Matplotlib plot with Y axis formatted in degrees Celsius using custom FuncFormatter


Check out similar posts by category: Python