Visualization Methods
A collection of visualization functions for financial and data analysis using Plotly.js. These methods provide interactive charts and graphs for data exploration and presentation.
visualize (Core Helper)
visualize.visualize(fig)Displays a Plotly figure object. This is the core visualization function used internally by all other methods. In Jupyter notebooks, this will render the chart inline. In scripts, it will open the chart in a browser.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| fig | plotly.graph_objects.Figure | Required | Plotly figure object to display |
Example
# Create a simple figure
import plotly.graph_objects as go
fig = go.Figure(data=go.Scatter(x=[1,2,3], y=[4,5,6]))
# Display it
visualize.visualize(fig)visualize.cov()
visualize.cov(df)Creates a heatmap visualization of the correlation matrix for numeric columns in a DataFrame. Uses the Viridis color scale and automatically handles the correlation calculation.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| df | pandas.DataFrame | Required | DataFrame containing numeric columns for correlation analysis. Non-numeric columns are automatically ignored. |
Example
import pandas as pd
from axion import visualize
# Create sample data
df = pd.DataFrame({
'A': [1, 2, 3, 4, 5],
'B': [2, 4, 6, 8, 10],
'C': [5, 4, 3, 2, 1],
'D': [1, 3, 5, 7, 9]
})
# Display correlation heatmap
visualize.cov(df)visualize.candles()
visualize.candles(df)Creates an interactive candlestick chart for financial price data with OHLC (Open, High, Low, Close) values. Perfect for stock prices, cryptocurrency data, or any time-series financial data.
Required DataFrame Columns
time- Time series data (dates/timestamps)open- Opening price for each periodhigh- Highest price during each periodlow- Lowest price during each periodclose- Closing price for each period
Example
import pandas as pd
from axion import visualize
# Sample stock data
df = pd.DataFrame({
'time': ['2024-01-01', '2024-01-02', '2024-01-03'],
'open': [100, 102, 101],
'high': [105, 103, 104],
'low': [99, 101, 100],
'close': [102, 101, 103]
})
# Display candlestick chart
visualize.candles(df)visualize.line()
visualize.line(df, x, y, log=False)Creates a line chart for time series or sequential data visualization. Supports logarithmic scaling for the x-axis.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| df | pandas.DataFrame | - | DataFrame containing the data to plot |
| x | string | - | Column name for x-axis values (typically time/date) |
| y | string | - | Column name for y-axis values |
| log | boolean | False | Use logarithmic scale for x-axis |
Examples
Basic Line Chart
# Basic line chart
df = pd.DataFrame({
'date': ['2024-01-01', '2024-01-02', '2024-01-03'],
'value': [10, 15, 13]
})
visualize.line(df, x='date', y='value')Logarithmic X-Axis
# Line chart with logarithmic x-axis
df = pd.DataFrame({
'x': [1, 10, 100, 1000],
'y': [1, 2, 3, 4]
})
visualize.line(df, x='x', y='y', log=True)visualize.pie()
visualize.pie(df, values, labels)Creates a pie chart showing proportional distribution of categorical data. Each slice represents a category, and the size corresponds to the numeric values.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| df | pandas.DataFrame | Required | DataFrame containing the data |
| values | string | Required | Column name for pie slice sizes (numeric) |
| labels | string | Required | Column name for pie slice labels (categorical) |
Example
# Market share data
df = pd.DataFrame({
'company': ['Apple', 'Samsung', 'Google', 'Others'],
'market_share': [45, 30, 15, 10]
})
visualize.pie(df, values='market_share', labels='company')visualize.fit()
visualize.fit(df, x, y, log=False, hover=[], group=None)Creates a scatter plot with an Ordinary Least Squares (OLS) trendline for regression analysis. The trendline shows the linear relationship between variables with confidence bands.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| df | pandas.DataFrame | - | DataFrame containing the data |
| x | string | - | Column name for x-axis values (independent variable) |
| y | string | - | Column name for y-axis values (dependent variable) |
| log | boolean | False | Use logarithmic scale for x-axis |
| hover | list | [] | List of column names to show in hover tooltips |
| group | string | None | Column name for color grouping (creates separate trendlines per group) |
Examples
With Grouping
# Simple linear regression
df = pd.DataFrame({
'x': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
'y': [2, 4, 5, 4, 5, 7, 8, 9, 10, 12],
'category': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B', 'B']
})
# With grouping
visualize.fit(df, x='x', y='y', group='category', hover=['category'])
# Without grouping
visualize.fit(df, x='x', y='y')visualize.scatter()
visualize.scatter(df, x, y, log=False, hover=[], group=None)Creates a standard scatter plot for visualizing relationships between two variables. Supports grouping and hover data for enhanced data exploration.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| df | pandas.DataFrame | - | DataFrame containing the data |
| x | string | - | Column name for x-axis values |
| y | string | - | Column name for y-axis values |
| log | boolean | False | Use logarithmic scale for x-axis |
| hover | list | [] | List of column names to show in hover tooltips |
| group | string | None | Column name for color grouping |
Examples
With Grouping
# Scatter plot with grouping and hover data
df = pd.DataFrame({
'height': [150, 160, 170, 180, 155, 165, 175, 185],
'weight': [50, 60, 70, 80, 55, 65, 75, 85],
'gender': ['F', 'F', 'M', 'M', 'F', 'F', 'M', 'M'],
'age': [25, 30, 35, 40, 28, 33, 38, 43]
})
visualize.scatter(df, x='height', y='weight',
group='gender', hover=['age'])Simple Scatter
# Simple scatter
visualize.scatter(df, x='height', y='weight')visualize.bar()
visualize.bar(df, x, y)Creates a vertical bar chart for categorical data comparison. Each bar represents a category, and the height represents the corresponding value.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| df | pandas.DataFrame | Required | DataFrame containing the data |
| x | string | Required | Column name for categories (x-axis) |
| y | string | Required | Column name for values (bar heights) |
Example
# Sales by product
df = pd.DataFrame({
'product': ['A', 'B', 'C', 'D'],
'sales': [1500, 2300, 1200, 3100]
})
visualize.bar(df, x='product', y='sales')visualize.area()
visualize.area(df, x, y, group, sub="")Creates a stacked area chart showing cumulative values over time by category. Great for visualizing composition changes over time.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| df | pandas.DataFrame | - | DataFrame containing the data |
| x | string | - | Column name for x-axis (typically time/date) |
| y | string | - | Column name for y-axis values |
| group | string | - | Column name for grouping/categories |
| sub | string | "" | Subgroup identifier (currently unused) |
Example
# Monthly sales by region
df = pd.DataFrame({
'month': ['Jan', 'Jan', 'Feb', 'Feb', 'Mar', 'Mar'],
'sales': [100, 150, 120, 180, 140, 200],
'region': ['North', 'South', 'North', 'South', 'North', 'South']
})
visualize.area(df, x='month', y='sales', group='region')visualize.heatmap()
visualize.heatmap(df, x, y, hover=[])Creates a density heatmap showing the distribution of data points across two dimensions. The color intensity represents the density of points in each region.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| df | pandas.DataFrame | - | DataFrame containing the data |
| x | string | - | Column name for x-axis values |
| y | string | - | Column name for y-axis values |
| hover | list | [] | List of column names to show in hover tooltips |
Example
# Create sample data with clusters
import numpy as np
import SEOMetadata from '@/components/SEOMetadata';
np.random.seed(42)
df = pd.DataFrame({
'x': np.random.normal(0, 1, 1000),
'y': np.random.normal(0, 1, 1000),
'value': np.random.randn(1000)
})
visualize.heatmap(df, x='x', y='y', hover=['value'])visualize.radar()
visualize.radar(df, values, labels)Creates a radar chart for multivariate data visualization across multiple axes. Each axis represents a different variable, and the area shows the profile.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| df | pandas.DataFrame | Required | DataFrame containing a single row of data |
| values | string | Required | Column name containing the numeric values |
| labels | string | Required | Column name containing the axis labels |
Example
# Product performance metrics
df = pd.DataFrame({
'metric': ['Speed', 'Reliability', 'Cost', 'Quality', 'Support'],
'score': [85, 92, 78, 95, 88]
})
visualize.radar(df, values='score', labels='metric')visualize.barh()
visualize.barh(df, x, y)Creates a horizontal bar chart, useful for comparing categories with long names or when you have many categories. Bars extend horizontally from the y-axis.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| df | pandas.DataFrame | Required | DataFrame containing the data |
| x | string | Required | Column name for values (bar lengths) |
| y | string | Required | Column name for categories (y-axis labels) |
Example
# Long category names
df = pd.DataFrame({
'product': ['Enterprise License', 'Professional Edition',
'Standard Package', 'Basic Subscription'],
'revenue': [150000, 85000, 45000, 20000]
})
visualize.barh(df, x='revenue', y='product')visualize.spread()
visualize.spread(dfs, x, y)Creates a composite chart showing two series and their difference (spread) as a bar chart. Perfect for pairs trading analysis or comparing two related time series.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| dfs | list of DataFrames | Required | List containing two DataFrames to compare |
| x | string | Required | Common column name for merging and x-axis (usually time/date) |
| y | string | Required | Column name for values in both DataFrames |
Example
# Compare two correlated stocks
df1 = pd.DataFrame({
'date': ['2024-01-01', '2024-01-02', '2024-01-03'],
'price': [100, 102, 101]
})
df2 = pd.DataFrame({
'date': ['2024-01-01', '2024-01-02', '2024-01-03'],
'price': [98, 101, 103]
})
visualize.spread([df1, df2], x='date', y='price')visualize.polls()
visualize.polls(df)Creates a multi-line chart where each column in the DataFrame becomes a separate line series. The x-axis uses the DataFrame index, making it ideal for time series with multiple variables.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| df | pandas.DataFrame | Required | DataFrame where each column becomes a line series. Index is used for x-axis. |
Example
# Opinion polling over time
df = pd.DataFrame({
'Candidate A': [45, 47, 46, 48, 50, 49],
'Candidate B': [42, 41, 43, 42, 41, 43],
'Undecided': [13, 12, 11, 10, 9, 8]
}, index=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'])
visualize.polls(df)visualize.tree()
visualize.tree(df)Creates a hierarchical treemap visualization for nested categorical data. Rectangles represent hierarchy levels, with size proportional to marketCap and color indicating pctchange.
Expected DataFrame Columns
sector- Top-level category (e.g., Technology, Healthcare)industry- Mid-level category (e.g., Software, Biotech)symbol- Leaf-level identifier (e.g., AAPL, MSFT)marketCap- Size value (determines rectangle area)pctchange- Color/hover data (red-green color scale)lastsale- Additional hover data (last sale price)
Example
# Stock market sector visualization
df = pd.DataFrame({
'sector': ['Tech', 'Tech', 'Tech', 'Health', 'Health'],
'industry': ['Software', 'Software', 'Hardware', 'Biotech', 'Pharma'],
'symbol': ['AAPL', 'MSFT', 'NVDA', 'GILD', 'PFE'],
'marketCap': [2500000, 2100000, 800000, 700000, 180000],
'pctchange': [2.5, 1.8, -0.5, 3.2, -1.1],
'lastsale': [175.50, 380.25, 450.75, 85.30, 42.60]
})
visualize.tree(df)visualize.graph()
visualize.graph(df, x, bars=[], lines=[], areas=[], title='', color=None)Creates a comprehensive composite chart combining bars, lines, and areas on the same plot. Offers maximum flexibility for complex visualizations with multiple series types.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
| df | pandas.DataFrame | - | DataFrame containing all data |
| x | string | - | Column name for x-axis values |
| bars | list | [] | List of column names for bar series |
| lines | list | [] | List of column names for line series |
| areas | list | [] | List of column names for area series |
| title | string | '' | Chart title |
| color | string | None | Column name for color grouping bars |
Color Palette
shades_of_white = [
'rgb(31, 119, 180)', // blue
'rgb(255, 127, 14)', // orange
'rgb(44, 160, 44)', // green
'rgb(214, 39, 40)', // red
'rgb(148, 103, 189)', // purple
'rgb(140, 86, 75)', // brown
'rgb(255, 255, 255)',
'rgb(245, 245, 245)',
'rgb(235, 235, 235)',
'rgb(235, 225, 225)',
'rgb(235, 215, 215)',
'rgb(235, 205, 205)',
]Examples
Composite Chart
# Composite chart with bars and lines
df = pd.DataFrame({
'date': ['2024-01-01', '2024-01-02', '2024-01-03'],
'revenue': [1000, 1200, 1100],
'profit': [200, 250, 230],
'users': [500, 600, 650],
'region': ['North', 'South', 'North']
})
visualize.graph(df, x='date',
bars=['revenue'],
lines=['profit', 'users'],
title='Company Performance')With Color Grouping
# With color grouping for bars
visualize.graph(df, x='date',
bars=['revenue'],
lines=['profit'],
color='region',
title='Performance by Region')