Drawing and visualization

In data processing and analysis, data visualization is an important part, it can help us better obtain information from complex data more intuitively and effectively. In actual drawing with the python module, there are two most used sections:

1. Use matplotlib to draw images : When I first came into contact, I felt that the charts drawn by matplotlib and matlab are very similar. There are many plotting functions in the matplotlib module that have many similarities with the functions provided by matlab, such as: plot(), imshow (), the figure drawn by matlab has more jaggies, and the effect drawn by matplotlib under python will be more rounded. Among them, it is very convenient to use matlab to quickly generate functions. For example, use ezplot() in matalb to draw a parabola y = x 2 y=x^2and=x2

ezplot('x^2',[-10,10])

Insert picture description hereHowever, you need to import the mpmath package in Python and use the lambda function expression to draw a parabola y = x 2 y=x^2and=x2

import mpmath as mp
mp.plot(lambda x: x*x, [-10, 10])

Insert picture description here

In addition:
1: Matlab not only has ezplot, but also ezmesh to quickly generate images of three-dimensional functions.
2: Python has a sympy package, which specializes in symbolic mathematics, which is a bit similar to the symbolic functions in matlab.

2: Drawing with pandas and seaborn : It is very common to use pandas to draw images. It is worth discussing that after reading data using pandas, you can directly draw the dataframe table. Simply put: in pandas, we There are multiple columns of data, as well as row and column labels. Pandas itself has built-in methods to simplify drawing graphics from DataFrame and Series. In other words: we can draw multiple data with a certain parameter standard, for example:

df = pd.DataFrame(np.random.randn(10, 4).cumsum(0),
                  columns=['A', 'B', 'C', 'D'],
                  index=np.arange(0, 100, 10))
df.plot()

Insert picture description here
In the above figure, there is index=np.arange(0, 100, 10)), that is, 10 numbers are randomly generated as independent variables, there are columns=['A','B','C','D'] Four dependent variables. Compared with matplotlib, this is also the advantage of using pandas for drawing, which reduces the time of writing code. In addition, you can also manipulate the dataframe table using: df.plot.bar(), df.plot.hist(), df.plot. density() draws histograms, histograms, density maps, etc.
Why use seaborn drawing?
I think it's for beauty. The drawing function of seaborn uses the data parameter, which may be a pandas DataFrame. Seaborn has automatically modified the aesthetics of the graphics: the default color palette, the color of the graphics background and grid lines. You can use seaborn.set to switch between different graphical appearances. [too strong! ]
Here is an example of how I did a product sales forecast not long ago to explain, using seaborn to define the grid of the layout.

#seaborn设置背景
# sns.set(color_codes=True)

The image is:
Insert picture description here
[The image is not very beautiful hahaha!
After studying "Data Analysis Using Python", a must-read book for data processing and analysis, I also left my own mind map, including the function call method of each section, and some image drawing techniques.

The following is a mind map (notes) that I learned to draw and visualize :
Insert picture description here
PS: If there are more drawing methods, it will be filled later...

1 Matplotlib plotting

Learning link : Matplotlib official website .

Here is a brief introduction to the commonly used plot() parameters and the general calling form of the plot() function:

#单条线:
plot([x], y, [fmt], data=None, **kwargs)
#多条线
plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)

The parameter [fmt] is a string to define the basic attributes of the graph, such as: color (color), point type (marker), line style (linestyle).

Specific form: fmt ='[color][marker][line]'

For example: plot(x, y,'bo-') draws a solid blue dotted line .
Everyone will have questions, what are'b','o', and'-'? , And point out one by one next.
1.1Keyword parameter color :

  character        color
 =============    ===============================
  ``'b'``          blue 蓝    
  ``'g'``          green 绿
  ``'r'``          red 红    
  ``'c'``          cyan 蓝绿    
  ``'m'``          magenta 洋红    
  ``'y'``          yellow 黄    
  ``'k'``          black 黑    
  ``'w'``          white 白

1.2 Point type parameter Markers :

character        description
=============    ===============================
``'.'``          point marker    
``','``          pixel marker    
``'o'``          circle marker
``'v'``          triangle_down marker    
``'^'``          triangle_up marker    
``'<'``          triangle_left marker
``'>'``          triangle_right marker    
``'1'``          tri_down marker    
``'2'``          tri_up marker
``'3'``          tri_left marker    
``'4'``          tri_right marker    
``'s'``          square marker
``'p'``          pentagon marker    `
`'*'``          star marker    
``'h'``          hexagon1 marker
``'H'``          hexagon2 marker    
``'+'``          plus marker    
``'x'``          x marker    
``'D'``          diamond marker    
``'d'``          thin_diamond marker    
``'|'``          vline marker    
``'_'``          hline marker

1.3 Line parameter Line :

character        description    
=============    ===============================    
``'-'``          solid line style 实线    
``'--'``         dashed line style 虚线    
``'-.'``         dash-dot line style 点画线    
``':'``          dotted line style 点线

Take our example, plot(x, y,'bo-'), to draw the image.
Insert picture description here
In the future, I will be in contact with data processing and analysis for a long time, and will slowly learn other modules of machine learning using python.
Welcome to learn and communicate...

1 Link: Pandas-learning data processing .
2 Link: Numpy-learning data processing .

Guess you like

Origin blog.csdn.net/qq_41709378/article/details/105675095