在 Python 中的一个图中的停留图 [英] layover plots in one figure in Python

查看:83
本文介绍了在 Python 中的一个图中的停留图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以像这样在 1 个图中停留两条曲线

I can layover two curves in 1 plot like so

X = np.array([1, 5, 8])
y = np.array([2, 10, 3])

x_max = np.array([5])
y_max = np.array([10])

fig, ax = plt.subplots(figsize=(8,6));

ax.plot(X, y, 'k--', label="savitzky")
ax.scatter(x_max, y_max, s=200, c='k', marker='*');

然后我会得到以下内容:

Then I will get the following:

假设我有一个数据框,我想一次绘制它的所有列.我可以这样:

Lets say I have a data frame and I want to plot all of its columns at once. I can do that like so:

df_2 = pd.DataFrame(data = {'col_1':np.array([2, 10, 3]), 'col_2':np.array([3, 4, 7])}, 
                index = np.array([1, 5, 8]))
df_2.plot()

获得:

我的问题是如何将两者结合起来,以便立即绘制整个数据框然后放在我的最大点向量上?(我的实际数据帧比这个大,因此最大向量也是如此)

My question is how can I combine these two so I can plot the whole dataframe at once and then lay over my vectors of maximum points?(my real data frame is bigger than this, and so are the vectors of maximums)

谢谢

推荐答案

以下是一种方法:

  1. 创建轴对象 ax
  2. 在此轴上绘制 DataFrame
  3. 获取每列的最大元素和对应的索引
  4. 在同一个轴上做散点图ax

<小时>

fig, ax = plt.subplots()

df_2 = pd.DataFrame(data = {'col_1':np.array([2, 10, 3]), 
                            'col_2':np.array([3, 4, 7])}, 
                            index = np.array([1, 5, 8]))
df_2.plot(ax=ax) # Plot the DataFrame on ax object

max_points = [(df_2[col].idxmax(),  df_2[col].max()) for col in df_2.columns]

ax.plot(*zip(*max_points), 'b*', ms=10) # Unpack the list of (x, y) tuples
ax.set_xlim(None, 8.2)

这篇关于在 Python 中的一个图中的停留图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