Hướng dẫn scatter plot multiple variables python - phân tán biểu đồ nhiều biến python

Scatter plot is a graph in which the values of two variables are plotted along two axes. It is a most basic type of plot that helps you visualize the relationship between two variables.

Concept

  1. What is a Scatter plot?
  2. Basic Scatter plot in python
  3. Correlation with Scatter plot
  4. Changing the color of groups of points
  5. Changing the Color and Marker
  6. Scatter plot with Linear fit plot using seaborn
  7. Scatter Plot with Histograms using seaborn
  8. Bubble plot
  9. Exploratory Analysis using mtcars Dataset
    • Multiple line of best fits
    • Adjusting color and style for different categories
    • Text Annotation in Scatter Plot
    • Bubble Plot with categorical variables
    • Categorical Plot

What is a Scatter plot?

Scatter plot is a graph of two sets of data along the two axes. It is used to visualize the relationship between the two variables.

If the value along the Y axis seem to increase as X axis increases(or decreases), it could indicate a positive (or negative) linear relationship. Whereas, if the points are randomly distributed with no obvious pattern, it could possibly indicate a lack of dependent relationship.

In python matplotlib, the scatterplot can be created using the

# Simple Scatterplot
x = range(50)
y = range(50) + np.random.randint(0,30,50)
plt.scatter(x, y)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.title('Simple Scatter plot')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()
9 or the
# Simple Scatterplot with colored points
x = range(50)
y = range(50) + np.random.randint(0,30,50)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.scatter(x, y, c=y, cmap='Spectral')
plt.colorbar()
plt.title('Simple Scatter plot')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()
0. Using these functions, you can add more feature to your scatter plot, like changing the size, color or shape of the points.

So what is the difference between

# Simple Scatterplot with colored points
x = range(50)
y = range(50) + np.random.randint(0,30,50)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.scatter(x, y, c=y, cmap='Spectral')
plt.colorbar()
plt.title('Simple Scatter plot')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()
1 vs
# Simple Scatterplot with colored points
x = range(50)
y = range(50) + np.random.randint(0,30,50)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.scatter(x, y, c=y, cmap='Spectral')
plt.colorbar()
plt.title('Simple Scatter plot')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()
2?

The difference between the two functions is: with

# Simple Scatterplot
x = range(50)
y = range(50) + np.random.randint(0,30,50)
plt.scatter(x, y)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.title('Simple Scatter plot')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()
9 any property you apply (color, shape, size of points) will be applied across all points whereas in
# Simple Scatterplot with colored points
x = range(50)
y = range(50) + np.random.randint(0,30,50)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.scatter(x, y, c=y, cmap='Spectral')
plt.colorbar()
plt.title('Simple Scatter plot')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()
0 you have more control in each point’s appearance.

That is, in

# Simple Scatterplot with colored points
x = range(50)
y = range(50) + np.random.randint(0,30,50)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.scatter(x, y, c=y, cmap='Spectral')
plt.colorbar()
plt.title('Simple Scatter plot')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()
1 you can have the color, shape and size of each dot (datapoint) to vary based on another variable. Or even the same variable (y). Whereas, with
# Simple Scatterplot
x = range(50)
y = range(50) + np.random.randint(0,30,50)
plt.scatter(x, y)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.title('Simple Scatter plot')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()
9, the properties you set will be applied to all the points in the chart.

First, I am going to import the libraries I will be using.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})

The

# Simple Scatterplot with colored points
x = range(50)
y = range(50) + np.random.randint(0,30,50)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.scatter(x, y, c=y, cmap='Spectral')
plt.colorbar()
plt.title('Simple Scatter plot')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()
7 function is used to change the default parameters of the plot’s figure.

Basic Scatter plot in python

First, let’s create artifical data using the

# Simple Scatterplot with colored points
x = range(50)
y = range(50) + np.random.randint(0,30,50)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.scatter(x, y, c=y, cmap='Spectral')
plt.colorbar()
plt.title('Simple Scatter plot')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()
8. You need to specify the no. of points you require as the arguments.

You can also specify the lower and upper limit of the random variable you need.

Then use the

# Simple Scatterplot with colored points
x = range(50)
y = range(50) + np.random.randint(0,30,50)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.scatter(x, y, c=y, cmap='Spectral')
plt.colorbar()
plt.title('Simple Scatter plot')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()
1 function to draw a scatter plot using matplotlib. You need to specify the variables x and y as arguments.

