从图或曲线的坐标中提取数据

时间:2019-03-22 08:32:49

标签: python matplotlib

有什么方法可以访问绘图下的数据吗?我正在使用数据游标,应用回调函数时唯一得到的就是图形的坐标。我想做的是当鼠标悬停在图形上时显示日期和印象数以及曲线注释或共享的名称或..

Impressions_Figure, Impressions_Graph = plt.subplots()
Impressions_Graph.tick_params(axis='x')
Impressions_Graph.tick_params(axis='y')
Impressions_Graph.set_ylabel('Number of Impressions')
Impressions_Graph.set_xlabel('Time')

a = records['Share_count']
b = records['Comment_count']
c = records['Likes']
d = records['Favorite_count']

try:
    reg = pd.to_datetime(records['Created_at'], format="%Y-%m-%d %H:%M:%S")

    data = pd.DataFrame()
    data["Shares"]=a
    data["Likes"]=c
    data["Comments"]=b
    data["Favourites"]=d
    data.index = reg
    data= data.fillna(0)

    data["Likes"]= data["Likes"].astype(float)
    data["Comments"]=data["Comments"].astype(float)
    data["Shares"]= data["Shares"].astype(float)
    data["Favourites"] = data["Favourites"].astype(float)

    data["Likes"] = StandardScaler().fit_transform(np.array(data["Likes"].values.tolist()).reshape(-1, 1))
    data["Comments"] = StandardScaler().fit_transform(np.array(data["Comments"].values.tolist()).reshape(-1, 1))
    data["Shares"] = StandardScaler().fit_transform(np.array(data["Shares"].values.tolist()).reshape(-1, 1))
    data["Favourites"] = StandardScaler().fit_transform(np.array(data["Favourites"].values.tolist()).reshape(-1, 1))

    Impressions_Graph =sns.lineplot(data=data, linewidth=2.5)
    try:
        Impressions_Graph.legend(bbox_to_anchor=(0.4,-0.1), ncol=5, frameon=True, shadow=True, title='KPIs')
        Impressions_Graph.autofmt_xdate()
    except:
        pass

except:
    try:
        reg = pd.to_datetime(dateRecords['Created_at'], format="%Y-%m-%d %H:%M:%S")

        data = pd.DataFrame()
        data["Shares"]=a
        data["Likes"]=c
        data["Comments"]=b
        data["Favourites"]=d
        data.index = reg
        data= data.fillna(0)

        data["Shares"] = StandardScaler().fit_transform(np.array(data["Shares"].values.tolist()).reshape(-1, 1))
        data["Likes"] = StandardScaler().fit_transform(np.array(data["Likes"].values.tolist()).reshape(-1, 1))
        data["Comments"] = StandardScaler().fit_transform(np.array(data["Comments"].values.tolist()).reshape(-1, 1))
        data["Favourites"] = StandardScaler().fit_transform(np.array(data["Favourites"].values.tolist()).reshape(-1, 1))

        Impressions_Graph =sns.lineplot(data=data, linewidth=2.5)
        Impressions_Graph.legend(bbox_to_anchor=(0.4,-0.1), ncol=5, frameon=True, shadow=True, title='KPIs')

    except:
        img = mpimg.imread("Images/Error/NO_IMPS.png")
        Impressions_Graph.imshow(img)

barCursor(Impressions_Graph)
Impressions_Graph.set_xlabel('Time')

Impressions_Figure.set_tight_layout(True)
Impressions_Figure.savefig("Report/data/Impressions.png")
Impressions_Figure.set_size_inches(5.5.3)

datacursor(Imperssions_Graph, xytext=(15, 0), bbox=dict(fc='orange', alpha=10), arrowprops=None, hover=True,
                 formatter='{x},{y}'.format, draggable=True)

the cruve look like this

0 个答案:

没有答案