Visualization GO#

import plotly.graph_objects as go
import plotly.io as pio
pio.renderers.default = "png"

Line Plot#

fig = go.Figure()

fig.add_trace(go.Scatter(x=[1, 2, 3], y=[3, 2, 6] ))
_images/008a0fc508283ecaed6a3e6744a045a2e55cb5e86d967971da3f9f49cb90ed0a.png

Line Width, Color, Style#

fig = go.Figure()

fig.add_trace(go.Scatter(x=[1, 2, 3], y=[3, 2, 6],line=dict(color='red', width=6, dash='dash')))
_images/2f32b94e68bb68ba199bf412385cb7db5192358a1098996f3f6233e302f52acf.png

Scatter Plot#

fig = go.Figure()

fig.add_trace(go.Scatter(x=[1, 2, 3, 4, 5, 6], y=[3, 2, 6, 10, 8, 3], mode='markers'))
_images/463a069a4075c5a34ac702e3464e44650b27aa572bf317374aa2c077fe5442cc.png

Marker Color and Size#

fig = go.Figure()

fig.add_trace(
    go.Scatter(x=[1, 2, 3, 4, 5, 6], y=[3, 2, 6, 10, 8, 3], mode='markers', marker=dict(color='red', size=40))
)
_images/4bf5be77af900d99645d2844f6bf680a316852004f96361ea1b003e1ac578937.png

Line and Marker#

fig = go.Figure()

fig.add_trace(
    go.Scatter(x=[1, 2, 3, 4, 5, 6], y=[3, 2, 6, 10, 8, 3], mode='lines+markers', marker=dict(color='orange', size=40),
              line=dict(color='red', width=6, dash='dash'))
)
_images/ba84eee3c9521e407003f345cf0412136eac331d169e64cd9aff0bc606e3a949.png

Title#

  • use update_layout()

fig = go.Figure()

fig.add_trace(go.Scatter(x=[1, 2, 3], y=[3, 2, 6]))

fig.update_layout(title='Three Points')
_images/7765a283cf08205da211e5db36b5e6b50f8c6b503f1b71896c3a5499231f950a.png

Axis Labels#

  • use update_layout()

fig = go.Figure()

fig.add_trace(go.Scatter(x=[1, 2, 3], y=[3, 2, 6]))

fig.update_layout(title='Three Points', xaxis_title='X-axis', yaxis_title='Y-axis')
_images/88723e82941cdfcadb8ce1fc3ef001669ebb86a51bc99c5fe91aabde1bd41efa.png

Figure Size#

  • Use update_layout()

fig = go.Figure()

fig.add_trace(go.Scatter(x=[1, 2, 3], y=[3, 2, 6]))

fig.update_layout(width=400, height=400)
_images/b29750b6a830da840c4af41b251990bb0b1c2c70698816b51cb50f7441c7e67c.png

Legend#

fig = go.Figure()

fig.add_trace(go.Scatter(x=[1, 2, 3], y=[3, 2, 6], name='Simple Graph-1'))
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[9, -2, 10], name='Simple Graph-2'))

fig.update_layout(showlegend=True)
_images/f874c26ad64448100ed0e3a7b60be3d9e079ecdabcffc7c9413c18f298e8d534.png

Pie Chart#

sizes = ['small','medium','large']
values = [300, 500, 400]
fig = go.Figure()
fig.add_trace(go.Pie(labels=sizes, values=values))
_images/7485b0d777c75bc2b4bf55a296131db42da8ce655abaf547f87ee26551285410.png
fig = go.Figure(data=[go.Pie(labels=sizes, values=values)])
fig.show()
_images/7485b0d777c75bc2b4bf55a296131db42da8ce655abaf547f87ee26551285410.png

Candlestick#

import yfinance as yf
df = yf.Ticker('AAPL').history()
df.head()
Open High Low Close Volume Dividends Stock Splits
Date
2024-12-23 00:00:00-05:00 254.770004 255.649994 253.449997 255.270004 40858800 0.0 0.0
2024-12-24 00:00:00-05:00 255.490005 258.209991 255.289993 258.200012 23234700 0.0 0.0
2024-12-26 00:00:00-05:00 258.190002 260.100006 257.630005 259.019989 27237100 0.0 0.0
2024-12-27 00:00:00-05:00 257.829987 258.700012 253.059998 255.589996 42355300 0.0 0.0
2024-12-30 00:00:00-05:00 252.229996 253.500000 250.750000 252.199997 35557500 0.0 0.0
fig = go.Figure()