# Scatterplot of non-random vzriables
x=np.arange(1,10,0.2)
y= np.exp(x)
plt.scatter(x,y)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.title('Exponential Relation dataset')
plt.show()
0 is used to set title to your plot.

# Scatterplot of non-random vzriables
x=np.arange(1,10,0.2)
y= np.exp(x)
plt.scatter(x,y)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.title('Exponential Relation dataset')
plt.show()
1 is used to label the x axis.

# Scatterplot of non-random vzriables
x=np.arange(1,10,0.2)
y= np.exp(x)
plt.scatter(x,y)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.title('Exponential Relation dataset')
plt.show()
2 is used to label the y axis.

Get Free Complete Python Course

Facing the same situation like everyone else?

Build your data science career with a globally recognised, industry-approved qualification. Get the mindset, the confidence and the skills that make Data Scientist so valuable.

Hướng dẫn scatter plot multiple variables python - phân tán biểu đồ nhiều biến python

Get Free Complete Python Course

Build your data science career with a globally recognised, industry-approved qualification. Get the mindset, the confidence and the skills that make Data Scientist so valuable.

# Simple Scatterplot
x = range(50)
y = range(50) + np.random.randint(0,30,50)
plt.scatter(x, y)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.title('Simple Scatter plot')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()

Hướng dẫn scatter plot multiple variables python - phân tán biểu đồ nhiều biến python

You can see that there is a positive linear relation between the points. That is, as X increases, Y increases as well, because the Y is actually just X + random_number.

If you want the color of the points to vary depending on the value of Y (or another variable of same size), specify the color each dot should take using the

# Scatterplot of non-random vzriables
x=np.arange(1,10,0.2)
y= np.exp(x)
plt.scatter(x,y)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.title('Exponential Relation dataset')
plt.show()
3 argument.

You can also provide different variable of same size as X.

# Simple Scatterplot with colored points
x = range(50)
y = range(50) + np.random.randint(0,30,50)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.scatter(x, y, c=y, cmap='Spectral')
plt.colorbar()
plt.title('Simple Scatter plot')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()

Hướng dẫn scatter plot multiple variables python - phân tán biểu đồ nhiều biến python

Lets create a dataset with exponentially increasing relation and visualize the plot.

# Scatterplot of non-random vzriables
x=np.arange(1,10,0.2)
y= np.exp(x)
plt.scatter(x,y)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.title('Exponential Relation dataset')
plt.show()

Hướng dẫn scatter plot multiple variables python - phân tán biểu đồ nhiều biến python

# Scatterplot of non-random vzriables
x=np.arange(1,10,0.2)
y= np.exp(x)
plt.scatter(x,y)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.title('Exponential Relation dataset')
plt.show()
4 is used to create a dataset between the lower limit and upper limit with a step of ‘interval’ no. of points.

Now you can see that there is a exponential relation between the x and y axis.

Correlation with Scatter plot

1) If the value of y increases with the value of x, then we can say that the variables have a positive correlation.

2) If the value of y decreases with the value of x, then we can say that the variables have a negative correlation.

3) If the value of y changes randomly independent of x, then it is said to have a zero corelation.

# Scatterplot and Correlations
# Data
x=np.random.randn(100)
y1= x*5 +9 
y2= -5*x
y3=np.random.randn(100)

# Plot
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.scatter(x, y1, label=f'y1 Correlation = {np.round(np.corrcoef(x,y1)[0,1], 2)}')
plt.scatter(x, y2, label=f'y2 Correlation = {np.round(np.corrcoef(x,y2)[0,1], 2)}')
plt.scatter(x, y3, label=f'y3 Correlation = {np.round(np.corrcoef(x,y3)[0,1], 2)}')

# Plot
plt.title('Scatterplot and Correlations')
plt.legend()
plt.show()

Hướng dẫn scatter plot multiple variables python - phân tán biểu đồ nhiều biến python

In the above graph, you can see that the blue line shows an positive correlation, the orange line shows a negative corealtion and the green dots show no relation with the x values(it changes randomly independently).

Changing the color of groups of points

Use the

# Scatterplot of non-random vzriables
x=np.arange(1,10,0.2)
y= np.exp(x)
plt.scatter(x,y)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.title('Exponential Relation dataset')
plt.show()
5 command to change the colour to represent scatter plot.

