首页 > 解决方案 > 在pyplot中的乳胶代码中迭代格式化值

问题描述

抱歉,如果已经问过这个问题,但我找不到任何相关信息。在 pyplot 中,我想知道是否可以一段乳胶代码中从 for 循环中格式化值?例如,我希望使用类似的东西

import matplotlib.pyplot as plt

for k in range(len(b)):

    plt.plot(a, b)
    plt.ylabel(r'$\sqrt{| R(a, {}) |}$'.format(k))
    plt.close()

这将返回 sqrt(| R(a, 1) |)、sqrt(| R(a, 1) |) 等作为每张新图片上的乳胶形式的 y 标签。

标签: pythonmatplotlib

解决方案


是的,有可能:

import matplotlib.pyplot as plt

for k in range(len(b)):
    plt.plot(a, b)
    plt.ylabel(r'$\sqrt{| R(a, {%s}) |}$' % k)
    plt.show()

输出

在此处输入图像描述


推荐阅读