【问题标题】:Drawing a rectangle representing a value in python?在python中绘制一个表示值的矩形?
【发布时间】:2016-09-22 08:26:27
【问题描述】:

如何在 python 中绘制一个颜色表示像这张图片一样的值的矩形?

我已经显示了一个矩形,但仍在尝试在矩形中显示值!

import matplotlib
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

fig = plt.figure()
ax = fig.add_subplot(111)
rect1 = matplotlib.patches.Rectangle((0,1), 5, 0.5, color='c')

ax.add_patch(rect1)
ax.grid()
plt.xlim([-5, 20])
plt.ylim([-5, 6])

plt.show()


someX, someY = 0.5, 0.5
plt.figure()
currentAxis = plt.gca()
currentAxis.add_patch(Rectangle((someX - .1, someY - .1), 0.2, 0.2, color = 'c', alpha=0.5))
plt.grid()
plt.show()

结果:

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    例如,您可以使用 matplotlib 库中的 text 函数。

    http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.text

    您还可以在此处的类似帖子中找到另一个解决方案:

    How to add a text into a Rectangle?

    【讨论】:

    • 太棒了!非常感谢!
    猜你喜欢
    • 2020-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多