# Scatterplot - Color Change
x = np.random.randn(50)
y1 = np.random.randn(50)
y2= np.random.randn(50)

# Plot
plt.scatter(x,y1,color='blue')
plt.scatter(x,y2,color= 'red')
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})

# Decorate
plt.title('Color Change')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()

Hướng dẫn scatter plot multiple variables python - phân tán biểu đồ nhiều biến python

Changing the Color and Marker

Use the

# Scatterplot of non-random vzriables
x=np.arange(1,10,0.2)
y= np.exp(x)
plt.scatter(x,y)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.title('Exponential Relation dataset')
plt.show()
6 command to change the marker type in scatter plot.

[‘.’,’o’,’v’,’^’,’>’,'&lt;‘,’s’,’p’,’*’,’h’,’H’,’D’,’d’,’1′,”,”] – These are the types of markers that you can use for your plot.<p></p> <pre><code class="lang-python"><span class="hljs-comment"># Scatterplot of different distributions. Color and Shape of Points.</span> x = np.random.randn(<span class="hljs-number">500</span>) y1 = np.random.randn(<span class="hljs-number">500</span>) y2 = np.random.chisquare(<span class="hljs-number">10</span>, <span class="hljs-number">500</span>) y3 = np.random.poisson(<span class="hljs-number">5</span>, <span class="hljs-number">500</span>) # Plot plt.rcParams.update({<span class="hljs-string">'figure.figsize'</span>:(<span class="hljs-number">10</span>,<span class="hljs-number">8</span>), <span class="hljs-string">'figure.dpi'</span>:<span class="hljs-number">100</span>}) plt.scatter(x,y1,color=<span class="hljs-string">'blue'</span>, marker= <span class="hljs-string">'*'</span>, <span class="hljs-keyword">label</span><span class="bash">=<span class="hljs-string">'Standard Normal'</span>) </span>plt.scatter(x,y2,color= <span class="hljs-string">'red'</span>, marker=<span class="hljs-string">'v'</span>, <span class="hljs-keyword">label</span><span class="bash">=<span class="hljs-string">'Chi-Square'</span>) </span>plt.scatter(x,y3,color= <span class="hljs-string">'green'</span>, marker=<span class="hljs-string">'.'</span>, <span class="hljs-keyword">label</span><span class="bash">=<span class="hljs-string">'Poisson'</span>) </span> # Decorate plt.title(<span class="hljs-string">'Distributions: Color and Shape change'</span>) plt.xlabel(<span class="hljs-string">'X - value'</span>) plt.ylabel(<span class="hljs-string">'Y - value'</span>) plt.legend(loc=<span class="hljs-string">'best'</span>) plt.show() </code></pre> <p><a href="https://www.machinelearningplus.com/wp-content/uploads/2020/04/different-color-and-styles.png"><img data-attachment-id="2787" data-permalink="https://www.machinelearningplus.com/plots/python-scatter-plot/attachment/different-color-and-styles/" data-orig-file="https://www.machinelearningplus.com/wp-content/uploads/2020/04/different-color-and-styles.png" data-orig-size="844,689" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="different color and styles" data-image-description data-image-caption data-medium-file="https://www.machinelearningplus.com/wp-content/uploads/2020/04/different-color-and-styles-300x245.png" data-large-file="https://www.machinelearningplus.com/wp-content/uploads/2020/04/different-color-and-styles.png" loading="lazy" class="alignnone size-full wp-image-2787" src="https://www.machinelearningplus.com/wp-content/uploads/2020/04/different-color-and-styles.png" alt width="844" height="689" srcset="https://www.machinelearningplus.com/wp-content/uploads/2020/04/different-color-and-styles.png 844w, https://www.machinelearningplus.com/wp-content/uploads/2020/04/different-color-and-styles-300x245.png 300w, https://www.machinelearningplus.com/wp-content/uploads/2020/04/different-color-and-styles-768x627.png 768w" sizes="(max-width: 844px) 100vw, 844px" /></a></p> <h2 id="scatter-plot-with-linear-fit-plot-using-seaborn">Scatter Plot with Linear fit plot using Seaborn</h2> <p>Lets try to fit the dataset for the best fitting line using the <code>lmplot()</code> function in seaborn.</p> <p>Lets use the mtcars dataset.</p> <p>You can download the dataset from the given address: <a href="https://www.kaggle.com/ruiromanini/mtcars/download">https://www.kaggle.com/ruiromanini/mtcars/download</a></p> <p>Now lets try whether there is a linear fit between the <code>mpg</code> and the <code>displ</code> column .<span id="ezoic-pub-ad-placeholder-612" class="ezoic-adpicker-ad"></span><span class="ezoic-ad ezoic-at-0 large-mobile-banner-1 large-mobile-banner-1612 adtester-container adtester-container-612" data-ez-name="machinelearningplus_com-large-mobile-banner-1"><span id="div-gpt-ad-machinelearningplus_com-large-mobile-banner-1-0" ezaw="336" ezah="280" style="position:relative;z-index:0;display:inline-block;padding:0;width:100%;max-width:1200px;margin-left:auto!important;margin-right:auto!important;min-height:280px;min-width:336px" class="ezoic-ad"><script data-ezscrex="false" data-cfasync="false" type="text/javascript" style="display:none">if(typeof ez_ad_units != 'undefined'){ez_ad_units.push([[336,280],'machinelearningplus_com-large-mobile-banner-1','ezslot_9',612,'0','0'])};__ez_fad_position('div-gpt-ad-machinelearningplus_com-large-mobile-banner-1-0');

# Linear - Line of best fit import
seaborn as sns url = 'https://gist.githubusercontent.com/seankross/a412dfbd88b3db70b74b/raw/5f23f993cd87c283ce766e7ac6b329ee7cc2e1d1/mtcars.csv' df=pd.read_csv(url) plt.rcParams.update({'figure.figsize':(10,8),
'figure.dpi':100}) sns.lmplot(x='mpg', y='disp', data=df) plt.title("Scatter Plot with
Linear fit"); 

Hướng dẫn scatter plot multiple variables python - phân tán biểu đồ nhiều biến python

You can see that we are getting a negative corelation between the 2 columns.

Mục tiêu của phân tích thăm dò là để hiểu mối quan hệ giữa các thông số kỹ thuật và số dặm khác nhau.

df=pd.read_csv("mtcars.csv")
df.head()

Hướng dẫn scatter plot multiple variables python - phân tán biểu đồ nhiều biến python

Bạn có thể thấy rằng bộ dữ liệu chứa các thông tin khác nhau về một chiếc xe hơi.

Trước tiên, hãy để Lôi thấy một biểu đồ phân tán để thấy sự phân phối giữa

# Scatterplot of non-random vzriables
x=np.arange(1,10,0.2)
y= np.exp(x)
plt.scatter(x,y)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.title('Exponential Relation dataset')
plt.show()
9 và
# Scatterplot and Correlations
# Data
x=np.random.randn(100)
y1= x*5 +9 
y2= -5*x
y3=np.random.randn(100)

# Plot
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.scatter(x, y1, label=f'y1 Correlation = {np.round(np.corrcoef(x,y1)[0,1], 2)}')
plt.scatter(x, y2, label=f'y2 Correlation = {np.round(np.corrcoef(x,y2)[0,1], 2)}')
plt.scatter(x, y3, label=f'y3 Correlation = {np.round(np.corrcoef(x,y3)[0,1], 2)}')

# Plot
plt.title('Scatterplot and Correlations')
plt.legend()
plt.show()
0 và phân phối biểu đồ của chúng. Bạn có thể làm điều này bằng cách sử dụng chức năng
# Scatterplot and Correlations
# Data
x=np.random.randn(100)
y1= x*5 +9 
y2= -5*x
y3=np.random.randn(100)

# Plot
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.scatter(x, y1, label=f'y1 Correlation = {np.round(np.corrcoef(x,y1)[0,1], 2)}')
plt.scatter(x, y2, label=f'y2 Correlation = {np.round(np.corrcoef(x,y2)[0,1], 2)}')
plt.scatter(x, y3, label=f'y3 Correlation = {np.round(np.corrcoef(x,y3)[0,1], 2)}')

# Plot
plt.title('Scatterplot and Correlations')
plt.legend()
plt.show()
1 ở Seaborn.

# joint plot for finding distribution
sns.jointplot(x=df["mpg"], y=df["disp"],color='red', kind='scatter')
# Simple Scatterplot
x = range(50)
y = range(50) + np.random.randint(0,30,50)
plt.scatter(x, y)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.title('Simple Scatter plot')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()
0

Hướng dẫn scatter plot multiple variables python - phân tán biểu đồ nhiều biến python

Nhiều dòng phù hợp tốt nhất

Nếu bạn cần thực hiện regrssion tuyến tính phù hợp với nhiều loại tính năng giữa X và Y, như trong trường hợp này, tôi sẽ chia thêm các danh mục được tích lũy thành

# Scatterplot and Correlations
# Data
x=np.random.randn(100)
y1= x*5 +9 
y2= -5*x
y3=np.random.randn(100)

# Plot
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.scatter(x, y1, label=f'y1 Correlation = {np.round(np.corrcoef(x,y1)[0,1], 2)}')
plt.scatter(x, y2, label=f'y2 Correlation = {np.round(np.corrcoef(x,y2)[0,1], 2)}')
plt.scatter(x, y3, label=f'y3 Correlation = {np.round(np.corrcoef(x,y3)[0,1], 2)}')

# Plot
plt.title('Scatterplot and Correlations')
plt.legend()
plt.show()
2 và cố gắng phù hợp với một đường tuyến tính cho phù hợp. Đối với điều này, hãy sử dụng đối số
# Scatterplot and Correlations
# Data
x=np.random.randn(100)
y1= x*5 +9 
y2= -5*x
y3=np.random.randn(100)

# Plot
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.scatter(x, y1, label=f'y1 Correlation = {np.round(np.corrcoef(x,y1)[0,1], 2)}')
plt.scatter(x, y2, label=f'y2 Correlation = {np.round(np.corrcoef(x,y2)[0,1], 2)}')
plt.scatter(x, y3, label=f'y3 Correlation = {np.round(np.corrcoef(x,y3)[0,1], 2)}')

# Plot
plt.title('Scatterplot and Correlations')
plt.legend()
plt.show()
3 trong hàm
# Scatterplot and Correlations
# Data
x=np.random.randn(100)
y1= x*5 +9 
y2= -5*x
y3=np.random.randn(100)

# Plot
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.scatter(x, y1, label=f'y1 Correlation = {np.round(np.corrcoef(x,y1)[0,1], 2)}')
plt.scatter(x, y2, label=f'y2 Correlation = {np.round(np.corrcoef(x,y2)[0,1], 2)}')
plt.scatter(x, y3, label=f'y3 Correlation = {np.round(np.corrcoef(x,y3)[0,1], 2)}')

# Plot
plt.title('Scatterplot and Correlations')
plt.legend()
plt.show()
4.

# Simple Scatterplot
x = range(50)
y = range(50) + np.random.randint(0,30,50)
plt.scatter(x, y)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.title('Simple Scatter plot')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()
1

Hướng dẫn scatter plot multiple variables python - phân tán biểu đồ nhiều biến python

Xem rằng chức năng đã phù hợp với 3 dòng khác nhau cho 3 loại bánh răng trong bộ dữ liệu.

Điều chỉnh màu sắc và phong cách cho các loại khác nhau

Tôi đã chia bộ dữ liệu theo các loại thiết bị khác nhau. Sau đó, tôi vẽ chúng một cách riêng biệt bằng cách sử dụng hàm

# Scatterplot and Correlations
# Data
x=np.random.randn(100)
y1= x*5 +9 
y2= -5*x
y3=np.random.randn(100)

# Plot
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.scatter(x, y1, label=f'y1 Correlation = {np.round(np.corrcoef(x,y1)[0,1], 2)}')
plt.scatter(x, y2, label=f'y2 Correlation = {np.round(np.corrcoef(x,y2)[0,1], 2)}')
plt.scatter(x, y3, label=f'y3 Correlation = {np.round(np.corrcoef(x,y3)[0,1], 2)}')

# Plot
plt.title('Scatterplot and Correlations')
plt.legend()
plt.show()
5.

# Simple Scatterplot
x = range(50)
y = range(50) + np.random.randint(0,30,50)
plt.scatter(x, y)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.title('Simple Scatter plot')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()
2
# Simple Scatterplot
x = range(50)
y = range(50) + np.random.randint(0,30,50)
plt.scatter(x, y)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.title('Simple Scatter plot')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()
3

Hướng dẫn scatter plot multiple variables python - phân tán biểu đồ nhiều biến python

Chú thích văn bản trong biểu đồ phân tán

Nếu bạn cần thêm bất kỳ văn bản nào trong biểu đồ của bạn, hãy sử dụng hàm

# Scatterplot and Correlations
# Data
x=np.random.randn(100)
y1= x*5 +9 
y2= -5*x
y3=np.random.randn(100)

# Plot
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.scatter(x, y1, label=f'y1 Correlation = {np.round(np.corrcoef(x,y1)[0,1], 2)}')
plt.scatter(x, y2, label=f'y2 Correlation = {np.round(np.corrcoef(x,y2)[0,1], 2)}')
plt.scatter(x, y3, label=f'y3 Correlation = {np.round(np.corrcoef(x,y3)[0,1], 2)}')

# Plot
plt.title('Scatterplot and Correlations')
plt.legend()
plt.show()
6 với văn bản và tọa độ nơi bạn cần thêm văn bản làm đối số.

# Simple Scatterplot
x = range(50)
y = range(50) + np.random.randint(0,30,50)
plt.scatter(x, y)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.title('Simple Scatter plot')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()
4
# Simple Scatterplot
x = range(50)
y = range(50) + np.random.randint(0,30,50)
plt.scatter(x, y)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.title('Simple Scatter plot')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()
5

Hướng dẫn scatter plot multiple variables python - phân tán biểu đồ nhiều biến python

Biểu đồ bong bóng với các biến phân loại

Thông thường bạn sẽ sử dụng 2 varibales để vẽ biểu đồ phân tán (x và y), sau đó tôi đã thêm một biến phân loại khác

# Scatterplot and Correlations
# Data
x=np.random.randn(100)
y1= x*5 +9 
y2= -5*x
y3=np.random.randn(100)

# Plot
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.scatter(x, y1, label=f'y1 Correlation = {np.round(np.corrcoef(x,y1)[0,1], 2)}')
plt.scatter(x, y2, label=f'y2 Correlation = {np.round(np.corrcoef(x,y2)[0,1], 2)}')
plt.scatter(x, y3, label=f'y3 Correlation = {np.round(np.corrcoef(x,y3)[0,1], 2)}')

# Plot
plt.title('Scatterplot and Correlations')
plt.legend()
plt.show()
7 sẽ được ngụ ý bởi màu của các điểm, tôi cũng đã thêm một biến khác
# Scatterplot and Correlations
# Data
x=np.random.randn(100)
y1= x*5 +9 
y2= -5*x
y3=np.random.randn(100)

# Plot
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.scatter(x, y1, label=f'y1 Correlation = {np.round(np.corrcoef(x,y1)[0,1], 2)}')
plt.scatter(x, y2, label=f'y2 Correlation = {np.round(np.corrcoef(x,y2)[0,1], 2)}')
plt.scatter(x, y3, label=f'y3 Correlation = {np.round(np.corrcoef(x,y3)[0,1], 2)}')

# Plot
plt.title('Scatterplot and Correlations')
plt.legend()
plt.show()
8 có giá trị sẽ được ngụ ý theo cường độ của mỗi màu.

# Simple Scatterplot
x = range(50)
y = range(50) + np.random.randint(0,30,50)
plt.scatter(x, y)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.title('Simple Scatter plot')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()
6
# Simple Scatterplot
x = range(50)
y = range(50) + np.random.randint(0,30,50)
plt.scatter(x, y)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.title('Simple Scatter plot')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()
7

Hướng dẫn scatter plot multiple variables python - phân tán biểu đồ nhiều biến python

Tôi đã vẽ giá trị

# Scatterplot of non-random vzriables
x=np.arange(1,10,0.2)
y= np.exp(x)
plt.scatter(x,y)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.title('Exponential Relation dataset')
plt.show()
9 so với giá trị
# Scatterplot and Correlations
# Data
x=np.random.randn(100)
y1= x*5 +9 
y2= -5*x
y3=np.random.randn(100)

# Plot
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.scatter(x, y1, label=f'y1 Correlation = {np.round(np.corrcoef(x,y1)[0,1], 2)}')
plt.scatter(x, y2, label=f'y2 Correlation = {np.round(np.corrcoef(x,y2)[0,1], 2)}')
plt.scatter(x, y3, label=f'y3 Correlation = {np.round(np.corrcoef(x,y3)[0,1], 2)}')

# Plot
plt.title('Scatterplot and Correlations')
plt.legend()
plt.show()
0 và cũng chia chúng thành các màu khác nhau đối với
# Scatterplot - Color Change
x = np.random.randn(50)
y1 = np.random.randn(50)
y2= np.random.randn(50)

# Plot
plt.scatter(x,y1,color='blue')
plt.scatter(x,y2,color= 'red')
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})

