Python scatter function parameters and Usage Details, pythonscatter

Source: Internet
Author: User

Python scatter function parameters and Usage Details, pythonscatter

Recently I started to learn Python programming. When I encountered a scatter function, I felt that the parameters in it didn't know what it meant. So I checked the information and summarized it as follows:

1. scatter function prototype

2. the scatter plot shape parameter marker is as follows:

3. The color parameter c is as follows:

4. The basic usage is as follows:

# Import required modules import numpy as np import matplotlib. pyplot as plt # Generate Test Data x = np. arange (1, 10) y = x fig = plt. figure () ax1 = fig. add_subplot (111) # Set the title ax1.set _ title ('scatter plot') # Set the X axis tag plt. xlabel ('x') # Set the Y axis label plt. ylabel ('y') # scatter plot ax1.scatter (x, Y, c = 'R', marker = 'O') # Set the icon plt. legend ('x1') # displays the drawn graph plt. show ()

The result is as follows:

5. How to Use arrays in parameters after scatter, such as s. When s is an array of the same size as x, each vertex in x corresponds to a size in s, and others such as c, and so on, as follows:

(1) Different Sizes

# Import required modules import numpy as np import matplotlib. pyplot as plt # Generate Test Data x = np. arange (1, 10) y = x fig = plt. figure () ax1 = fig. add_subplot (111) # Set the title ax1.set _ title ('scatter plot') # Set the X axis tag plt. xlabel ('x') # Set the Y axis label plt. ylabel ('y') # scatter plot sValue = x * 10 ax1.scatter (x, Y, s = sValue, c = 'R', marker = 'X ') # Set the icon plt. legend ('x1') # displays the drawn graph plt. show ()

(2) Different colors

# Import required modules import numpy as np import matplotlib. pyplot as plt # Generate Test Data x = np. arange (1, 10) y = x fig = plt. figure () ax1 = fig. add_subplot (111) # Set the title ax1.set _ title ('scatter plot') # Set the X axis tag plt. xlabel ('x') # Set the Y axis label plt. ylabel ('y') # scatter plot cValue = ['R', 'y', 'G', 'B', 'R', 'y ', 'G', 'B', 'R'] ax1.scatter (x, y, c = cValue, marker = 's') # Set the icon plt. legend ('x1') # displays the drawn graph plt. show ()

Result:

(3) linewidths

# Import required modules import numpy as np import matplotlib. pyplot as plt # Generate Test Data x = np. arange (1, 10) y = x fig = plt. figure () ax1 = fig. add_subplot (111) # Set the title ax1.set _ title ('scatter plot') # Set the X axis tag plt. xlabel ('x') # Set the Y axis label plt. ylabel ('y') # scatter plot lValue = x ax1.scatter (x, Y, c = 'R', s = 100, linewidths = lValue, marker = 'O ') # Set the icon plt. legend ('x1') # displays the drawn graph plt. show ()

Note: This is the basic usage of scatter.

PS: The following is an example.

This article records the data visualization in python-the scatter chart scatter, so that x is used as the data (50 points, each 30 dimensions). We only visualize the first two dimensions. Labels is its category (assume there are three categories ).

Here, "x" uses random to analyze specific data.

Label is set to []-> 1, []-> 2, [36: 50]-> 3, (array connection method in python: Convert to list first, +, and then returns to array)

Use matplotlib's scatter to draw a scatter chart. legend and matlab are slightly different. For details, see the code.

x = rand(50,30) from numpy import * import matplotlib import matplotlib.pyplot as plt  #basic f1 = plt.figure(1) plt.subplot(211) plt.scatter(x[:,1],x[:,0])  # with label plt.subplot(212) label = list(ones(20))+list(2*ones(15))+list(3*ones(15)) label = array(label) plt.scatter(x[:,1],x[:,0],15.0*label,15.0*label)  # with legend f2 = plt.figure(2) idx_1 = find(label==1) p1 = plt.scatter(x[idx_1,1], x[idx_1,0], marker = 'x', color = 'm', label='1', s = 30) idx_2 = find(label==2) p2 = plt.scatter(x[idx_2,1], x[idx_2,0], marker = '+', color = 'c', label='2', s = 50) idx_3 = find(label==3) p3 = plt.scatter(x[idx_3,1], x[idx_3,0], marker = 'o', color = 'r', label='3', s = 15) plt.legend(loc = 'upper right') 

Result:

Figure (1 ):


Figure (2 ):

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.