pandas-ai
pandas-ai copied to clipboard
Multiple graphs overlapping
🐛 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
line of code is like this: response = pandas_ai.run(analytics_df, prompt=f"{inputQuestion}, as png image with filename as {timestamp}2.png")
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.
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
Can you please share the log? Also is this happening with successive prompts or only when one prompt results in multiple charts?
First time graph is giving results fine:
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:
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
this basically happens while using it in a API.
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.
I call the code below
plt.show()
plt.close("all")
everytime after i call pandaai's api to resolve this problem
So @wqh17101 you expect that running plt.close("all")
everytime after plt.show()
should fix it, right?
@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.
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)
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 i think you maybe you can keep this issue open to make pandasai @gventuri @jonbiemond add this feature by default.
@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?
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?
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 now it should add close('all')
automatically (we are gonna release the new version with the changes soon)!