根据排序值标准添加要打印的水平线

2024-10-03 17:16:08 发布

您现在位置:Python中文网/ 问答频道 /正文

问题:

如何根据在top_5变量中捕获的下面指定的sort_values条件向plot添加水平线:

数据:

以下是CSV中data的一部分:

这是电流图。在

axnum = today_numBars_slice[['High','Low']].plot()
axnum.yaxis.set_major_formatter(FormatStrFormatter('%.2f'))

enter image description here

这是我要添加到此绘图中的数据(每行的HighLow值):

^{pr2}$

期望输出:

这是所需输出的一个示例,其中显示了来自顶部5的两行:

enter image description here


Tags: csv数据datatodayplottop条件sort
2条回答

你要找的是^{}吗?在

axnum = today_numBars_slice[['High','Low']].plot()
axnum.yaxis.set_major_formatter(FormatStrFormatter('%.2f'))

top_5 = today_numBars_slice[['High','Low','# of Trades']].sort_values(by='# of Trades',ascending=False).head()

for l in top_5.iterrows():
    plt.axhline(l['high'], color='r')
    plt.axhline(l['low'], color='b')

plt.show();

您可以将更快的^{}用于顶部5行,然后使用^{}和{a3}:

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

df = pd.read_csv('for_stack_nums')
#print (df.head())

top_5 = df[['High','Low','# of Trades']].nlargest(5, '# of Trades')
print (top_5)
      High     Low  # of Trades
94  164.88  164.84          470
90  164.90  164.86          465
93  164.90  164.86          431
89  164.87  164.83          427
65  164.60  164.56          332

axnum = df[['High','Low']].plot()
axnum.yaxis.set_major_formatter(ticker.FormatStrFormatter('%.2f')) 

for idx, l in top_5.iterrows():
    plt.axhline(y=l['High'], color='r')
    plt.axhline(y=l['Low'], color='b')
plt.show()

graph

也不需要子集:

^{pr2}$

相关问题 更多 >