简体   繁体   English

如何获取 matplotlib 条形图中的所有条形?

[英]How do I get all bars in a matplotlib bar chart?

It is easy to retrieve all lines in a line chart by calling the get_lines() function.通过调用get_lines()函数可以轻松检索折线图中的所有线条。 I cannot seem to find an equivalent function for a barchart, that is retrieving all Rectangle instances in the AxesSubplot .我似乎无法找到一个条形图同等功能,即检索所有矩形实例在AxesSubplot Suggestions?建议?

If you want all bars, just capture the output from the plotting method.如果您想要所有条形,只需捕获绘图方法的输出。 Its a list containing the bars:它是一个包含栏的列表:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

x = np.arange(5)
y = np.random.rand(5)

bars = ax.bar(x, y, color='grey')    
bars[3].set_color('g')

在此处输入图片说明

If you do want all Rectangle object in the axes, but these can be more then just bars, use:如果您确实需要坐标区中的所有 Rectangle 对象,但这些对象可能不仅仅是条形,请使用:

bars = [rect for rect in ax.get_children() if isinstance(rect, mpl.patches.Rectangle)]

Another option that might be useful to some people is to access ax.containers .另一个可能对某些人有用的选项是访问ax.containers You have to be a little careful though as if your plot contains other types of containers you'll get those back too.你必须小心一点,因为如果你的情节包含其他类型的容器,你也会得到它们。 To get just the bar containers something like只获得酒吧容器之类的东西

from matplotlib.container import BarContainer
bars = [i for i in ax.containers if isinstance(i, BarContainer)]

This can be pretty powerful with a few tricks (taking inspiration from the accepted example).这可以通过一些技巧非常强大(从公认的例子中获得灵感)。

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

x = np.arange(5)
y = np.random.rand(2, 5)

ax.bar(x, y[0], width=0.5)
ax.bar(x + 0.5, y[1], width=0.5)

for bar, color in zip(ax.containers, ("red", "green")):
    # plt.setp sets a property on all elements of the container
    plt.setp(bar, color=color)

will give you:会给你:

在此处输入图片说明

If you add some labels to your plots you can construct a dictionary of containers to access them by label如果您在绘图中添加一些标签,您可以构建一个容器字典以通过标签访问它们

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

x = np.arange(5)
y = np.random.rand(2, 5)

ax.bar(x, y[0], width=0.5)
ax.bar(x + 0.5, y[1], width=0.5, label='my bars')

named_bars = {i.get_label(): i for i in ax.containers}
plt.setp(named_bars["my bars"], color="magenta")

will give you会给你

在此处输入图片说明

Of course, you can still access an individual bar patch within a container eg当然,您仍然可以访问容器中的单个条形补丁,例如

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

x = np.arange(5)
y = np.random.rand(2, 5)

ax.bar(x, y[0], width=0.5)
ax.bar(x + 0.5, y[1], width=0.5)

plt.setp(ax.containers[0], color="black")
plt.setp(ax.containers[1], color="grey")
ax.containers[0][3].set_color("red")

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在matplotlib(python)中有一个带有误差条的条形图,但我希望误差条位于该条的中间。 我该怎么做呢? 谢谢 - I have a bar chart with error bars in matplotlib (python), but i want the error bars in the middle of the bar. how do i do this? Thanks 如何将第二个轴添加到 matplotlib/seaborn 条形图并使次要点与正确的条形对齐? - How do I add a second axis to a matplotlib/seaborn bar chart and have the secondary points align with the correct bars? 如何避免在matplotlib中的多条形图中的条形重叠 - How do I avoid overlap between bars in a multi-bar chart in matplotlib 如何使用 matplotlib 在此条形图的相应条形上方显示这些值? 我的尝试不起作用 - How do I display these values above their respective bars on this bar chart with matplotlib? My attempts are not working 如何确定matplotlib条形图中条形的顺序 - How to determine the order of bars in a matplotlib bar chart matplotlib当x轴相隔1周时,如何减少堆积条形图中条形之间的空间量? - matplotlib how do I reduce the amount of space between bars in a stacked bar chart when x-axis are dates 1-week apart? 如何为条形图中的不同条形提供十六进制颜色? - How do I give a hexadecimal color to different bars in a bar chart? 如何更改此条形图上条形的顺序? - How do I change the order of the bars on this bar chart? 如何在Pygal的堆积条形图中为条形添加值? - How do I add values to the bars in a Stacked Bar chart in Pygal? 如何在 matplotlib 日期条形图中的某个日期之后为所有条形着色 - How to colour all bars after a certain date in a matplotlib date bar chart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM