Note
Click here to download the full example code
10. Plot sparklines with weather
6 # -------------------
7 # Main
8 # -------------------
9 # https://chart-studio.plotly.com/~empet/13748/sparklines/#/code
10 # https://omnipotent.net/jquery.sparkline/#s-about
11 # https://chart-studio.plotly.com/create/?fid=Dreamshot:8025#/
12
13 # Libraries
14 import glob
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 colors += px.colors.sequential.Viridis_r
33
34 # -------------------
35 # Load data
36 # -------------------
37 # Define folder
38 folder = '../../datasets/weather-hmc-dataset/'
39
40 # Load data
41 data = pd.concat([pd.read_csv(f) \
42 for f in glob.glob('%s/*.csv' % folder)])
43
44 # Drop duplicates
45 data = data.drop_duplicates()
46 data['dates'] = pd.to_datetime(data['Date time'])
47 data = data.set_index('dates')
48 data = data.drop(columns=['Name',
49 'Date time',
50 'Conditions'])
51
52 if TERMINAL:
53 print("\nData:")
54 print(data)
55 data
56
57
58 # ----------------
59 # Visualize
60 # ----------------
61 # Create layout
62 layout = {
63 "font": {"family": "Georgia, serif"},
64 "title": "Weather Ho Chi Minh",
65 #"width": 500,
66 "height": 1000,
67 #"margin": {"t": 80},
68 "paper_bgcolor": 'rgba(0,0,0,0)', # transparent
69 "plot_bgcolor": 'rgba(0,0,0,0)', # transparent
70 #"autosize": False,
71 "hovermode": "closest",
72 "showlegend": False,
73 }
74
75 # Create figure
76 fig = make_subplots(rows=data.shape[1], cols=1,
77 shared_xaxes=True,
78 subplot_titles=[t for t in data.columns])
79
80 # Add traces
81 for i, column in enumerate(data.columns):
82 # Colors
83 c = colors[i]
84 x = data.index
85 y = data[column]
86
87 # Add trace
88 fig.add_trace(go.Scatter(x=x, y=y,
89 name=column,
90 mode='lines', fill='tozeroy',
91 line=dict(color=c, width=0.5)),
92 row=i+1, col=1)
93
94 # Update axes
95 fig.update_yaxes(title_text='', row=i+1, col=1)
96
97 # Update layout
98 fig.update_layout(layout)
99
100 # Show
101 show(fig)
Total running time of the script: ( 0 minutes 5.910 seconds)