03. Plot Sparklines

This example shows to to plot sparklines style graphs.

  7 # -------------------
  8 # Main
  9 # -------------------
 10 # https://chart-studio.plotly.com/~empet/13748/sparklines/#/code
 11 # https://omnipotent.net/jquery.sparkline/#s-about
 12 # https://chart-studio.plotly.com/create/?fid=Dreamshot:8025#/
 13
 14 # Libraries
 15 import numpy as np
 16 import pandas as pd
 17 import plotly.express as px
 18 import plotly.graph_objects as go
 19
 20 from pandas.tseries.offsets import DateOffset
 21 from plotly.subplots import make_subplots
 22 from plotly.io import show
 23
 24 try:
 25     __file__
 26     TERMINAL = True
 27 except:
 28     TERMINAL = False
 29
 30 # Constants
 31 colors = px.colors.sequential.Viridis_r
 32
 33 # Size
 34 S = 100
 35 N = 7
 36
 37 # Create data
 38 x = np.arange(S)
 39 y = np.random.randint(low=1, high=100, size=(S, N))
 40
 41 # Create DataFrame
 42 data = pd.DataFrame(y)
 43 data.columns = ['c%s'%i for i in data.columns]
 44
 45 # Create timedelta
 46 data['timedelta'] = \
 47     pd.to_timedelta(data.index / 1, unit='D')
 48
 49 # Create datetimes (if needed)
 50 today = pd.to_datetime('today').normalize()
 51 data['dates'] = pd.to_datetime(today)
 52 data['dates'] += pd.to_timedelta(data.index / 1, unit='D')
 53
 54 # Set dates as index
 55 data = data.set_index('dates')
 56
 57 # Drop timedelta
 58 data = data.drop(columns='timedelta')
 59
 60 # Show data
 61 if TERMINAL:
 62     print("\nData:")
 63     print(data)
 64 data
 65
 66
 67
 68 # ----------------
 69 # Visualize
 70 # ----------------
 71 # Create layout
 72 layout = {
 73   "font": {"family": "Georgia, serif"},
 74   #"title": "Sparklines",
 75   "width": 700,
 76   "height": 500,
 77   "margin": {"t": 80},
 78   "paper_bgcolor": 'rgba(0,0,0,0)',  # transparent
 79   "plot_bgcolor": 'rgba(0,0,0,0)',  # transparent
 80   "autosize": False,
 81   "hovermode": "closest",
 82   "showlegend": False,
 83 }
 84
 85 # Create figure
 86 fig = make_subplots(rows=N, cols=1,
 87                     subplot_titles=None)
 88
 89 # Add traces
 90 for i, column in enumerate(data.columns):
 91     # Colors
 92     c = colors[i]
 93     x = data.index
 94     y = data[column]
 95
 96     # Add trace
 97     fig.add_trace(go.Scatter(x=x, y=y,
 98         name=column.upper(),
 99         mode='lines', fill='tozeroy',
100         line=dict(color=c, width=0.5),
101         xaxis='x%s' % (i+1), yaxis='y%s' % (i+1)),
102         row=i+1, col=1)
103
104     # Update axes
105     fig.update_yaxes(title_text=column, row=i+1, col=1)
106
107     # Add to layout
108     layout["xaxis%s" % (i+1)] = {
109         "ticks": "",
110         "anchor": 'y%s' % (i+1),
111         "domain": [0.0, 1.0],
112         "mirror": False,
113         "showgrid": False,
114         "showline": False,
115         "zeroline": False,
116         "showticklabels": False
117     }
118     layout["yaxis%s" % (i+1)] = {
119       "ticks": "",
120       "anchor": 'x%s' % (i+1),
121       #"domain": [0.08416666666666667, 0.15833333333333333],
122       "mirror": False,
123       "showgrid": False,
124       "showline": False,
125       "zeroline": False,
126       "showticklabels": False
127     }
128
129 # Update layout
130 fig.update_layout(layout)
131
132 # Show
133 show(fig)

Total running time of the script: ( 0 minutes 0.270 seconds)

Gallery generated by Sphinx-Gallery