Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Pandas makes drawing more beautiful

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how Pandas makes the drawing more beautiful. I hope you will get something after reading this article. Let's discuss it together.

The drawing function in Pandas, a popular Python data analysis library, has always been one of the first choices for drawing charts quickly. However, the available visualization effect is always very rough, more practical and less beautiful.

The author often uses Pandas's drawing function to quickly perform some visual data exploration, but when introducing data insight, I will use a "more beautiful" drawing library (such as Plotly or Bokeh) to redo visualization.

Since the latest Pandas version 0.25.3 was released, there is no need to do this, and now we can use a third-party visualization library as the back end of the Pandas drawing function. Plotly is a popular Python library for interactive visualization based on web, which recently released the Pandas drawing back-end.

Take a look at how to use the Plotly and Bokeh backend to create richer visualizations.

Use a different backend

Different backends that want to activate the drawing feature need to add this line of code after importing pandas:

Pd.options.plotting.backend = 'plotly'

Currently available backends are:

Plotly

Holoviews

Matplotlib

Pandas _ bokeh

Hyplot

Plotly backend

Plotly is a Python library that supports rich interactive visualization effects. One of the benefits of the Plotly package is that it is built on top of the Javascript version of the library, which means that the diagram is based on Web and can be displayed as a HTML file or embedded in a Python-based Web application. Users can also download visual content into high-quality image files for use in documents or papers.

Here are some quick examples of Plotly as a Pandas drawing backend.

If you don't already have Plotly installed, you need to use pip intsall plotly to install it. If you are using Plotly in Jupyterlab, you need to perform several additional installation steps to show the visualization. First, install IPywaidgets:

Pipenv install jupyterlab "ipywidgets > = 7.5" pip install jupyterlab "ipywidgets > = 7.5"

Then run the following command to install the Plotly extension:

Jupyter labextension install jupyterlab-plotly@4.8.1

To illustrate the use of the drawing back end, use a dataset named "wine" in openml.org.

Import pandas as pd import numpy as np from sklearn.datasets import fetch_openml pd.options.plotting.backend = 'plotly' XMagi y = fetch_openml ("wine", version=1, as_frame=True, return_X_y=True) data = pd.concat ([XMagi y], axis=1) data.head ()

The data set consists of multiple features and corresponding labels of all kinds of wines. The following figure shows the first few rows of the dataset.

The drawing function works in much the same way as the standard Pandas drawing function, but now the visualization is as rich as Plotly. The following code draws the relationship between the two features in the dataset.

Fig = data [['Alcohol',' Proline']] .plot.roomter (yawned Alcoholic, Xerographic Proline') fig.show ()

You can summarize the average tonal differences between classes by combining the groupby function of Pandas to create a bar chart:

Data [['Hue','class']] .groupby ([' class']) .mean () .plot.bar ()

Add the class to the previously created scatter chart. With Plotly, you can easily give each class a different color to visually distinguish:

Fig = data [['Alcohol',' Proline']] .plot.roomter (yawned Alcoholic, Xerographic Proline') fig.show ()

Bokeh backend

Bokeh can also provide rich interactive visualization. Its visual charts can be viewed in Web browsers, embedded in Web applications, or used to create interactive dashboards. Bokeh even has a streaming API that creates real-time visual charts for streaming data, such as financial market data.

Libraries can be installed through pip:

Pip install pandas-bokeh

To display the visualization of Bokeh in Jupyterlab, you need to install two new extensions:

Jupyterlabextension install @ jupyter-widgets/jupyterlab-managerjupyterlabextension install @ bokeh/jupyter_bokeh

Recreate the previous scatter chart using the Bokeh backend:

Pd.options.plotting.backend = 'pandas_bokeh' import pandas_bokeh from bokeh.io import output_notebook from bokeh.plotting import figure, show output_notebook () p1 = data.plot_bokeh.scatter Yawned production lines, category='class', title='Proline and Hue by wine class' Show_figure=False) show (p1)

The visualization results are as follows:

Bokeh has a plot_grid function that creates a dashboard layout for multiple charts. The following code creates four charts in the grid layout:

Output_notebook () p1 = data.plot_bokeh.scatter (x-ray hub, y-bag profile, category='class', title='Proline and Hue by wine class') Show_figure=False) p2 = data [['Hue'] 'class']] .groupby ([' class']) .mean () .plot.bar (title='Mean Hue per Class') df_hue = pd.DataFrame ({'class_1': data [data [' class'] = ='1'] ['Hue'],' class_2': data [data ['class'] = =' 2'] ['Hue'] 'class_3': data [data [' class'] = ='3'] ['Hue']}, columns= [' class_1', 'class_2' 'class_3']) p3 = df_hue.plot_bokeh.hist (title='Distribution perClass: Hue') df_proline = pd.DataFrame ({' class_1': data [data ['class'] = =' 1'] ['Proline'],' class_2': data [data ['class'] = =' 2'] ['Proline'] 'class_3': data [data [' class'] = ='3'] ['Proline']}, columns= [' class_1', 'class_2',' class_3']) p4 = df_proline.plot_bokeh.hist (title='Distribution per Class: Proline') pandas_bokeh.plot_grid ([[p1, p2] [p3, p4]], plot_width=450)

The ability of the library for data visualization is greatly enhanced by adding multiple third-party backends to the built-in Pandas drawing function. From then on, pandas can be both beautiful and practical.

After reading this article, I believe you have a certain understanding of "how Pandas makes drawings more beautiful". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report