# Decorate
plt.title('Color Change')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()
1Value và kích thước của mỗi bong bóng đại diện cho giá trị
# Scatterplot - Color Change
x = np.random.randn(50)
y1 = np.random.randn(50)
y2= np.random.randn(50)

# Plot
plt.scatter(x,y1,color='blue')
plt.scatter(x,y2,color= 'red')
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})

# Decorate
plt.title('Color Change')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()
2.

# Scatterplot - Color Change
x = np.random.randn(50)
y1 = np.random.randn(50)
y2= np.random.randn(50)

# Plot
plt.scatter(x,y1,color='blue')
plt.scatter(x,y2,color= 'red')
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})

# Decorate
plt.title('Color Change')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()
3 Tham số được sử dụng để điều chỉnh cường độ màu của lô. Nhiều aplha hơn sẽ là cường độ màu.

Cốt truyện phân loại

# Simple Scatterplot
x = range(50)
y = range(50) + np.random.randint(0,30,50)
plt.scatter(x, y)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.title('Simple Scatter plot')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()
8

Hướng dẫn scatter plot multiple variables python - phân tán biểu đồ nhiều biến python

# Scatterplot - Color Change
x = np.random.randn(50)
y1 = np.random.randn(50)
y2= np.random.randn(50)

# Plot
plt.scatter(x,y1,color='blue')
plt.scatter(x,y2,color= 'red')
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})

