Spaces:
Sleeping
Sleeping
SHAMIL SHAHBAZ AWAN
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -66,15 +66,27 @@ def generate_graph(data, query):
|
|
| 66 |
"""Generate a graph based on user query."""
|
| 67 |
try:
|
| 68 |
fig, ax = plt.subplots(figsize=(10, 6))
|
|
|
|
| 69 |
if "correlation" in query.lower():
|
|
|
|
| 70 |
sns.heatmap(data.corr(), annot=True, cmap="coolwarm", ax=ax)
|
| 71 |
st.pyplot(fig)
|
| 72 |
elif "histogram" in query.lower():
|
|
|
|
| 73 |
column = st.selectbox("Select a column for the histogram", data.columns)
|
| 74 |
sns.histplot(data[column], kde=True, ax=ax)
|
| 75 |
st.pyplot(fig)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
else:
|
| 77 |
-
st.error("Unsupported graph type. Try asking for a correlation matrix or
|
| 78 |
except Exception as e:
|
| 79 |
st.error(f"Error generating graph: {e}")
|
| 80 |
|
|
|
|
| 66 |
"""Generate a graph based on user query."""
|
| 67 |
try:
|
| 68 |
fig, ax = plt.subplots(figsize=(10, 6))
|
| 69 |
+
|
| 70 |
if "correlation" in query.lower():
|
| 71 |
+
# Correlation matrix
|
| 72 |
sns.heatmap(data.corr(), annot=True, cmap="coolwarm", ax=ax)
|
| 73 |
st.pyplot(fig)
|
| 74 |
elif "histogram" in query.lower():
|
| 75 |
+
# Histogram
|
| 76 |
column = st.selectbox("Select a column for the histogram", data.columns)
|
| 77 |
sns.histplot(data[column], kde=True, ax=ax)
|
| 78 |
st.pyplot(fig)
|
| 79 |
+
elif "bar" in query.lower() and "country" in query.lower():
|
| 80 |
+
# Bar chart for countries and units sold
|
| 81 |
+
if 'country' in data.columns and 'units sold' in data.columns:
|
| 82 |
+
country_data = data[['country', 'units sold']].groupby('country').sum().reset_index()
|
| 83 |
+
sns.barplot(x='country', y='units sold', data=country_data, ax=ax)
|
| 84 |
+
ax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha='right')
|
| 85 |
+
st.pyplot(fig)
|
| 86 |
+
else:
|
| 87 |
+
st.error("The dataset must contain 'country' and 'units sold' columns.")
|
| 88 |
else:
|
| 89 |
+
st.error("Unsupported graph type. Try asking for a correlation matrix, histogram, or bar chart.")
|
| 90 |
except Exception as e:
|
| 91 |
st.error(f"Error generating graph: {e}")
|
| 92 |
|