python 如何在海运中绘制连接条形图顶点的直线

yftpprvb  于 2023-05-21  发布在  Python
关注(0)|答案(1)|浏览(95)

我有一个成组的条形图,并希望将条形图的顶部与一种趋势线联系起来。
在图片上我结合了一个条形图和一个点图,但它并没有导致预期的结果。

ax_line = fig_line.subplots()

ax_line = sns.barplot(x="Number of Agents", y="Duration",data=df, palette="Set1", ci=None, hue="Szenario")
ax_line = sns.pointplot(x="Anzahl Agenten", y="self.total_duration",hue="Szenario", data=df,palette="Set1", ci=None)

ax_line.set_ylabel("Duration",fontsize=15)
ax_line.set_xlabel("Number of Agents", fontsize=15)

有人有主意吗?不幸的是,我仍然是这个领域的初学者:D

v6ylcynt

v6ylcynt1#

虽然有点晚,但也许将来有人会发现它很有用:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

randoms = np.sort(np.random.randint(100, size=30))
df = pd.DataFrame(randoms.reshape(-1, 3), columns=['A', 'B', 'C'])

hue = len(df.columns)
bar_idxs = np.arange(len(df)*(hue+1))
bar_idxs[::hue+1]

plt.figure(figsize=(10, 4))
for i, col in enumerate(df.columns):
    hue_idxs = bar_idxs[::hue+1] + i
    plt.bar(hue_idxs, df[col], width = 1, alpha=0.9)
    plt.plot(hue_idxs, df[col], '-o', label='line with marker')
plt.show()

结果:

相关问题