# Decorate
plt.title('Color Change')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.show()
4 được sử dụng để cung cấp quyền truy cập vào một số hàm cấp trục cho thấy mối quan hệ giữa một biến số và một hoặc nhiều biến phân loại sử dụng một trong một số biểu diễn trực quan.

Sử dụng lệnh

# Scatterplot and Correlations
# Data
x=np.random.randn(100)
y1= x*5 +9 
y2= -5*x
y3=np.random.randn(100)

# Plot
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.scatter(x, y1, label=f'y1 Correlation = {np.round(np.corrcoef(x,y1)[0,1], 2)}')
plt.scatter(x, y2, label=f'y2 Correlation = {np.round(np.corrcoef(x,y2)[0,1], 2)}')
plt.scatter(x, y3, label=f'y3 Correlation = {np.round(np.corrcoef(x,y3)[0,1], 2)}')

# Plot
plt.title('Scatterplot and Correlations')
plt.legend()
plt.show()
3 để chia thêm dữ liệu thành các loại khác.

Bài viết đề xuất

  1. Top 50 trực quan hóa matplotlib
  2. Hướng dẫn Matplotlib
  3. Matplotlib pyplot
  4. Biểu đồ matplotlib
  5. Biểu đồ thanh trong Python
  6. Biểu đồ hộp trong Python

