Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/operations/namespaces/test_plot.py
6940 views
1
import altair as alt
2
3
import polars as pl
4
5
6
def test_dataframe_plot() -> None:
7
# dry-run, check nothing errors
8
df = pl.DataFrame(
9
{
10
"length": [1, 4, 6],
11
"width": [4, 5, 6],
12
"species": ["setosa", "setosa", "versicolor"],
13
}
14
)
15
df.plot.line(x="length", y="width", color="species").to_json()
16
df.plot.point(x="length", y="width", size="species").to_json()
17
df.plot.scatter(x="length", y="width", size="species").to_json()
18
df.plot.bar(x="length", y="width", color="species").to_json()
19
df.plot.area(x="length", y="width", color="species").to_json()
20
21
22
def test_dataframe_plot_tooltip() -> None:
23
df = pl.DataFrame(
24
{
25
"length": [1, 4, 6],
26
"width": [4, 5, 6],
27
"species": ["setosa", "setosa", "versicolor"],
28
}
29
)
30
result = df.plot.line(x="length", y="width", color="species").to_dict()
31
assert result["mark"]["tooltip"] is True
32
result = df.plot.line(
33
x="length", y="width", color="species", tooltip=["length", "width"]
34
).to_dict()
35
assert result["encoding"]["tooltip"] == [
36
{"field": "length", "type": "quantitative"},
37
{"field": "width", "type": "quantitative"},
38
]
39
40
41
def test_series_plot() -> None:
42
# dry-run, check nothing errors
43
s = pl.Series("a", [1, 4, 4, 4, 7, 2, 5, 3, 6])
44
s.plot.kde().to_json()
45
s.plot.hist().to_json()
46
s.plot.line().to_json()
47
s.plot.point().to_json()
48
49
50
def test_series_plot_tooltip() -> None:
51
s = pl.Series("a", [1, 4, 4, 4, 7, 2, 5, 3, 6])
52
result = s.plot.line().to_dict()
53
assert result["mark"]["tooltip"] is True
54
result = s.plot.line(tooltip=["a"]).to_dict()
55
assert result["encoding"]["tooltip"] == [{"field": "a", "type": "quantitative"}]
56
57
58
def test_empty_dataframe() -> None:
59
pl.DataFrame({"a": [], "b": []}).plot.point(x="a", y="b")
60
61
62
def test_nameless_series() -> None:
63
pl.Series([1, 2, 3]).plot.kde().to_json()
64
65
66
def test_x_with_axis_18830() -> None:
67
df = pl.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]})
68
result = df.plot.line(x=alt.X("a", axis=alt.Axis(labelAngle=-90))).to_dict()
69
assert result["mark"]["tooltip"] is True
70
71
72
def test_errorbar_19787() -> None:
73
df = pl.DataFrame({"A": [0, 1, 2], "B": [10, 11, 12], "C": [1, 2, 3]})
74
result = df.plot.errorbar(x="A", y="B", yError="C").to_dict()
75
assert "tooltip" not in result["encoding"]
76
result = df["A"].plot.errorbar().to_dict()
77
assert "tooltip" not in result["encoding"]
78
79