-1

UPDATE

Trying some more, I managed to run this code without error:

from matplotlib.pyplot import figure

dict = pd.DataFrame({"Return": mkw_returns, "Standard Deviation": mkw_stds})
dict.head()
#plt.annotate("Sharpe Ratio", xytext=(0.5,0.5), xy=(0.03,0.03) ,  arrowprops=dict(facecolor='blue', shrink=0.01, width=220)) # arrowprops={width = 3, "facecolor":
#dict.plot(x="Standard Deviation", y = "Return", kind="scatter", figsize=(10,6))
#plt.xlabel("Standard Deviations")
#plt.ylabel("log_Return YoY")
figure(num=None, figsize=(15, 10), dpi=100, facecolor='w', edgecolor='k')
plt.plot( 'Standard Deviation', 'Return', data=dict, linestyle='none', marker='o')
plt.xlabel("Standard Deviations")
plt.ylabel("log_Return YoY")

    # Annotate with text + Arrow
plt.annotate(
# Label and coordinate
'This is a Test', xy=(0.01, 1), xytext=(0.01, 1), color= "r", arrowprops={"facecolor": 'black', "shrink": 0.05}
)

Which now works YaY, can anybody shed some light onto this issue? Im not so sure why it suddenly started working. Thank you :) Also, how would I simply mark a point, instead of using the arrow?

Problem: Cannot figure out how to mark/select/highlight a specific point in my scatter graph

(Python 3 Beginner)

So my goal is to highlight one or more points in a scatter graph with some text by it or supplied by a legend.

https://i.stack.imgur.com/NR2Jm.jpg

(not enough reputation to post images, sorry)

dict = pd.DataFrame({"Return": mkw_returns, "Standard Deviation": mkw_stds})
dict.head()
#plt.annotate("Sharpe Ratio", xytext=(0.5,0.5), xy=(0.03,0.03) ,  arrowprops=dict(facecolor='blue', shrink=0.01, width=220)) # arrowprops={width = 3, "facecolor":
dict.plot(x="Standard Deviation", y = "Return", kind="scatter", figsize=(10,6))
plt.xlabel("Standard Deviations")
plt.ylabel("log_Return YoY")

The supressed "plt.annotate" would give an error as specified below.

Specifically i would like to select the sharpe ratio, but for now Im happy if I manage to select any point in the scatter graph.

Im truly confused how to work with matplotlib, so any help is welcomed

I tried the following solutions I found online:

I) This shows a simple way to use annotate in a plot, to mark a specific point by an arrow. https://www.youtube.com/watch?v=ItHDZEE5wSk

However the pd.dataframe environment does not like annotate and i get the error:

TypeError: 'DataFrame' object is not callable

II) Since Im running into issues with annotate in a Data Frame environment, I looked at the following solution

Annotate data points while plotting from Pandas DataFrame

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

df = pd.DataFrame({'x':np.random.rand(10), 'y':np.random.rand(10)}, 
                  index=list(string.ascii_lowercase[:10]))
fig, ax = plt.subplots()
df.plot('x', 'y', kind='scatter', ax=ax, figsize=(10,6))

for k, v in df.iterrows():
    ax.annotate(k, v)


However the resulting plot does not show any annotation what so ever when applied to my problem, besides this very long horizontal scroll bar

https://i.stack.imgur.com/mmzVe.jpg

III) Further, I stumbled upon this solution, to use a marker instead of an arrow,

Matplotlib annotate with marker instead of arrow

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

x=[1,2,3,4,5,6,7,8,9,10]
y=[1,1,1,2,10,2,1,1,1,1]
line, = ax.plot(x, y)

ymax = max(y)
xpos = y.index(ymax)
xmax = x[xpos]

# Add dot and corresponding text
ax.plot(xmax, ymax, 'ro')
ax.text(xmax, ymax+2, 'local max:' + str(ymax))

ax.set_ylim(0,20)
plt.show()

however the code does absolutely nothing, when applied to my situation like so

dict = pd.DataFrame({"Return": mkw_returns, "Standard Deviation": mkw_stds})
dict.head()
plt.annotate("Sharpe Ratio", xytext=(0.5,0.5), xy=(0.03,0.03) ,  arrowprops=dict(facecolor='blue', shrink=0.01, width=220)) # arrowprops={width = 3, "facecolor":
dict.plot(x="Standard Deviation", y = "Return", kind="scatter", figsize=(10,6))
plt.xlabel("Standard Deviations")
plt.ylabel("log_Return YoY")


ymax = max(y)
xpos = y.index(ymax)

xmax = x[xpos]

# Add dot and corresponding text
ax.plot(xmax, ymax, 'ro')
ax.text(xmax, ymax+2, 'local max:' + str(ymax))

ax.set_ylim(0,20)
plt.show()

IV) Lastly, I tried a solution that apparently works flawlessly with an arrow in a pd.dataframe, https://python-graph-gallery.com/193-annotate-matplotlib-chart/

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

# Basic chart
df=pd.DataFrame({'x': range(1,101), 'y': np.random.randn(100)*15+range(1,101) })
plt.plot( 'x', 'y', data=df, linestyle='none', marker='o')

# Annotate with text + Arrow
plt.annotate(
# Label and coordinate
'This point is interesting!', xy=(25, 50), xytext=(0, 80),

# Custom arrow
arrowprops=dict(facecolor='black', shrink=0.05)
)

however running this code yields me the same error as above:

TypeError: 'DataFrame' object is not callable

Version:

import sys; print(sys.version)
3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)]

Sorry for the WoT, but I thought its best to have everything I tried together in one post. Any help is appreciated, thank you!

quh86
  • 23
  • 3
  • Case II) results in [this plot](https://i.stack.imgur.com/BInbi.png). So that works as expected. Case III) isn't reproducible (see [mcve]). Case IV) has a syntax error. – ImportanceOfBeingErnest Jul 25 '19 at 15:10

1 Answers1

0

I think one solution is the following, as posted above as the "UPDATE":

UPDATE

Trying some more, I managed to run this code without error:

from matplotlib.pyplot import figure

dict = pd.DataFrame({"Return": mkw_returns, "Standard Deviation": mkw_stds})
dict.head()
#plt.annotate("Sharpe Ratio", xytext=(0.5,0.5), xy=(0.03,0.03) ,  arrowprops=dict(facecolor='blue', shrink=0.01, width=220)) # arrowprops={width = 3, "facecolor":
#dict.plot(x="Standard Deviation", y = "Return", kind="scatter", figsize=(10,6))
#plt.xlabel("Standard Deviations")
#plt.ylabel("log_Return YoY")
figure(num=None, figsize=(15, 10), dpi=100, facecolor='w', edgecolor='k')
plt.plot( 'Standard Deviation', 'Return', data=dict, linestyle='none', marker='o')
plt.xlabel("Standard Deviations")
plt.ylabel("log_Return YoY")

    # Annotate with text + Arrow
plt.annotate(
# Label and coordinate
'This is a Test', xy=(0.01, 1), xytext=(0.01, 1), color= "r", arrowprops={"facecolor": 'black', "shrink": 0.05}
)

One question remains, how can I use a different marker or color and write about it in the legend instead?

Thanks in advance :)

quh86
  • 23
  • 3