Các sơ đồ phân tán có thể có 3 biến?

Không giống như biểu đồ phân tán XY cổ điển, biểu đồ phân tán 3D hiển thị các điểm dữ liệu trên ba trục (X, Y và Z) để hiển thị mối quan hệ giữa ba biến.Do đó, nó thường được gọi là cốt truyện XYZ.a 3D scatter plot displays data points on three axes (x, y, and z) in order to show the relationship between three variables. Therefore, it is often called an XYZ plot.

Một biểu đồ phân tán có thể có nhiều hơn 2 biến?

Bạn có thể có nhiều hơn một biến phụ thuộc.Bộ dữ liệu của bạn có thể bao gồm nhiều hơn một biến phụ thuộc và bạn vẫn có thể theo dõi điều này trên một biểu đồ phân tán.Điều duy nhất bạn muốn thay đổi là màu của từng biến phụ thuộc để bạn có thể đo chúng với nhau trên biểu đồ phân tán.. Your data set might include more than one dependent variable, and you can still track this on a scatter plot. The only thing you'll want to change is the color of each dependent variable so that you can measure them against each other on the scatter plot.

Làm thế nào để bạn vẽ hai biến trong Python?

100 xp..
Sử dụng PLT.Các ô con để tạo một đối tượng hình và trục được gọi là FIG và AX, tương ứng ..
Vẽ biểu đồ biến carbon dioxide trong màu xanh bằng phương pháp biểu đồ trục ..
Sử dụng phương thức Axes Twinx để tạo một trục đôi chia sẻ trục x ..
Vẽ biểu đồ biến nhiệt độ tương đối màu đỏ trên trục đôi bằng phương pháp cốt truyện của nó ..