Python 如果条颜色等于紫色,如何在条内插入文本
所以我有这样的图像: 如果将“H37Rv”仅保留在紫色条中,我将尝试做什么 我的代码如下:Python 如果条颜色等于紫色,如何在条内插入文本,python,matplotlib,Python,Matplotlib,所以我有这样的图像: 如果将“H37Rv”仅保留在紫色条中,我将尝试做什么 我的代码如下: rects = ax.bar(ind, num, width, color=colors) for rect in rects: height = int(rect.get_height()) if height < 5: yloc = height + 2 clr = '#182866'
rects = ax.bar(ind, num, width, color=colors)
for rect in rects:
height = int(rect.get_height())
if height < 5:
yloc = height + 2
clr = '#182866'
else:
yloc = height / 2.0
clr = '#182866'
p = 'H37Rv'
xloc = rect.get_x() + (rect.get_width() / 2.0)
ax.text(xloc, yloc, p, horizontalalignment='center', verticalalignment='center', color=clr, weight='bold')
rects=ax.bar(ind,num,width,color=colors)
对于矩形中的矩形:
height=int(rect.get_height())
如果高度<5:
yloc=高度+2
clr='#182866'
其他:
yloc=高度/2.0
clr='#182866'
p='H37Rv'
xloc=rect.get_x()+(rect.get_width()/2.0)
ax.text(xloc,yloc,p,水平对齐='center',垂直对齐='center',颜色=clr,权重='bold')
我也试过:
for rect in rects:
if color == purple:
height = int(rect.get_height())
if height < 5:
yloc = height + 2
clr = '#182866'
对于矩形中的矩形:
如果颜色=紫色:
height=int(rect.get_height())
如果高度<5:
yloc=高度+2
clr='#182866'
但是我得到一个错误,说颜色没有定义
有人知道怎么解决这个问题吗
非常感谢 如果您将第一个示例的最后三行移动到一个缩进级别,因此它们是将颜色设置为紫色的“else”子句的一部分,那么应该这样做 [编辑:抱歉,我有点误读。这也会将文本保留在第二栏中。据我所知,无法获得矩形的颜色,但您可以:
rects = ax.bar(ind, num, width, color=colors)
rect = rects[-1]
height = int(rect.get_height())
if height < 5:
yloc = height + 2
else:
yloc = height / 2.0
clr = '#182866'
p = 'H37Rv'
xloc = rect.get_x() + (rect.get_width() / 2.0)
ax.text(xloc, yloc, p, horizontalalignment='center', verticalalignment='center', color=clr, weight='bold')
rects=ax.bar(ind,num,width,color=colors)
rect=rects[-1]
height=int(rect.get_height())
如果高度<5:
yloc=高度+2
其他:
yloc=高度/2.0
clr='#182866'
p='H37Rv'
xloc=rect.get_x()+(rect.get_width()/2.0)
ax.text(xloc,yloc,p,水平对齐='center',垂直对齐='center',颜色=clr,权重='bold')
这将只在最后一个栏中设置文本
如果它可能是紫色的任何条,不一定是最后一条,那么,您已经有了初始化矩形的颜色列表,因此:
rects = ax.bar(ind, num, width, color=colors)
for i in range(len(colors):
if colors[i] == purple: # or however you specified "purple" in your colors list
labelled_rects.append(i)
for i in labelled_rects:
rect = rects[i]
height = int(rect.get_height())
if height < 5:
yloc = height + 2
else:
yloc = height / 2.0
clr = '#182866'
p = 'H37Rv'
xloc = rect.get_x() + (rect.get_width() / 2.0)
ax.text(xloc, yloc, p, horizontalalignment='center', verticalalignment='center', color=clr, weight='bold')
rects=ax.bar(ind,num,width,color=colors)
对于范围内的i(透镜(颜色):
如果颜色[i]==紫色:#或者您在颜色列表中指定了“紫色”
带标签的矩形追加(i)
对于带标签的矩形中的i:
rect=rects[i]
height=int(rect.get_height())
如果高度<5:
yloc=高度+2
其他:
yloc=高度/2.0
clr='#182866'
p='H37Rv'
xloc=rect.get_x()+(rect.get_width()/2.0)
ax.text(xloc,yloc,p,水平对齐='center',垂直对齐='center',颜色=clr,权重='bold')
如果将第一个示例的最后三行移动到一个缩进级别,因此它们是将颜色设置为紫色的“else”子句的一部分,则应该这样做
[编辑:抱歉,我有点误读。这也会将文本保留在第二栏中。据我所知,无法获得矩形的颜色,但您可以:
rects = ax.bar(ind, num, width, color=colors)
rect = rects[-1]
height = int(rect.get_height())
if height < 5:
yloc = height + 2
else:
yloc = height / 2.0
clr = '#182866'
p = 'H37Rv'
xloc = rect.get_x() + (rect.get_width() / 2.0)
ax.text(xloc, yloc, p, horizontalalignment='center', verticalalignment='center', color=clr, weight='bold')
rects=ax.bar(ind,num,width,color=colors)
rect=rects[-1]
height=int(rect.get_height())
如果高度<5:
yloc=高度+2
其他:
yloc=高度/2.0
clr='#182866'
p='H37Rv'
xloc=rect.get_x()+(rect.get_width()/2.0)
ax.text(xloc,yloc,p,水平对齐='center',垂直对齐='center',颜色=clr,权重='bold')
这将只在最后一个栏中设置文本
如果它可能是紫色的任何条,不一定是最后一条,那么,您已经有了初始化矩形的颜色列表,因此:
rects = ax.bar(ind, num, width, color=colors)
for i in range(len(colors):
if colors[i] == purple: # or however you specified "purple" in your colors list
labelled_rects.append(i)
for i in labelled_rects:
rect = rects[i]
height = int(rect.get_height())
if height < 5:
yloc = height + 2
else:
yloc = height / 2.0
clr = '#182866'
p = 'H37Rv'
xloc = rect.get_x() + (rect.get_width() / 2.0)
ax.text(xloc, yloc, p, horizontalalignment='center', verticalalignment='center', color=clr, weight='bold')
rects=ax.bar(ind,num,width,color=colors)
对于范围内的i(透镜(颜色):
如果颜色[i]==紫色:#或者您在颜色列表中指定了“紫色”
带标签的矩形追加(i)
对于带标签的矩形中的i:
rect=rects[i]
height=int(rect.get_height())
如果高度<5:
yloc=高度+2
其他:
yloc=高度/2.0
clr='#182866'
p='H37Rv'
xloc=rect.get_x()+(rect.get_width()/2.0)
ax.text(xloc,yloc,p,水平对齐='center',垂直对齐='center',颜色=clr,权重='bold')
您可以使用rect.get\u facecolor()
获得矩形的颜色,这允许您按所需方式放置标签
或者,由于您知道绘制条形图时使用的颜色,并且如果它们由列表表示,您确实可以轻松获得紫色矩形的列表。您可以使用
rect.get\u facecolor()
获得矩形的颜色,这使您可以按所需方式放置标签
或者,由于您知道绘制条形图时使用的颜色,并且如果它们由列表表示,您确实可以轻松获得紫色矩形列表。在本例中,我没有将颜色设置为紫色。如果您谈论的是:clr='#182866',那么这就是我们在字母中看到的深蓝色,实际上它是e相同。紫色在一个颜色列表中,因为它是一个根据请求而变化的图形。我不确定我是否足够清楚。谢谢!我试图用@EOL示例做类似的事情,但你的示例也很简单。:)在这个例子中,我没有将颜色设置为紫色。如果你说的是:clr='#182866',那么这是我们在字母中看到的深蓝色,实际上是一样的。紫色在一个颜色列表中,因为它是一个根据请求而变化的图形。我不确定我是否足够清楚。谢谢!我正在尝试做类似的事情使用@EOL示例,但您的示例也很简单。:)谢谢!我正在寻找类似的内容,但是@Vicky和您的建议也起作用:)谢谢!我正在寻找类似的内容,但是@Vicky和您的建议也起作用:)