fig.add_trace(go.Candlestick(x=df.index,open=df.Open,high=df.High,low=df.Low,close=df.Close,name='Apple'))

fig.update_layout(xaxis=dict(title='Candles',rangeslider=dict(visible=False)) )  # remove the second graph
_images/8891f8739b226c43b2665353c47e10f68b1d4074faf267273842011fa54f1761.png

Multiple Axis#

fig = go.Figure()
fig.add_trace(go.Scatter(x=[1,2,3,4,5], y=[1,3,2,5,4], name='same'))
fig.add_trace(go.Scatter(x=[1,2,3,4,5], y=[10,20,30,40,50], name='tens', yaxis='y2'))
fig.update_layout(
        yaxis=dict(title='Mulltiple Axis', side='left'),height=500,
        yaxis2=dict(overlaying='y',side='right',position=1,showgrid=False))
 
_images/ffaa4ae0747337fc545a35007af7716dd2e67fde6d9093f8f9fd4f7dd282e791.png
fig = go.Figure()
fig.add_trace(go.Scatter(x=[1,2,3,4,5], y=[1,3,2,5,4]))
fig.add_trace(go.Scatter(x=[1,2,3,4,5], y=[10,20,30,40,50], name='tens'))
fig.add_trace(go.Scatter(x=[1,2,3,4,5], y=[300,500,200,400,100], name='hundreds'))
_images/3e8c876f3f2e4bf5c1a1fc18dd483c100d392602dc9383ef10e6b3fbb156a656.png
fig = go.Figure()
fig.add_trace(go.Scatter(x=[1,2,3,4,5], y=[1,3,2,5,4], name='ones'))
fig.add_trace(go.Scatter(x=[1,2,3,4,5], y=[10,20,30,40,50], name='tens', yaxis='y2'))
fig.add_trace(go.Scatter(x=[1,2,3,4,5], y=[300,500,200,400,100], name='hundreds', yaxis='y3'))
fig.update_layout(
        yaxis=dict(title='Mulltiple Axis', side='left'),height=700,
        yaxis2=dict(overlaying='y',side='right',position=1,showgrid=False),
        yaxis3=dict(overlaying='y',side='right',position=0.8,showgrid=False))
 
_images/3d4576d8ea94923a6e2447d89b202c0426499262bbca779d0ad7313a26b73b0a.png
fig = go.Figure()
fig.add_trace(go.Scatter(x=[1,2,3,4,5], y=[1,3,2,5,4], name='ones', line=dict(color='blue')))
fig.add_trace(go.Scatter(x=[1,2,3,4,5], y=[10,20,30,40,50], name='tens', yaxis='y2', line=dict(color='red')))
fig.add_trace(go.Scatter(x=[1,2,3,4,5], y=[300,500,200,400,100], name='hundreds', yaxis='y3', line=dict(color='green')))
fig.update_layout(
        yaxis=dict(title='Mulltiple Axis', side='left', color='blue'),height=700,
        yaxis2=dict(overlaying='y',side='right',position=1, color='red', showgrid=False),
        yaxis3=dict(overlaying='y',side='right',position=0.8, color='green', showgrid=False))
 
_images/7812be9cb3b6154d2859d12b01661ccd7aa2a92ca398ada9975dd8de68f94d1a.png

Area Between Lines#

  • Use the fill and fillcolor parameters

  • fill = ‘tozeroy’ fills the area between the line and the y-axis.

  • fill = ‘tonexty’ fills the area between the line and the next trace.

fig = go.Figure()

fig.add_trace(go.Scatter(x=[1,2,3,4,5], y=[10,50,20,35,60], fill='tozeroy',  fillcolor='green'))
_images/a12d285d35881dd63e49cdcf8c64765cc26778c55ee64a1ca1323bcd3ce760eb.png
fig = go.Figure()

fig.add_trace(go.Scatter(x=[1,2,3,4,5], y=[10,50,20,35,60], line=dict(color='green', width=5)))
fig.add_trace(go.Scatter(x=[1,2,3,4,5], y=[3,10,4,10,25], line=dict(color='red', width=5), fill='tonexty',  fillcolor='yellow'))
_images/1bc7a2ce90b349903c03d8e9423f2d117cd8df3e173b8d458edeb22556193ef1.png

vline and hline#

fig = go.Figure()

fig.add_vline(x=3, line=dict(color='red'))
fig.add_hline(y=2, line=dict(color='green'))
_images/66705e719dd3fd18551091a032ecbca666d06a79dad1ba5b4c7963f27d9ae32f.png