> ## Documentation Index
> Fetch the complete documentation index at: https://docs.textql.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Python

> Visualize and analyze your data

After Ana queries your data with Text to SQL or Ontology, Python transforms and visualizes it. Just describe what charts or analysis you want—you never need to write code yourself.

<Note>
  **Works with SQL Tools:** Python is typically used alongside Text to SQL or Ontology. Ana queries your data first, then uses Python to create visualizations and perform additional analysis.
</Note>

<Frame caption="Python execution cell">
  <img src="https://mintcdn.com/textql/_MjEsXv_WMgj05pB/images/tool/python_cell.png?fit=max&auto=format&n=_MjEsXv_WMgj05pB&q=85&s=0ae0eeb08394333f6d95c67c1c4c2f8e" alt="Python execution cell" width="3340" height="1922" data-path="images/tool/python_cell.png" />
</Frame>

## <Icon icon="chart-line" iconType="solid" /> Primary Use: Data Visualization

Python is mainly used to create visualizations from data retrieved by Text to SQL or Ontology queries:

<CardGroup cols={2}>
  <Card title="Charts & Graphs" icon="chart-simple">
    "Create a bar chart of this data"

    "Show monthly trends as a line chart"

    "Make a pie chart of sales by category"
  </Card>

  <Card title="Advanced Visualizations" icon="chart-mixed">
    "Build a scatter plot with trend line"

    "Create a heat map of correlations"

    "Show this as a stacked area chart"
  </Card>

  <Card title="Interactive Maps" icon="map-location-dot">
    "Plot customer locations on a map"

    "Show sales by region on a map"

    "Create an interactive map with markers"
  </Card>

  <Card title="Statistical Plots" icon="chart-scatter">
    "Show the distribution as a histogram"

    "Create a box plot by segment"

    "Make a correlation matrix"
  </Card>
</CardGroup>

## <Icon icon="circle-check" iconType="solid" /> When to Use

Python works best after you've retrieved data with Text to SQL or Ontology:

<Steps>
  <Step title="Query Your Data First">
    Start by getting the data you need using Text to SQL or Ontology.

    **Example:** "Show me monthly sales by region for 2024"
  </Step>

  <Step title="Then Visualize or Analyze">
    Once you have the data, ask Ana to create visualizations or perform calculations.

    **Example:** "Create a line chart of those trends" or "Calculate the growth rate"
  </Step>
</Steps>

### Common Workflows

<Tabs>
  <Tab title="SQL → Chart">
    **The most common pattern:**

    1. "Get total sales by product category"
    2. "Create a bar chart of that data"

    Ana queries your database, then visualizes the results.
  </Tab>

  <Tab title="Ontology → Visualization">
    **Using your semantic layer:**

    1. "Query my 'Monthly Revenue' metric"
    2. "Show it as a trend line with forecasts"

    Ana uses your pre-defined metrics, then creates visualizations.
  </Tab>

  <Tab title="SQL → Analysis → Chart">
    **Multi-step analysis:**

    1. "Get customer purchase history"
    2. "Calculate the average order value per customer"
    3. "Show the distribution as a histogram"

    Ana queries, calculates, then visualizes.
  </Tab>
</Tabs>

## <Icon icon="lightbulb" iconType="solid" /> Tips for Better Visualizations

<CardGroup cols={2}>
  <Card title="Be Specific About Chart Type" icon="chart-column">
    Tell Ana exactly what kind of chart you want.

    **Good:** "Create a stacked bar chart showing monthly revenue by product line"

    **Vague:** "Make a chart"
  </Card>

  <Card title="Request Changes Easily" icon="pen">
    If a visualization isn't quite right, just ask for adjustments.

    **Examples:**

    * "Make that chart larger"
    * "Use different colors"
    * "Sort by highest to lowest"
    * "Add a trend line"
  </Card>

  <Card title="Combine Visualizations" icon="table-cells">
    You can ask for multiple charts to compare different views.

    **Example:** "Show me a bar chart and a trend line of the same data"
  </Card>

  <Card title="Ask for Insights" icon="magnifying-glass-chart">
    Ana can calculate additional metrics to visualize.

    **Example:** "Show the data with moving averages" or "Add percentage changes"
  </Card>
</CardGroup>

## <Icon icon="comments" iconType="solid" /> Example Visualization Requests

Real examples of how SQL + Python work together:

<Tabs>
  <Tab title="Basic Charts">
    **Request:**

    * "Get sales by month, then show it as a line chart"
    * "Query top 10 products and create a bar chart"
    * "Show customer distribution by state on a map"
  </Tab>

  <Tab title="Trend Analysis">
    **Request:**

    * "Get daily revenue for the year and show trends"
    * "Query user signups over time with a trend line"
    * "Show seasonal patterns in sales data"
  </Tab>

  <Tab title="Comparisons">
    **Request:**

    * "Compare this quarter vs last quarter as a grouped bar chart"
    * "Show revenue by region side-by-side"
    * "Create a heat map comparing products and regions"
  </Tab>

  <Tab title="Advanced">
    **Request:**

    * "Show a scatter plot of price vs units sold with a regression line"
    * "Create a box plot showing distribution by segment"
    * "Build a correlation matrix of our key metrics"
  </Tab>
</Tabs>

## <Icon icon="info-circle" iconType="solid" /> What Else Python Can Do

While visualization is the primary use case, Python can also:

<AccordionGroup>
  <Accordion title="Simple Calculations" icon="calculator">
    * Calculate growth rates and percentages
    * Compute moving averages
    * Find totals and subtotals
    * Determine min/max values
  </Accordion>

  <Accordion title="Data Exports" icon="download">
    * Export processed data as CSV
    * Create Excel files with multiple sheets
    * Generate formatted reports
  </Accordion>

  <Accordion title="Statistical Analysis" icon="chart-scatter">
    * Calculate correlations
    * Perform basic regression
    * Show distributions
    * Identify outliers
  </Accordion>
</AccordionGroup>

<Tip>
  **Best Practice:** Always start with a SQL query (Text to SQL or Ontology) to get your data, then use Python to visualize or analyze it. This workflow gives you the best results.
</Tip>
