pandas-ai icon indicating copy to clipboard operation
pandas-ai copied to clipboard

Multiple graphs overlapping

Open vpurandara opened this issue 1 year ago • 1 comments

🐛 Describe the bug

version: pandasai==0.3.0

while saving graphs as png image in an API, first image is saving fine but from second images they are ovelaping with past images image

line of code is like this: response = pandas_ai.run(analytics_df, prompt=f"{inputQuestion}, as png image with filename as {timestamp}2.png")

vpurandara avatar Jun 05 '23 13:06 vpurandara

Are you using the save_charts feature? Charts can be saved by setting save_charts to True during the inital call of PandasAI. e.g. PandasAI(llm, save_charts=True) This will give each chart a unique name.

jonbiemond avatar Jun 05 '23 14:06 jonbiemond

I'm using PandasAI(llm, save_charts=True) as well. My issue is not with saving the chart, except first result every other result graph is overlapping with previous graph

vpurandara avatar Jun 05 '23 19:06 vpurandara

Can you please share the log? Also is this happening with successive prompts or only when one prompt results in multiple charts?

jonbiemond avatar Jun 05 '23 19:06 jonbiemond

First time graph is giving results fine:

image

Running PandasAI with openai LLM...

                Code generated:
                ```
                import pandas as pd

import matplotlib.pyplot as plt

read the dataframe from a csv file

df = pd.read_csv('filename.csv')

plot a bar graph of lead's Sub Source

df['Sub Source'].value_counts().plot(kind='bar')

set the title and labels for the graph

plt.title("Lead's Sub Source") plt.xlabel('Sub Source') plt.ylabel('Count')

save the graph as a png image with filename as 20230606_1220352.png

plt.savefig('20230606_1220352.png', dpi=300, bbox_inches='tight') ```

Code running:

df['Sub Source'].value_counts().plot(kind='bar')
plt.title("Lead's Sub Source")
plt.xlabel('Sub Source')
plt.ylabel('Count')
plt.savefig('20230606_1220352.png', dpi=300, bbox_inches='tight')

Answer: None

but from second api call onwards the output is overlapping:

image

Running PandasAI with openai LLM...

                Code generated:
                ```
                import pandas as pd

import matplotlib.pyplot as plt

read the dataframe

df = pd.read_csv('data.csv')

drop rows with missing values in Sub Source column

df = df.dropna(subset=['Sub Source'])

count the number of leads for each Sub Source

sub_source_counts = df['Sub Source'].value_counts()

plot a pie chart

plt.pie(sub_source_counts, labels=sub_source_counts.index) plt.title("Leads by Sub Source") plt.savefig('20230606_1221432.png') ```

Code running:

sub_source_counts = df['Sub Source'].value_counts()
plt.pie(sub_source_counts, labels=sub_source_counts.index)
plt.title('Leads by Sub Source')
plt.savefig('20230606_1221432.png')

Answer: None

vpurandara avatar Jun 06 '23 06:06 vpurandara

this basically happens while using it in a API.

vpurandara avatar Jun 06 '23 10:06 vpurandara

The reason the charts are overlapping is because the figures are not getting cleared between prompts. They are typically cleared with plt.show(). Also note that the save_charts feature currently requires a call to plt.show() to recognize a chart is being displayed. I suggest you do not include instructions to save the chart in your prompt, instead set save_charts=True and PandasAI will neatly handle that for you.

jonbiemond avatar Jun 06 '23 12:06 jonbiemond

I call the code below

        plt.show()
        plt.close("all")

everytime after i call pandaai's api to resolve this problem

wqh17101 avatar Jun 08 '23 08:06 wqh17101

So @wqh17101 you expect that running plt.close("all") everytime after plt.show() should fix it, right?

gventuri avatar Jun 08 '23 10:06 gventuri

@gventuri Yeah, i am not sure whether you add plt.show() when you plot. So i think that after you save picture, you can trigger it.

wqh17101 avatar Jun 08 '23 11:06 wqh17101

In my experience just adding plt.show() should resolve this. A solution then is to always add plt.show() whenever there's a call to a plotting library. Are there situations where someone would not want plt.show() called? I also see this being difficult with multiple plots. (Could hand it off to the LLM in that case)

jonbiemond avatar Jun 08 '23 14:06 jonbiemond

The issue is resolved, if I add plt.close() after every API request:

llm = OpenAI(api_token=OpenAI_ACCESSKEY) pandas_ai = PandasAI(llm, save_charts=True)

response = pandas_ai.run(analytics_df, prompt=f"{inputQuestion}, as png image with filename as {timestamp}2.png")
plt.close()

Thanks to @jonbiemond @wqh17101 @gventuri

vpurandara avatar Jun 09 '23 06:06 vpurandara

@vpurandara i think you maybe you can keep this issue open to make pandasai @gventuri @jonbiemond add this feature by default.

wqh17101 avatar Jun 09 '23 07:06 wqh17101

@vpurandara you shouldn't need to add plt.close()

I suggest you do not include instructions to save the chart in your prompt, instead set save_charts=True and PandasAI will neatly handle that for you.

Have you tried this?

jonbiemond avatar Jun 09 '23 13:06 jonbiemond

A solution then is to always add plt.show() whenever there's a call to a plotting library. Are there situations where someone would not want plt.show() called?

@wqh17101 Any thoughts on this?

jonbiemond avatar Jun 09 '23 13:06 jonbiemond

i think use plt.close(all) is the best choice. Because it means close all the figures. i don't know whether plt.show will always work in this scene.

One more thing is that is it possible for user to use their own figure and pandasai together? For pandasai exec the generative code in the main process,all the codes will affect the main process context. Maybe we should think about this or tell users to try to avoid using their own plt with pandasai together?

wqh17101 avatar Jun 10 '23 05:06 wqh17101

@wqh17101 now it should add close('all') automatically (we are gonna release the new version with the changes soon)!

gventuri avatar Jun 10 '23 22:06 gventuri