Path: blob/main/py-polars/tests/unit/operations/namespaces/test_plot.py
6940 views
import altair as alt12import polars as pl345def test_dataframe_plot() -> None:6# dry-run, check nothing errors7df = pl.DataFrame(8{9"length": [1, 4, 6],10"width": [4, 5, 6],11"species": ["setosa", "setosa", "versicolor"],12}13)14df.plot.line(x="length", y="width", color="species").to_json()15df.plot.point(x="length", y="width", size="species").to_json()16df.plot.scatter(x="length", y="width", size="species").to_json()17df.plot.bar(x="length", y="width", color="species").to_json()18df.plot.area(x="length", y="width", color="species").to_json()192021def test_dataframe_plot_tooltip() -> None:22df = pl.DataFrame(23{24"length": [1, 4, 6],25"width": [4, 5, 6],26"species": ["setosa", "setosa", "versicolor"],27}28)29result = df.plot.line(x="length", y="width", color="species").to_dict()30assert result["mark"]["tooltip"] is True31result = df.plot.line(32x="length", y="width", color="species", tooltip=["length", "width"]33).to_dict()34assert result["encoding"]["tooltip"] == [35{"field": "length", "type": "quantitative"},36{"field": "width", "type": "quantitative"},37]383940def test_series_plot() -> None:41# dry-run, check nothing errors42s = pl.Series("a", [1, 4, 4, 4, 7, 2, 5, 3, 6])43s.plot.kde().to_json()44s.plot.hist().to_json()45s.plot.line().to_json()46s.plot.point().to_json()474849def test_series_plot_tooltip() -> None:50s = pl.Series("a", [1, 4, 4, 4, 7, 2, 5, 3, 6])51result = s.plot.line().to_dict()52assert result["mark"]["tooltip"] is True53result = s.plot.line(tooltip=["a"]).to_dict()54assert result["encoding"]["tooltip"] == [{"field": "a", "type": "quantitative"}]555657def test_empty_dataframe() -> None:58pl.DataFrame({"a": [], "b": []}).plot.point(x="a", y="b")596061def test_nameless_series() -> None:62pl.Series([1, 2, 3]).plot.kde().to_json()636465def test_x_with_axis_18830() -> None:66df = pl.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]})67result = df.plot.line(x=alt.X("a", axis=alt.Axis(labelAngle=-90))).to_dict()68assert result["mark"]["tooltip"] is True697071def test_errorbar_19787() -> None:72df = pl.DataFrame({"A": [0, 1, 2], "B": [10, 11, 12], "C": [1, 2, 3]})73result = df.plot.errorbar(x="A", y="B", yError="C").to_dict()74assert "tooltip" not in result["encoding"]75result = df["A"].plot.errorbar().to_dict()76assert "tooltip" not in result["encoding"]777879