【问题标题】:Python: Highlighting, marking or indicating point in (scatter) plotPython:在(散点图)图中突出显示、标记或指示点
【发布时间】:2019-12-03 21:49:40
【问题描述】:

更新

尝试了更多,我设法运行此代码而没有错误:

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}
)

现在可以使用 YaY,任何人都可以阐明这个问题吗?我不太清楚为什么它突然开始工作。谢谢 :) 另外,我如何简单地标记一个点,而不是使用箭头?

问题:无法弄清楚如何在我的散点图中标记/选择/突出显示特定点

(Python 3 初学者)

所以我的目标是在散点图中突出显示一个或多个点,其中包含一些文本或由图例提供。

https://imgur.com/a/VWeO1EH

(没有足够的声誉来发布图片,抱歉)

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")

被抑制的“plt.annotate”会给出如下所示的错误。

具体来说,我想选择锐化比率,但现在如果我设法选择散点图中的任何点,我很高兴。

我真的很困惑如何使用 matplotlib,所以欢迎任何帮助

我尝试了以下在网上找到的解决方案:

我) 这显示了一种在绘图中使用注释的简单方法,通过箭头标记特定点。 https://www.youtube.com/watch?v=ItHDZEE5wSk

但是 pd.dataframe 环境不喜欢注释,我得到了错误:

TypeError: 'DataFrame' object is not callable

二) 由于我在数据框环境中遇到了注释问题,因此我查看了以下解决方案

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)


但是,除了这个非常长的水平滚动条之外,结果图在应用于我的问题时没有显示任何注释

https://imgur.com/a/O8ykmeg

III) 此外,我偶然发现了这个解决方案,使用标记而不是箭头,

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()

然而,当应用到我的情况时,代码完全没有任何作用

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()

四) 最后,我尝试了一个解决方案,该解决方案显然与 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)
)

但是运行此代码会产生与上述相同的错误:

TypeError: 'DataFrame' object is not callable

版本:

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

对 WoT 感到抱歉,但我认为最好将我尝试的所有内容都放在一篇文章中。 感谢您的帮助,谢谢!

【问题讨论】:

标签: python dataframe matplotlib


【解决方案1】:

我认为一种解决方案如下,如上面发布的“更新”:

更新

尝试了更多,我成功地运行了这段代码而没有错误:

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}
)

还有一个问题,我如何使用不同的标记或颜色并在图例中写下它?

提前致谢:)

【讨论】:

    猜你喜欢
    • 2016-11-25
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多