gpt4 book ai didi

python - 如何在条形图顶部添加一条线?

转载 作者:行者123 更新时间:2023-11-28 16:24:03 25 4
gpt4 key购买 nike

我有一个条形图,我想在其上绘制两个坐标之间的线段(即在同一图中)。我尝试添加另一个 plt 语句,但未绘制线条。

plt.figure(figsize=(12, 10))
ax = freq_series.plot(kind='bar',color=colors)
plt.plot([coord_x1,coord_y1], [coord_x2, coord_y2], "r--")

最佳答案

更新:

import datetime
import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.style.use('ggplot')

d = {
datetime.date(2015, 6, 21): 101.0,
datetime.date(2015, 6, 22): 81.0,
datetime.date(2015, 6, 23): 94.0,
datetime.date(2015, 4, 24): 67.5,
datetime.date(2015, 6, 26): 99.1
}

df = pd.DataFrame({'date': [x for x in d.keys()], 'val':[x for x in d.values()]})

%matplotlib

ax = df.set_index('date').plot.bar(rot=0, figsize=(12, 10))

x = [ax.patches[0].get_x(), ax.patches[-1].get_x() + ax.patches[-1].get_width()]
y = [df.val.max()] * 2

plt.plot(x, y, 'r--', c='g', linewidth=4)

#plt.plot([ax.patches[0].get_width(), ax.patches[-1].get_width()], [y,y], 'r--', c='k')

enter image description here

旧答案:

这是一个小演示:

import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.style.use('ggplot')

df = pd.DataFrame(np.random.randint(0, 10, size=(5)), columns=list('a'))

ax = df.plot.bar(figsize=(12, 10))

coord_x1 = 0.5
coord_y1 = 7.5

coord_x2 = 4.5
coord_y2 = 7.5

plt.plot([coord_x1, coord_x2], [coord_y1, coord_y1], '-o')

enter image description here

关于python - 如何在条形图顶部添加一条线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38017465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com