Home:ALL Converter>Using format with multiple objects

Using format with multiple objects

Ask Time:2021-09-22T05:05:30         Author:user16233760

Json Formatter

This is a code from a LinkedIn post, there is one line starting at comments.append that is not shown fully (I put ???). I was wondering if anyone know how to solve it.

price = data["Adj Close"].loc["2020-03-02":"2020-12-21"]
fig.ax = plt.subplots(figsize=(20,5))
comments = []
for ticker in tickers:
    returns = price.apply(lambda x:np.log(x / x.shift(1))).dropna()
    title="{} . cumulative log return".format(ticker)
    ax.plot(returns.index,returns[ticker].cumsum(), label=ticker)
    final_cum_log_return = returns[ticker].cumsum().iloc[-1]
    exp_final_cum_log_return = (np.exp(final_cum_log_return)-1)*100
    comments.append("{2}: Cum log return = {0}.\nSimple return = (exp({0}) -1)*100= {1} %.\n".format(final_cum_log_return ???))
plt.legend()
plt.show()
print("On Dec. 21:\n")
[print(x) for x in comments]

The only way I know how to change it fixes most of the problem, but my code shows the following error "No handles with labels found to put in legend". And the result is not as clean as the one in the LinkedIn post.

    comments.append("{tickers}: Cum log return = {returns}.\nSimple return = (exp({returns}) -1)*100= {exp_final_cum_log_return} %.\n".format(tickers=tickers,returns=returns,exp_final_cum_log_return=exp_final_cum_log_return))

The author's result

My result

https://www.linkedin.com/pulse/stock-price-analysis-python-anthony-skolozdrzyk-ardouin/

Author:user16233760,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/69275550/using-format-with-multiple-objects
yy