Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/docs/source/src/python/user-guide/misc/visualization.py
7890 views
1
# --8<-- [start:dataframe]
2
import polars as pl
3
4
path = "docs/assets/data/iris.csv"
5
6
df = pl.read_csv(path)
7
print(df)
8
# --8<-- [end:dataframe]
9
10
"""
11
# --8<-- [start:hvplot_show_plot]
12
import hvplot.polars
13
df.hvplot.scatter(
14
x="sepal_width",
15
y="sepal_length",
16
by="species",
17
width=650,
18
title="Irises",
19
xlabel='Sepal Width',
20
ylabel='Sepal Length',
21
)
22
# --8<-- [end:hvplot_show_plot]
23
"""
24
25
# --8<-- [start:hvplot_make_plot]
26
import hvplot.polars
27
28
plot = df.hvplot.scatter(
29
x="sepal_width",
30
y="sepal_length",
31
by="species",
32
width=650,
33
title="Irises",
34
xlabel="Sepal Width",
35
ylabel="Sepal Length",
36
)
37
hvplot.save(plot, "docs/assets/images/hvplot_scatter.html", resources="cdn")
38
with open("docs/assets/images/hvplot_scatter.html", "r") as f:
39
chart_html = f.read()
40
print(f"{chart_html}")
41
# --8<-- [end:hvplot_make_plot]
42
43
"""
44
# --8<-- [start:matplotlib_show_plot]
45
import matplotlib.pyplot as plt
46
47
fig, ax = plt.subplots()
48
ax.scatter(
49
x=df["sepal_width"],
50
y=df["sepal_length"],
51
c=df["species"].cast(pl.Categorical).to_physical(),
52
)
53
ax.set_title('Irises')
54
ax.set_xlabel('Sepal Width')
55
ax.set_ylabel('Sepal Length')
56
# --8<-- [end:matplotlib_show_plot]
57
"""
58
59
# --8<-- [start:matplotlib_make_plot]
60
import base64
61
62
import matplotlib.pyplot as plt
63
64
fig, ax = plt.subplots()
65
ax.scatter(
66
x=df["sepal_width"],
67
y=df["sepal_length"],
68
c=df["species"].cast(pl.Categorical).to_physical(),
69
)
70
ax.set_title("Irises")
71
ax.set_xlabel("Sepal Width")
72
ax.set_ylabel("Sepal Length")
73
fig.savefig("docs/assets/images/matplotlib_scatter.png")
74
with open("docs/assets/images/matplotlib_scatter.png", "rb") as f:
75
png = base64.b64encode(f.read()).decode()
76
print(f'<img src="data:image/png;base64, {png}"/>')
77
# --8<-- [end:matplotlib_make_plot]
78
79
"""
80
# --8<-- [start:plotnine_show_plot]
81
from plotnine import ggplot, aes, geom_point, labs
82
83
(
84
ggplot(df, mapping=aes(x="sepal_width", y="sepal_length", color="species"))
85
+ geom_point()
86
+ labs(title="Irises", x="Sepal Width", y="Sepal Length")
87
)
88
# --8<-- [end:plotnine_show_plot]
89
"""
90
91
# --8<-- [start:plotnine_make_plot]
92
import base64
93
94
from plotnine import ggplot, aes, geom_point, labs
95
96
fig_path = "docs/assets/images/plotnine.png"
97
98
(
99
ggplot(df, mapping=aes(x="sepal_width", y="sepal_length", color="species"))
100
+ geom_point()
101
+ labs(title="Irises", x="Sepal Width", y="Sepal Length")
102
).save(fig_path, dpi=300, verbose=False)
103
104
with open(fig_path, "rb") as f:
105
png = base64.b64encode(f.read()).decode()
106
print(f'<img src="data:image/png;base64, {png}"/>')
107
# --8<-- [end:plotnine_make_plot]
108
109
"""
110
# --8<-- [start:seaborn_show_plot]
111
import seaborn as sns
112
import matplotlib.pyplot as plt
113
114
fig, ax = plt.subplots()
115
sns.scatterplot(
116
df,
117
x="sepal_width",
118
y="sepal_length",
119
hue="species",
120
ax=ax,
121
)
122
ax.set_title('Irises')
123
ax.set_xlabel('Sepal Width')
124
ax.set_ylabel('Sepal Length')
125
# --8<-- [end:seaborn_show_plot]
126
"""
127
128
# --8<-- [start:seaborn_make_plot]
129
import seaborn as sns
130
import matplotlib.pyplot as plt
131
132
fig, ax = plt.subplots()
133
sns.scatterplot(
134
df,
135
x="sepal_width",
136
y="sepal_length",
137
hue="species",
138
ax=ax,
139
)
140
ax.set_title("Irises")
141
ax.set_xlabel("Sepal Width")
142
ax.set_ylabel("Sepal Length")
143
fig.savefig("docs/assets/images/seaborn_scatter.png")
144
with open("docs/assets/images/seaborn_scatter.png", "rb") as f:
145
png = base64.b64encode(f.read()).decode()
146
print(f'<img src="data:image/png;base64, {png}"/>')
147
# --8<-- [end:seaborn_make_plot]
148
149
"""
150
# --8<-- [start:plotly_show_plot]
151
import plotly.express as px
152
153
px.scatter(
154
df,
155
x="sepal_width",
156
y="sepal_length",
157
color="species",
158
width=650,
159
title="Irises",
160
labels={'sepal_width': 'Sepal Width', 'sepal_length': 'Sepal Length'}
161
)
162
# --8<-- [end:plotly_show_plot]
163
"""
164
165
# --8<-- [start:plotly_make_plot]
166
import plotly.express as px
167
168
fig = px.scatter(
169
df,
170
x="sepal_width",
171
y="sepal_length",
172
color="species",
173
width=650,
174
title="Irises",
175
labels={"sepal_width": "Sepal Width", "sepal_length": "Sepal Length"},
176
)
177
fig.write_html(
178
"docs/assets/images/plotly_scatter.html", full_html=False, include_plotlyjs="cdn"
179
)
180
with open("docs/assets/images/plotly_scatter.html", "r") as f:
181
chart_html = f.read()
182
print(f"{chart_html}")
183
# --8<-- [end:plotly_make_plot]
184
185
"""
186
# --8<-- [start:altair_show_plot]
187
chart = (
188
df.plot.point(
189
x="sepal_width",
190
y="sepal_length",
191
color="species",
192
)
193
.properties(width=500, title="Irises")
194
.configure_scale(zero=False)
195
.configure_axisX(tickMinStep=1)
196
)
197
chart.encoding.x.title = "Sepal Width"
198
chart.encoding.y.title = "Sepal Length"
199
chart
200
# --8<-- [end:altair_show_plot]
201
"""
202
203
# --8<-- [start:altair_make_plot]
204
chart = (
205
df.plot.point(
206
x="sepal_width",
207
y="sepal_length",
208
color="species",
209
)
210
.properties(width=500, title="Irises")
211
.configure_scale(zero=False)
212
.configure_axisX(tickMinStep=1)
213
)
214
chart.encoding.x.title = "Sepal Width"
215
chart.encoding.y.title = "Sepal Length"
216
chart.save("docs/assets/images/altair_scatter.html")
217
with open("docs/assets/images/altair_scatter.html", "r") as f:
218
chart_html = f.read()
219
print(f"{chart_html}")
220
# --8<-- [end:altair_make_plot]
221
222