Sinaptik-AI / pandas-ai

Chat with your database (SQL, CSV, pandas, polars, mongodb, noSQL, etc). PandasAI makes data analysis conversational using LLMs (GPT 3.5 / 4, Anthropic, VertexAI) and RAG.
https://pandas-ai.com
Other
11.69k stars 1.08k forks source link

DIsplay japanese characters in the plot image #1182

Open Tmpts21 opened 1 month ago

Tmpts21 commented 1 month ago

System Info

pandas 2.2.0 , python 3.11

🐛 Describe the bug

I am trying to display japanese characters in the generated plot results but it doesn't display the characters correctly.

Here is my sample query : plot the top 5 pokemon based on attack. change the label for Y axis to "攻撃" and "名前" for X axis.

plot result : image

Here is the last_executed_code method result : df = dfs[0] description = df.describe() top_attackers = df.nlargest(5, 'Attack') plt.figure(figsize=(10, 6)) plt.barh(top_attackers['Name'], top_attackers['Attack']) plt.xlabel('名前') plt.ylabel('攻撃') plt.title('Top 5 Pokémon by Attack') plt.savefig('C:/Users/P102NRPH1/Desktop/generative-ai/demo-data-analysis/exports/charts/temp_chart.png') result = {'type': 'plot', 'value': 'C:/Users/P102NRPH1/Desktop/generative-ai/demo-data-analysis/exports/charts/temp_chart.png'}

Here is how I create the smartdataframe instance : df = SmartDataframe(st.session_state.context, config={"llm": llm })

How can I resolve this? Is there a configuration I can add so that pandas ai can display these type of characters? Thanks for the help !!

dosubot[bot] commented 1 month ago

Hello @Tmpts21! I'm here to help you with any bugs, questions, or contributions you have. Let's work together to resolve this issue!

To display Japanese characters correctly in your plot, you can set the font to one that supports Japanese characters, such as 'SimHei' or another appropriate font. You can configure matplotlib to use this font before generating the plot. Here is an example of how you can modify your code to include this configuration:

import matplotlib.pyplot as plt

# Set the font for matplotlib
plt.rcParams['font.sans-serif'] = ['SimHei']  # Use a font that supports Japanese characters
plt.rcParams['axes.unicode_minus'] = False  # Ensure the minus sign is displayed correctly

df = dfs[0]
description = df.describe()
top_attackers = df.nlargest(5, 'Attack')
plt.figure(figsize=(10, 6))
plt.barh(top_attackers['Name'], top_attackers['Attack'])
plt.xlabel('名前')
plt.ylabel('攻撃')
plt.title('Top 5 Pokémon by Attack')
plt.savefig('C:/Users/P102NRPH1/Desktop/generative-ai/demo-data-analysis/exports/charts/temp_chart.png')
result = {'type': 'plot', 'value': 'C:/Users/P102NRPH1/Desktop/generative-ai/demo-data-analysis/exports/charts/temp_chart.png'}

Additionally, ensure you are using the recommended version of matplotlib for compatibility with pandas-ai:

matplotlib = "^3.7.1"

This should help in rendering the Japanese characters correctly in your plot [1][2].

#### Details **About Dosu** This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.