Path: blob/main/docs/source/user-guide/misc/visualization.md
6940 views
Visualization
Data in a Polars DataFrame
can be visualized using common visualization libraries.
We illustrate plotting capabilities using the Iris dataset. We read a CSV and then plot one column against another, colored by a yet another column.
{{code_block('user-guide/misc/visualization','dataframe',[])}}
Built-in plotting with Altair
Polars has a plot
method to create plots using Altair:
{{code_block('user-guide/misc/visualization','altair_show_plot',[])}}
This is shorthand for:
and is only provided for convenience, and to signal that Altair is known to work well with Polars.
For configuration, we suggest reading Chart Configuration. For example, you can:
Change the width/height/title with
.properties(width=500, height=350, title="My amazing plot")
.Change the x-axis label rotation with
.configure_axisX(labelAngle=30)
.Change the opacity of the points in your scatter plot with
.configure_point(opacity=.5)
.
hvPlot
If you import hvplot.polars
, then it registers a hvplot
method which you can use to create interactive plots using hvPlot.
{{code_block('user-guide/misc/visualization','hvplot_show_plot',[])}}
Matplotlib
To create a scatter plot we can pass columns of a DataFrame
directly to Matplotlib as a Series
for each column. Matplotlib does not have explicit support for Polars objects but can accept a Polars Series
by converting it to a NumPy array (which is zero-copy for numeric data without null values).
Note that because the column 'species'
isn't numeric, we need to first convert it to numeric values so that it can be passed as an argument to c
.
{{code_block('user-guide/misc/visualization','matplotlib_show_plot',[])}}
Plotnine
Plotnine is a reimplementation of ggplot2 in Python, bringing the Grammar of Graphics to Python users with an interface similar to its R counterpart. It supports Polars DataFrame
by internally converting it to a pandas DataFrame
.
{{code_block('user-guide/misc/visualization','plotnine_show_plot',[])}}
Seaborn and Plotly
Seaborn and Plotly can accept a Polars DataFrame
by leveraging the dataframe interchange protocol, which offers zero-copy conversion where possible. Note that the protocol does not support all Polars data types (e.g. List
) so your mileage may vary here.
Seaborn
{{code_block('user-guide/misc/visualization','seaborn_show_plot',[])}}
Plotly
{{code_block('user-guide/misc/visualization','plotly_show_plot',[])}}