如何保存交互式Matplotlib图形

2024-04-25 23:34:10 发布

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

如何将交互式Matplotlib图形保存到excel或仅保存为图形?我只想与非技术用户分享。但是,我可以使用Plotly和共享链接来实现这一点,但由于数据隐私问题,他们不喜欢这种方法。我有什么办法可以做到这一点吗

我有下面这样的互动图片。有些是叠在一起的。所以我需要缩放,这就是为什么我需要以互动的方式保存图像。然后,非技术用户可以放大并检查结果

import pandas as pd

import matplotlib
import mplcursors

data = [['Alex',10000,15000,6705],['Bob',12000,11050,7050],['Clarke',10300,10450,9050],['Alan',10100,15500,6205],['Sam',10300,15050,6785]]
df = pd.DataFrame(data,columns=['Name','Year2019','Year2020','Year2021'])
dfLen = len(df)

x =df['Year2020']
if dfLen > 0:
    %matplotlib notebook
    %matplotlib notebook  
    import matplotlib.pyplot as plt
    fig , ax = plt.subplots(figsize=(8,6))
    a = df['Year2019']/100
    # Create a scatter plot (Use s - make the size of each vendors usage)
    plt.scatter('Year2019', 'Year2020',s= x , c='Year2019', data=df)
    plt.title("2019 Vs 2020 sold items",fontsize=12)
    plt.xlabel('2019', fontsize=10)
    plt.ylabel('2020',fontsize=10)
    list1 = list(df['Name'])
    i = 0;              
    for row in df.itertuples():
        h = list1[i]
        i=i+1
        h = str(h)
        c = row.Year2019
        d = row.Year2020
        ax.text(c,d,s = h, size = 8)
    crs = mplcursors.cursor(ax,hover=True)
    crs.connect("add", lambda sel: sel.annotation.set_text(
        'Point x - {} ,\n'
               'y- {}'.format(sel.target[0], sel.target[1])))
    plt.show()

Tags: 用户import图形dfdatamatplotlibasplt