Python 3.x 如何使标签仅显示在特定条的上方

Python 3.x 如何使标签仅显示在特定条的上方,python-3.x,matplotlib,label,bar-chart,Python 3.x,Matplotlib,Label,Bar Chart,我想在此图表中的某些条形图上方添加一个自定义标签。在本例中,如何仅向栏4和栏8添加标签: import pandas as pd import matplotlib.pyplot as plt # Bring some raw data. frequencies = [6, 16, 75, 160, 244, 260, 145, 73, 16, 4, 1] # In my original code I create a series and run on that, # so for co

我想在此图表中的某些条形图上方添加一个自定义标签。在本例中,如何仅向栏4和栏8添加标签:

import pandas as pd
import matplotlib.pyplot as plt

# Bring some raw data.
frequencies = [6, 16, 75, 160, 244, 260, 145, 73, 16, 4, 1]
# In my original code I create a series and run on that, 
# so for consistency I create a series from the list.
freq_series = pd.Series.from_array(frequencies)

x_labels = [108300.0, 110540.0, 112780.0, 115020.0, 117260.0, 119500.0,
            121740.0, 123980.0, 126220.0, 128460.0, 130700.0]

# Plot the figure.
plt.figure(figsize=(12, 8))
ax = freq_series.plot(kind='bar')
ax.set_title('Amount Frequency')
ax.set_xlabel('Amount ($)')
ax.set_ylabel('Frequency')
ax.set_xticklabels(x_labels)

rects = ax.patches

# Make some labels.
labels = ["label%d" % i for i in xrange(len(rects))]

for rect, label in zip(rects, labels):
    height = rect.get_height()
    ax.text(rect.get_x() + rect.get_width() / 2, height + 5, label,
            ha='center', va='bottom')
通过这个代码,我在每个条上都有一个标签。但我只希望标签(例如,值为34和55)位于第四和第八条的上方

实现这一目标的最佳方式是什么


这里是我发现这个例子的地方:

这段代码是为Python2和一个更早版本的
pandas
编写的;为了在Python 3中工作,我对它做了一些修改

import pandas as pd
import matplotlib.pyplot as plt

# Bring some raw data.
frequencies = [6, 16, 75, 160, 244, 260, 145, 73, 16, 4, 1]
# In my original code I create a series and run on that, 
# so for consistency I create a series from the list.
freq_series = pd.Series.from_array(frequencies)

x_labels = [108300.0, 110540.0, 112780.0, 115020.0, 117260.0, 119500.0,
            121740.0, 123980.0, 126220.0, 128460.0, 130700.0]

# Plot the figure.
plt.figure(figsize=(12, 8))
ax = freq_series.plot(kind='bar')
ax.set_title('Amount Frequency')
ax.set_xlabel('Amount ($)')
ax.set_ylabel('Frequency')
ax.set_xticklabels(x_labels)

rects = ax.patches

# Make some labels.
labels = ["label%d" % i for i in xrange(len(rects))]

for rect, label in zip(rects, labels):
    height = rect.get_height()
    ax.text(rect.get_x() + rect.get_width() / 2, height + 5, label,
            ha='center', va='bottom')
还有其他方法可以做到这一点,但在不太改变代码结构的情况下,您可以指定要打印的条形标签的索引,如下所示,如
所示,并且仅当它们对应时才打印:

import pandas as pd
import matplotlib.pyplot as plt

# Bring some raw data.
frequencies = [6, 16, 75, 160, 244, 260, 145, 73, 16, 4, 1]
# In my original code I create a series and run on that, 
# so for consistency I create a series from the list.
freq_series = pd.Series(frequencies)

x_labels = [108300.0, 110540.0, 112780.0, 115020.0, 117260.0, 119500.0,
            121740.0, 123980.0, 126220.0, 128460.0, 130700.0]

# Plot the figure.
fig, ax = plt.subplots(figsize=(12, 8))
freq_series.plot(kind='bar', ax=ax)
ax.set_title('Amount Frequency')
ax.set_xlabel('Amount ($)')
ax.set_ylabel('Frequency')
ax.set_xticklabels(x_labels)

rects = ax.patches

# Make some labels.
labels = [f'label{i}' for i in range(len(rects))]

which = [3, 7]

for index, rect in enumerate(rects):
    if index in which:
        height = rect.get_height()
        ax.text(rect.get_x() + rect.get_width() / 2, height + 5, height,
                ha='center', va='bottom')

哦,好的,我试图编写一个for循环,当第四个元素为true(即num%4==0)时,该循环会遍历并找到,但我只能得到前三个条上的值。这对我来说更直观,谢谢你的帮助!