Matplotlib:如何以度 (°) 格式化角度
基于我们之前关于Matplotlib 自定义 SI 前缀单位刻度格式化器的文章,这是一个简单的代码片段,你可以使用它来格式化 matplotlib 绘图的 Y 轴。在我们的示例中,函数显示小数点后 2 位数字(.2f),但你可以将其更改为你喜欢的任意位数。
matplotlib-how-to-format-angle-in-degrees.py
import matplotlib.ticker as mtick
from matplotlib import pyplot as plt
def format_degrees(value, pos=None):
return f'{value:.2f} °'
plt.gca().yaxis.set_major_formatter(mtick.FuncFormatter(format_degrees))示例图
来自我们的文章如何在 Python 中使用 skyfield 计算和绘制太阳路径图
Check out similar posts by category:
Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow