You are on page 1of 8

10-program-dv-lab

March 20, 2024

Program 10
a) Write a Python program to draw Time Series using Plotly Libraries.
b) Write a Python program for creating Maps using Plotly Libraries.

[ ]: # a) Write a Python program to draw Time Series using Plotly Libraries.


import plotly.graph_objects as go
from datetime import datetime
# imports the datetime class from the datetime module in Python's standard␣
↪library.

# Sample data
dates = [
datetime(2023, 10, 1),
datetime(2023, 10, 2),
datetime(2023, 10, 3),
datetime(2023, 10, 4),
datetime(2023, 10, 5)
]
print(dates)
values = [5, 7, 8, 4, 6]
# Create a trace
trace = go.Scatter(x=dates, y=values, mode='lines+markers')

#33
# Create layout
layout = go.Layout(title='Time Series Example', xaxis_title='Date',␣
↪yaxis_title='Value')

# Create figure
fig = go.Figure(data=[trace], layout=layout)
# Show the plot
fig.show()

[datetime.datetime(2023, 10, 1, 0, 0), datetime.datetime(2023, 10, 2, 0, 0),


datetime.datetime(2023, 10, 3, 0, 0), datetime.datetime(2023, 10, 4, 0, 0),
datetime.datetime(2023, 10, 5, 0, 0)]

[ ]: # a) Write a Python program to draw Time Series using Plotly Libraries.


import plotly.graph_objs as go

1
# Sample time series data
dates = ['2023-01-01', '2023-01-02', '2023-01-03', '2023-01-04', '2023-01-05']
values = [10, 15, 13, 18, 20]

# Create trace for time series plot


trace = go.Scatter(
x=dates,
y=values,
mode='lines+markers',
name='Time Series Data'
)

# Create layout for the plot


layout = go.Layout(
title='Time Series Plot',
xaxis_title='Date',
yaxis=dict(title='Value')
)

# Create figure object


fig = go.Figure(trace,layout)

# Show the plot


fig.show()

A choropleth map is a thematic map where areas are shaded or patterned in proportion to the value
of a variable being represented. These areas are typically geographical regions such as countries,
states, or counties. The purpose of a choropleth map is to visualize spatial patterns and variations
in the data.
[ ]: # b) Write a Python program for creating Maps using Plotly Libraries.
import plotly.graph_objects as go
# Create a trace for the map
# a choropleth map is created using Plotly's go.Choropleth class within a␣
↪Plotly figure (go.Figure).

# go.Figure(data=...): creates a new Plotly figure (go.Figure) and initializes␣


↪it with choropleth map data.

# locations=['IND']: parameter specifies the geographical locations to be␣


↪mapped. In this case, 'IND' represents the location code for India.

# z=[1]: This parameter specifies the values associated with each location.␣
↪Here, the value of 1 is assigned to India.

# hovertext=['India']: This parameter specifies the text that appears when␣


↪hovering over each location on the map.

# In this case, it displays 'India' when hovering over the location represented␣
↪by 'IND'.

2
''' Country Codes (ISO 3166-1 alpha-3): For world maps, you can use␣
↪three-letter country codes

from the ISO 3166-1 alpha-3 standard to represent countries. For example,
'USA' for the United States, 'GBR' for the United Kingdom, 'IND' for India, etc.
↪'''

# State Abbreviations (ISO 3166-2)


trace = go.Figure(data=go.Choropleth(
locations=['IND','AUS'],
z=[1,2],
hovertext=['INDIA','AUSTRALIA'],
hoverinfo='location+z+text',
colorscale='Viridis'
))
# Create layout for the map
layout = go.Layout(geo=dict(
showcoastlines=True
))

# Create figure
fig = go.Figure(trace, layout)
# Show the map
fig.show()

[ ]: !pip install holoviews


!pip install hvplot
import holoviews as hv
from holoviews import opts
import pandas as pd

Requirement already satisfied: holoviews in /usr/local/lib/python3.10/dist-


packages (1.17.1)
Requirement already satisfied: param<3.0,>=1.12.0 in
/usr/local/lib/python3.10/dist-packages (from holoviews) (2.0.2)
Requirement already satisfied: numpy>=1.0 in /usr/local/lib/python3.10/dist-
packages (from holoviews) (1.25.2)
Requirement already satisfied: pyviz-comms>=0.7.4 in
/usr/local/lib/python3.10/dist-packages (from holoviews) (3.0.1)
Requirement already satisfied: panel>=0.13.1 in /usr/local/lib/python3.10/dist-
packages (from holoviews) (1.3.8)
Requirement already satisfied: colorcet in /usr/local/lib/python3.10/dist-
packages (from holoviews) (3.0.1)
Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-
packages (from holoviews) (23.2)
Requirement already satisfied: pandas>=0.20.0 in /usr/local/lib/python3.10/dist-
packages (from holoviews) (1.5.3)
Requirement already satisfied: python-dateutil>=2.8.1 in
/usr/local/lib/python3.10/dist-packages (from pandas>=0.20.0->holoviews) (2.8.2)

3
Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-
packages (from pandas>=0.20.0->holoviews) (2023.4)
Requirement already satisfied: bokeh<3.4.0,>=3.2.0 in
/usr/local/lib/python3.10/dist-packages (from panel>=0.13.1->holoviews) (3.3.4)
Requirement already satisfied: xyzservices>=2021.09.1 in
/usr/local/lib/python3.10/dist-packages (from panel>=0.13.1->holoviews)
(2023.10.1)
Requirement already satisfied: markdown in /usr/local/lib/python3.10/dist-
packages (from panel>=0.13.1->holoviews) (3.5.2)
Requirement already satisfied: markdown-it-py in /usr/local/lib/python3.10/dist-
packages (from panel>=0.13.1->holoviews) (3.0.0)
Requirement already satisfied: linkify-it-py in /usr/local/lib/python3.10/dist-
packages (from panel>=0.13.1->holoviews) (2.0.3)
Requirement already satisfied: mdit-py-plugins in
/usr/local/lib/python3.10/dist-packages (from panel>=0.13.1->holoviews) (0.4.0)
Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-
packages (from panel>=0.13.1->holoviews) (2.31.0)
Requirement already satisfied: tqdm>=4.48.0 in /usr/local/lib/python3.10/dist-
packages (from panel>=0.13.1->holoviews) (4.66.2)
Requirement already satisfied: bleach in /usr/local/lib/python3.10/dist-packages
(from panel>=0.13.1->holoviews) (6.1.0)
Requirement already satisfied: typing-extensions in
/usr/local/lib/python3.10/dist-packages (from panel>=0.13.1->holoviews) (4.10.0)
Requirement already satisfied: pyct>=0.4.4 in /usr/local/lib/python3.10/dist-
packages (from colorcet->holoviews) (0.5.0)
Requirement already satisfied: Jinja2>=2.9 in /usr/local/lib/python3.10/dist-
packages (from bokeh<3.4.0,>=3.2.0->panel>=0.13.1->holoviews) (3.1.3)
Requirement already satisfied: contourpy>=1 in /usr/local/lib/python3.10/dist-
packages (from bokeh<3.4.0,>=3.2.0->panel>=0.13.1->holoviews) (1.2.0)
Requirement already satisfied: pillow>=7.1.0 in /usr/local/lib/python3.10/dist-
packages (from bokeh<3.4.0,>=3.2.0->panel>=0.13.1->holoviews) (9.4.0)
Requirement already satisfied: PyYAML>=3.10 in /usr/local/lib/python3.10/dist-
packages (from bokeh<3.4.0,>=3.2.0->panel>=0.13.1->holoviews) (6.0.1)
Requirement already satisfied: tornado>=5.1 in /usr/local/lib/python3.10/dist-
packages (from bokeh<3.4.0,>=3.2.0->panel>=0.13.1->holoviews) (6.3.2)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-
packages (from python-dateutil>=2.8.1->pandas>=0.20.0->holoviews) (1.16.0)
Requirement already satisfied: webencodings in /usr/local/lib/python3.10/dist-
packages (from bleach->panel>=0.13.1->holoviews) (0.5.1)
Requirement already satisfied: uc-micro-py in /usr/local/lib/python3.10/dist-
packages (from linkify-it-py->panel>=0.13.1->holoviews) (1.0.3)
Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.10/dist-
packages (from markdown-it-py->panel>=0.13.1->holoviews) (0.1.2)
Requirement already satisfied: charset-normalizer<4,>=2 in
/usr/local/lib/python3.10/dist-packages (from
requests->panel>=0.13.1->holoviews) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-
packages (from requests->panel>=0.13.1->holoviews) (3.6)

4
Requirement already satisfied: urllib3<3,>=1.21.1 in
/usr/local/lib/python3.10/dist-packages (from
requests->panel>=0.13.1->holoviews) (2.0.7)
Requirement already satisfied: certifi>=2017.4.17 in
/usr/local/lib/python3.10/dist-packages (from
requests->panel>=0.13.1->holoviews) (2024.2.2)
Requirement already satisfied: MarkupSafe>=2.0 in
/usr/local/lib/python3.10/dist-packages (from
Jinja2>=2.9->bokeh<3.4.0,>=3.2.0->panel>=0.13.1->holoviews) (2.1.5)
Collecting hvplot
Downloading hvplot-0.9.2-py2.py3-none-any.whl (1.8 MB)
���������������������������������������� 1.8/1.8 MB
16.4 MB/s eta 0:00:00
Requirement already satisfied: bokeh>=1.0.0 in
/usr/local/lib/python3.10/dist-packages (from hvplot) (3.3.4)
Requirement already satisfied: colorcet>=2 in /usr/local/lib/python3.10/dist-
packages (from hvplot) (3.0.1)
Requirement already satisfied: holoviews>=1.11.0 in
/usr/local/lib/python3.10/dist-packages (from hvplot) (1.17.1)
Requirement already satisfied: pandas in /usr/local/lib/python3.10/dist-packages
(from hvplot) (1.5.3)
Requirement already satisfied: numpy>=1.15 in /usr/local/lib/python3.10/dist-
packages (from hvplot) (1.25.2)
Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-
packages (from hvplot) (23.2)
Requirement already satisfied: panel>=0.11.0 in /usr/local/lib/python3.10/dist-
packages (from hvplot) (1.3.8)
Requirement already satisfied: param<3.0,>=1.12.0 in
/usr/local/lib/python3.10/dist-packages (from hvplot) (2.0.2)
Requirement already satisfied: Jinja2>=2.9 in /usr/local/lib/python3.10/dist-
packages (from bokeh>=1.0.0->hvplot) (3.1.3)
Requirement already satisfied: contourpy>=1 in /usr/local/lib/python3.10/dist-
packages (from bokeh>=1.0.0->hvplot) (1.2.0)
Requirement already satisfied: pillow>=7.1.0 in /usr/local/lib/python3.10/dist-
packages (from bokeh>=1.0.0->hvplot) (9.4.0)
Requirement already satisfied: PyYAML>=3.10 in /usr/local/lib/python3.10/dist-
packages (from bokeh>=1.0.0->hvplot) (6.0.1)
Requirement already satisfied: tornado>=5.1 in /usr/local/lib/python3.10/dist-
packages (from bokeh>=1.0.0->hvplot) (6.3.2)
Requirement already satisfied: xyzservices>=2021.09.1 in
/usr/local/lib/python3.10/dist-packages (from bokeh>=1.0.0->hvplot) (2023.10.1)
Requirement already satisfied: pyct>=0.4.4 in /usr/local/lib/python3.10/dist-
packages (from colorcet>=2->hvplot) (0.5.0)
Requirement already satisfied: pyviz-comms>=0.7.4 in
/usr/local/lib/python3.10/dist-packages (from holoviews>=1.11.0->hvplot) (3.0.1)
Requirement already satisfied: python-dateutil>=2.8.1 in
/usr/local/lib/python3.10/dist-packages (from pandas->hvplot) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-

5
packages (from pandas->hvplot) (2023.4)
Requirement already satisfied: markdown in /usr/local/lib/python3.10/dist-
packages (from panel>=0.11.0->hvplot) (3.5.2)
Requirement already satisfied: markdown-it-py in /usr/local/lib/python3.10/dist-
packages (from panel>=0.11.0->hvplot) (3.0.0)
Requirement already satisfied: linkify-it-py in /usr/local/lib/python3.10/dist-
packages (from panel>=0.11.0->hvplot) (2.0.3)
Requirement already satisfied: mdit-py-plugins in
/usr/local/lib/python3.10/dist-packages (from panel>=0.11.0->hvplot) (0.4.0)
Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-
packages (from panel>=0.11.0->hvplot) (2.31.0)
Requirement already satisfied: tqdm>=4.48.0 in /usr/local/lib/python3.10/dist-
packages (from panel>=0.11.0->hvplot) (4.66.2)
Requirement already satisfied: bleach in /usr/local/lib/python3.10/dist-packages
(from panel>=0.11.0->hvplot) (6.1.0)
Requirement already satisfied: typing-extensions in
/usr/local/lib/python3.10/dist-packages (from panel>=0.11.0->hvplot) (4.10.0)
Requirement already satisfied: MarkupSafe>=2.0 in
/usr/local/lib/python3.10/dist-packages (from Jinja2>=2.9->bokeh>=1.0.0->hvplot)
(2.1.5)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-
packages (from python-dateutil>=2.8.1->pandas->hvplot) (1.16.0)
Requirement already satisfied: webencodings in /usr/local/lib/python3.10/dist-
packages (from bleach->panel>=0.11.0->hvplot) (0.5.1)
Requirement already satisfied: uc-micro-py in /usr/local/lib/python3.10/dist-
packages (from linkify-it-py->panel>=0.11.0->hvplot) (1.0.3)
Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.10/dist-
packages (from markdown-it-py->panel>=0.11.0->hvplot) (0.1.2)
Requirement already satisfied: charset-normalizer<4,>=2 in
/usr/local/lib/python3.10/dist-packages (from requests->panel>=0.11.0->hvplot)
(3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-
packages (from requests->panel>=0.11.0->hvplot) (3.6)
Requirement already satisfied: urllib3<3,>=1.21.1 in
/usr/local/lib/python3.10/dist-packages (from requests->panel>=0.11.0->hvplot)
(2.0.7)
Requirement already satisfied: certifi>=2017.4.17 in
/usr/local/lib/python3.10/dist-packages (from requests->panel>=0.11.0->hvplot)
(2024.2.2)
Installing collected packages: hvplot
Successfully installed hvplot-0.9.2

[ ]: import pandas as pd
from holoviews import opts
import hvplot.pandas
df = pd.read_csv("Countries Population.csv")
# Remove commas and convert 'Population -2023' to numeric

6
df['Population -2023'] = pd.to_numeric(df['Population -2023'].str.replace(',',␣
↪''))

#print(df)
# Plotting with hvplot.scatter
scatter = df.hvplot.scatter(x='World Share', y='Country (or dependency)',
size='Population -2023', color='blue',
width=800, height=500)
scatter.opts(opts.Scatter(tools=['hover'], color='blue', size=5))

---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-7-8d215d7d0d6d> in <cell line: 4>()
2 from holoviews import opts
3 import hvplot.pandas
----> 4 df = pd.read_csv("Countries Population.csv")
5 # Remove commas and convert 'Population -2023' to numeric
6 df['Population -2023'] = pd.to_numeric(df['Population -2023'].str.
↪replace(',', ''))

/usr/local/lib/python3.10/dist-packages/pandas/util/_decorators.py in␣
↪wrapper(*args, **kwargs)

209 else:
210 kwargs[new_arg_name] = new_arg_value
--> 211 return func(*args, **kwargs)
212
213 return cast(F, wrapper)

/usr/local/lib/python3.10/dist-packages/pandas/util/_decorators.py in␣
↪wrapper(*args, **kwargs)

329 stacklevel=find_stack_level(),
330 )
--> 331 return func(*args, **kwargs)
332
333 # error: "Callable[[VarArg(Any), KwArg(Any)], Any]" has no

/usr/local/lib/python3.10/dist-packages/pandas/io/parsers/readers.py in␣
↪read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col,␣
↪usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters,␣
↪true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows,␣
↪na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates,␣
↪infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates,␣
↪iterator, chunksize, compression, thousands, decimal, lineterminator,␣
↪quotechar, quoting, doublequote, escapechar, comment, encoding,␣
↪encoding_errors, dialect, error_bad_lines, warn_bad_lines, on_bad_lines,␣

↪delim_whitespace, low_memory, memory_map, float_precision, storage_options)

948 kwds.update(kwds_defaults)
949
--> 950 return _read(filepath_or_buffer, kwds)
951

7
952

/usr/local/lib/python3.10/dist-packages/pandas/io/parsers/readers.py in␣
↪_read(filepath_or_buffer, kwds)

603
604 # Create the parser.
--> 605 parser = TextFileReader(filepath_or_buffer, **kwds)
606
607 if chunksize or iterator:

/usr/local/lib/python3.10/dist-packages/pandas/io/parsers/readers.py in␣
↪__init__(self, f, engine, **kwds)

1440
1441 self.handles: IOHandles | None = None
-> 1442 self._engine = self._make_engine(f, self.engine)
1443
1444 def close(self) -> None:

/usr/local/lib/python3.10/dist-packages/pandas/io/parsers/readers.py in␣
↪_make_engine(self, f, engine)

1733 if "b" not in mode:


1734 mode += "b"
-> 1735 self.handles = get_handle(
1736 f,
1737 mode,

/usr/local/lib/python3.10/dist-packages/pandas/io/common.py in␣
↪get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text,␣

↪errors, storage_options)

854 if ioargs.encoding and "b" not in ioargs.mode:


855 # Encoding
--> 856 handle = open(
857 handle,
858 ioargs.mode,

FileNotFoundError: [Errno 2] No such file or directory: 'Countries Population.


↪csv'

[ ]:

You might also like