Path: blob/main/docs/source/src/python/user-guide/misc/visualization.py
7890 views
# --8<-- [start:dataframe]1import polars as pl23path = "docs/assets/data/iris.csv"45df = pl.read_csv(path)6print(df)7# --8<-- [end:dataframe]89"""10# --8<-- [start:hvplot_show_plot]11import hvplot.polars12df.hvplot.scatter(13x="sepal_width",14y="sepal_length",15by="species",16width=650,17title="Irises",18xlabel='Sepal Width',19ylabel='Sepal Length',20)21# --8<-- [end:hvplot_show_plot]22"""2324# --8<-- [start:hvplot_make_plot]25import hvplot.polars2627plot = df.hvplot.scatter(28x="sepal_width",29y="sepal_length",30by="species",31width=650,32title="Irises",33xlabel="Sepal Width",34ylabel="Sepal Length",35)36hvplot.save(plot, "docs/assets/images/hvplot_scatter.html", resources="cdn")37with open("docs/assets/images/hvplot_scatter.html", "r") as f:38chart_html = f.read()39print(f"{chart_html}")40# --8<-- [end:hvplot_make_plot]4142"""43# --8<-- [start:matplotlib_show_plot]44import matplotlib.pyplot as plt4546fig, ax = plt.subplots()47ax.scatter(48x=df["sepal_width"],49y=df["sepal_length"],50c=df["species"].cast(pl.Categorical).to_physical(),51)52ax.set_title('Irises')53ax.set_xlabel('Sepal Width')54ax.set_ylabel('Sepal Length')55# --8<-- [end:matplotlib_show_plot]56"""5758# --8<-- [start:matplotlib_make_plot]59import base646061import matplotlib.pyplot as plt6263fig, ax = plt.subplots()64ax.scatter(65x=df["sepal_width"],66y=df["sepal_length"],67c=df["species"].cast(pl.Categorical).to_physical(),68)69ax.set_title("Irises")70ax.set_xlabel("Sepal Width")71ax.set_ylabel("Sepal Length")72fig.savefig("docs/assets/images/matplotlib_scatter.png")73with open("docs/assets/images/matplotlib_scatter.png", "rb") as f:74png = base64.b64encode(f.read()).decode()75print(f'<img src="data:image/png;base64, {png}"/>')76# --8<-- [end:matplotlib_make_plot]7778"""79# --8<-- [start:plotnine_show_plot]80from plotnine import ggplot, aes, geom_point, labs8182(83ggplot(df, mapping=aes(x="sepal_width", y="sepal_length", color="species"))84+ geom_point()85+ labs(title="Irises", x="Sepal Width", y="Sepal Length")86)87# --8<-- [end:plotnine_show_plot]88"""8990# --8<-- [start:plotnine_make_plot]91import base649293from plotnine import ggplot, aes, geom_point, labs9495fig_path = "docs/assets/images/plotnine.png"9697(98ggplot(df, mapping=aes(x="sepal_width", y="sepal_length", color="species"))99+ geom_point()100+ labs(title="Irises", x="Sepal Width", y="Sepal Length")101).save(fig_path, dpi=300, verbose=False)102103with open(fig_path, "rb") as f:104png = base64.b64encode(f.read()).decode()105print(f'<img src="data:image/png;base64, {png}"/>')106# --8<-- [end:plotnine_make_plot]107108"""109# --8<-- [start:seaborn_show_plot]110import seaborn as sns111import matplotlib.pyplot as plt112113fig, ax = plt.subplots()114sns.scatterplot(115df,116x="sepal_width",117y="sepal_length",118hue="species",119ax=ax,120)121ax.set_title('Irises')122ax.set_xlabel('Sepal Width')123ax.set_ylabel('Sepal Length')124# --8<-- [end:seaborn_show_plot]125"""126127# --8<-- [start:seaborn_make_plot]128import seaborn as sns129import matplotlib.pyplot as plt130131fig, ax = plt.subplots()132sns.scatterplot(133df,134x="sepal_width",135y="sepal_length",136hue="species",137ax=ax,138)139ax.set_title("Irises")140ax.set_xlabel("Sepal Width")141ax.set_ylabel("Sepal Length")142fig.savefig("docs/assets/images/seaborn_scatter.png")143with open("docs/assets/images/seaborn_scatter.png", "rb") as f:144png = base64.b64encode(f.read()).decode()145print(f'<img src="data:image/png;base64, {png}"/>')146# --8<-- [end:seaborn_make_plot]147148"""149# --8<-- [start:plotly_show_plot]150import plotly.express as px151152px.scatter(153df,154x="sepal_width",155y="sepal_length",156color="species",157width=650,158title="Irises",159labels={'sepal_width': 'Sepal Width', 'sepal_length': 'Sepal Length'}160)161# --8<-- [end:plotly_show_plot]162"""163164# --8<-- [start:plotly_make_plot]165import plotly.express as px166167fig = px.scatter(168df,169x="sepal_width",170y="sepal_length",171color="species",172width=650,173title="Irises",174labels={"sepal_width": "Sepal Width", "sepal_length": "Sepal Length"},175)176fig.write_html(177"docs/assets/images/plotly_scatter.html", full_html=False, include_plotlyjs="cdn"178)179with open("docs/assets/images/plotly_scatter.html", "r") as f:180chart_html = f.read()181print(f"{chart_html}")182# --8<-- [end:plotly_make_plot]183184"""185# --8<-- [start:altair_show_plot]186chart = (187df.plot.point(188x="sepal_width",189y="sepal_length",190color="species",191)192.properties(width=500, title="Irises")193.configure_scale(zero=False)194.configure_axisX(tickMinStep=1)195)196chart.encoding.x.title = "Sepal Width"197chart.encoding.y.title = "Sepal Length"198chart199# --8<-- [end:altair_show_plot]200"""201202# --8<-- [start:altair_make_plot]203chart = (204df.plot.point(205x="sepal_width",206y="sepal_length",207color="species",208)209.properties(width=500, title="Irises")210.configure_scale(zero=False)211.configure_axisX(tickMinStep=1)212)213chart.encoding.x.title = "Sepal Width"214chart.encoding.y.title = "Sepal Length"215chart.save("docs/assets/images/altair_scatter.html")216with open("docs/assets/images/altair_scatter.html", "r") as f:217chart_html = f.read()218print(f"{chart_html}")219# --8<-- [end:altair_make_plot]220221222