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

Analysis of basic examples of Python data Visualization

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "Python data Visualization basic example Analysis", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "Python data Visualization basic example Analysis"!

First of all, what libraries do we use to draw pictures?

Matplotlib

The most basic drawing library in python is matplotlib, which is the most basic Python visualization library. Generally, Python data visualization starts from matplotlib, and then begins to expand vertically and horizontally.

Seaborn

Is an advanced visualization effect library based on matplotlib, aiming at the feature selection of variables in data mining and machine learning. Seaborn can use short code to draw visualization effects that describe more dimensional data.

Other libraries include

Bokeh (a library for browser-side interaction visualization that enables analysts to interact with data)

Mapbox (a more powerful visualization tool library for dealing with geographic data engines) and so on.

This article mainly uses matplotlib for case study.

Determine the problem, select the drawing

The business may be complex, but after a split, we need to find out what specific problems we want to express graphically.

In python, we can summarize the following four basic visual elements to present graphics:

Points: scatter plot 2D data, suitable for simple 2D relationships

Lines: line plot 2D data, suitable for time series

Column: bar plot 2D data, suitable for category statistics

Color: heatmap is suitable for displaying the third dimension

There are relationships among data, such as distribution, composition, comparison, connection and changing trend. Corresponding to different relationships, select the corresponding graphics to display.

Convert data, apply function

A great deal of programming work in data analysis and modeling is based on data preparation: loading, cleaning, transformation, and reshaping. In the visualization step, we also need to sort out the data, convert it to the format we need, and then use the visualization method to complete the drawing.

Here are some common data conversion methods:

Merge: merge,concat,combine_frist (similar to full external connections in a database)

Reshape: reshape; Axial rotation: pivot (similar to excel PivotTable)

Weight removal: drop_duplicates

Mapping: map

Fill replacement: fillna,replace

Rename axis index: rename

Convert classified variables into the get_dummies function of 'dummy variable matrix', take a limit value for a column of data in df, etc.

The function looks for the corresponding function in python according to the selected graph in the first step.

Parameter setting is clear at a glance

After the original drawing is finished, we can modify the color (color), linetype (linestyle), mark (maker) or other chart decoration item title (Title), axis label (xlabel,ylabel), axis scale (set_xticks), and legend (legend) to make the graph more intuitive.

The third step is on the basis of the second step, in order to make the graphics more clear, do the modification work. The specific parameters can be found in the drawing function.

Below, I summarized some of the basics that will be used to achieve visualization.

The basis of visual drawing

Matplotlib

# Import package import numpy as np import pandas as pdimport matplotlib.pyplot as plt

Figure and Subplot

Matplotlib's graphics are all in the Figure (canvas), and Subplot creates the image space. You cannot draw through figure, you must create one or more subplot with add_subplot.

Figsize can specify the image size.

# create canvas fig = plt.figure () # create subplot,221 indicates that this is the first image in a 2-row and 2-column table. Ax1 = fig.add_subplot (221) # but now more accustomed to using the following methods to create canvases and images. 2Magne2 means that this is a 2p2 canvas that can place four image fig, and the sharex and sharey parameters of axes = plt.subplots (2PowerShaft TrueSharedShaft True) # plt.subplot can specify that all subplot use the same xMagine y-axis scale.

The spacing can be adjusted by using Figure's subplots_adjust method.

Subplots_adjust (left=None,bottom=None,right=None,top=None,wspace=None,hspace=None)

# create canvas fig = plt.figure () # create subplot,221 indicates that this is the first image in a 2-row and 2-column table. Ax1 = fig.add_subplot (221) # but now more accustomed to using the following methods to create canvases and images. 2Magne2 means that this is a 2p2 canvas that can place four image fig, and the sharex and sharey parameters of axes = plt.subplots (2PowerShaft TrueSharedSharedTrue) # plt.subplot can specify that all subplot use the same xMagine y-axis scale.

Python数据可视化基础讲解

Color color, Mark marker, and Linetype linestyle

The plot function of matplotlib accepts a set of X and Y coordinates, as well as a string abbreviation for color and linetype: 'GMurmuri', indicating that the color is green green and the linetype is'--'dashed line. You can also use parameters to specify explicitly.

Linetypes can also be marked (marker) to highlight the location of data points. Tags can also be placed in a format string, but the tag type and linetype must be placed after the color.

Plt.plot (np.random.randn (30), color='g',linestyle='--',marker='o') []

Ticks, labels and legends

Plt's xlim, xticks and xtickslabels methods control the range and, position and label of the chart, respectively. The current parameter value is returned when the method is called without a parameter, and the parameter value is set when the method is called with a parameter.

Plt.plot (np.random.randn (30), color='g',linestyle='--',marker='o') plt.xlim () # calls without parameters to display the current parameters; # you can replace xlim with two other methods (- 1.4500000000000002,30.45) plt.plot (np.random.randn (30), color='g',linestyle='--',marker='o') plt.xlim ([0Magne15]) # the horizontal scale becomes 0-15 (0Magne15)

Set title, axis label, scale and scale label

Fig = plt.figure () Ax = fig.add_subplot (1 My first Plot' 1) ax.plot (np.random.randn (1000). Cumsum () ticks = ax.set_xticks ([0min250 pint 500pje 750pr 1000]) # set scale value labels = ax.set_xticklabels (['one','two','three','four','five']) # set scale label ax.set_title (' My first Plot') # set title ax.set_xlabel ('Stage') # set axis label Text (0.5pc0mt Stage`)

Add Legend

Legend legend is another important tool for identifying icon elements. You can pass in the label parameter when you add subplot.

Fig = plt.figure (figsize= (12pm 5)) Ax = fig.add_subplot (1000) ax.plot (np.random.randn (1000). Cumsum () # pass in the label parameter, and define the label name ax.plot (np.random.randn (1000). Cumsum (), 'KMB (1000)) ax.plot (np.random.randn (1000). Cumsum (),) # after the graph is created, you only need to call the legend parameter to call the label. Ax.legend (loc='best') # if the requirement is not very strict, it is recommended to use the loc='best' parameter to let it choose the best location.

Notes

In addition to standard chart objects, we can also customize the addition of some text notes or arrows.

Annotations can be added through functions such as text,arrow and annotate. The text function can draw the text in the specified XMagne y coordinate location, and can also customize the format.

Plt.plot (np.random.randn (1000). Cumsum ()) plt.text (600, 10) family='monospace',fontsize=10) # Chinese annotations do not display properly in the default environment, so you need to modify the configuration file to support Chinese fonts. Please search for specific steps by yourself.

Save the chart to a file

Using plt.savefig, you can save the current chart to a file. For example, to save the chart as a png file, you can execute

Plt.savefig ('figpath.png')

The file type is based on the extension. Other parameters include:

Fname contains the string of the file path, and the extension specifies the file type

Dpi resolution. Default is 100facecolor.edgcolor image background color. Default is'w 'white.

Format display settings file format ('png','pdf','svg','ps','jpg', etc.)

Bbox_inches: the part of the chart that needs to be retained. If set to "tight", an attempt is made to cut out the white space around the image

Plt.savefig ('. / plot.jpg') # saves the image in jpg format with the name of plot. I believe you have a deeper understanding of "basic example Analysis of Python data Visualization". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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