File size: 4,425 Bytes
4ca09f3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
---
title: Examples
sidebar_position: 1
description: This page provides examples of creating charts with the `openbb-charting` extension.
keywords:
- tutorial
- OpenBB Platform
- Python client
- Fast API
- getting started
- extensions
- charting
- view
- Plotly
- toolkits
- how-to
- generic
- figure
---
import HeadTitle from '@site/src/components/General/HeadTitle.tsx';
<HeadTitle title="Examples - OpenBB Charting - Extensions | OpenBB Platform Docs" />
## Overview
This page will walk through creating different charts using the `openbb-charting` extension.
The perspective for this content is from the Python Interface,
and the examples will assume that the OpenBB Platform is installed with all optional packages.
```python
from datetime import datetime, timedelta
from openbb import obb
```
## Cumulative Returns
The historical (equity) prices can be requested for multiple symbols.
The extension will attempt to handle variations accordingly.
By default, more than three symbols will draw the chart as cumulative returns from the beginning of the series.
### Default View
The tickers below are a collection of State Street Global Advisors SPDR funds, representing S&P 500 components.
The data is looking back five years.
```python
SPDRS = [
"SPY",
"XLE",
"XLB",
"XLI",
"XHB",
"XLP",
"XLY",
"XRT",
"XLF",
"XLV",
"XLK",
"XLC",
"XLU",
"XLRE",
]
start_date = (datetime.now() - timedelta(weeks=52*5)).date()
spdrs = obb.equity.price.historical(SPDRS, start_date=start_date, provider="yfinance", chart=True)
spdrs.show()
```

### Redraw as YTD
The `charting` attribute of the command output has methods for creating the chart again.
The `data` parameter allows modifications to the data before creating the figure.
In this example, the length of the data is trimmed to the beginning of the year.
```python
new_data = spdrs.to_df().loc[datetime(2024,12,29).date():]
spdrs.charting.to_chart(data=new_data, title="YTD")
```
:::note
This replaces the chart that was already created.
:::

## Price Performance Bar Chart
The `obb.equity.price.performance` endpoint will create a bar chart over intervals.
```python
price_performance = obb.equity.price.performance(SPDRS, chart=True)
price_performance.show()
```

### Create Bar Chart
This example uses the `create_bar_chart()` method, which does not replace the existing chart, in `price_performance.chart`.
It isolates the one-month performance and orients the layout as horizontal.
```python
new_data = price_performance.to_df().set_index("symbol").multiply(100).reset_index()
price_performance.charting.create_bar_chart(
data=new_data,
x="symbol",
y="one_month",
orientation="h",
title="One Month Price Performance",
xtitle="Percent (%)"
)
```

## Create Your Own
This example analyzes the share volume turnover of the S&P 500 Energy Sector constituents, year-to-date.
```python
symbols = [
'XOM',
'CVX',
'COP',
'WMB',
'EOG',
'KMI',
'OKE',
'MPC',
'PSX',
'SLB',
'VLO',
'BKR',
'HES',
'TRGP',
'EQT',
'OXY',
'TPL',
'FANG',
'EXE',
'DVN',
'HAL',
'CTRA',
'APA',
]
data = obb.equity.price.historical(symbols, start_date="2025-01-01", provider="yfinance")
create_bar_chart = data.charting.create_bar_chart
volume = data.to_df().groupby("symbol").sum()["volume"]
shares = obb.equity.profile(
symbols, provider="yfinance"
).to_df().set_index("symbol")["shares_float"]
df = volume.to_frame().join(shares)
df["Turnover"] = (df.volume/df.shares_float).round(4)
df = df.sort_values(by="Turnover", ascending=False).reset_index()
create_bar_chart(
data=df,
x="symbol",
y="Turnover",
title="S&P Energy Sector YTD Turnover Rate",
)
```

|