Sebar plot di python matplotlib

Scatter plot adalah grafik di mana nilai dua variabel diplot sepanjang dua sumbu. Ini adalah jenis plot paling dasar yang membantu Anda memvisualisasikan hubungan antara dua variabel

Konsep

  1. Apa itu plot Pencar?
  2. Plot Scatter dasar dengan python
  3. Korelasi dengan plot Scatter
  4. Mengubah warna kelompok titik
  5. Mengubah Warna dan Penanda
  6. Scatter plot dengan Linear fit plot menggunakan seaborn
  7. Scatter Plot dengan Histogram menggunakan seaborn
  8. Plot gelembung
  9. Analisis Eksplorasi menggunakan mtcars Dataset
    • Beberapa baris yang paling cocok
    • Menyesuaikan warna dan gaya untuk berbagai kategori
    • Anotasi Teks di Scatter Plot
    • Bubble Plot dengan variabel kategori
    • Plot Kategoris

Apa itu plot Pencar?

Scatter plot adalah grafik dari dua set data sepanjang dua sumbu. Ini digunakan untuk memvisualisasikan hubungan antara dua variabel

Jika nilai di sepanjang sumbu Y terlihat meningkat saat sumbu X meningkat (atau menurun), ini dapat mengindikasikan hubungan linier positif (atau negatif). Sedangkan jika titik-titik tersebut terdistribusi secara acak tanpa pola yang jelas, hal tersebut dapat mengindikasikan kurangnya hubungan ketergantungan

Dalam python matplotlib, scatterplot dapat dibuat menggunakan

# 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 atau
# 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()
3. Dengan menggunakan fungsi ini, Anda dapat menambahkan lebih banyak fitur ke plot pencar, seperti mengubah ukuran, warna, atau bentuk titik

Jadi apa perbedaan antara

# 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()
4 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()
5?

Perbedaan kedua fungsi tersebut adalah. dengan

# 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 properti apa pun yang Anda terapkan (warna, bentuk, ukuran titik) akan diterapkan di semua titik sedangkan di
# 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()
3 Anda memiliki kontrol lebih besar dalam setiap tampilan titik

Yaitu, di

# 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()
4 Anda dapat mengubah warna, bentuk, dan ukuran setiap titik (titik data) berdasarkan variabel lain. Atau bahkan variabel yang sama (y). Sedangkan, dengan
# 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, properti yang Anda atur akan diterapkan ke semua titik di bagan

Pertama, saya akan mengimpor perpustakaan yang akan saya gunakan

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})
_

Fungsi

# 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 digunakan untuk mengubah parameter default dari gambar plot

Plot Scatter dasar dengan python

Pertama, mari buat data buatan menggunakan ________ 9 _______1. Anda perlu menentukan no. poin yang Anda butuhkan sebagai argumen

Anda juga dapat menentukan batas bawah dan atas dari variabel acak yang Anda butuhkan

Kemudian gunakan fungsi

# 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()
_4 untuk menggambar plot pencar menggunakan matplotlib. Anda perlu menentukan variabel x dan y sebagai argumen

# 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 digunakan untuk mengatur judul plot Anda

# 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 digunakan untuk menandai sumbu x

# 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 digunakan untuk menandai sumbu y

Program Ilmuwan Data Industri MLPlus

Apakah Anda ingin belajar Ilmu Data dari Ilmuwan Data berpengalaman?

Bangun karir ilmu data Anda dengan kualifikasi yang diakui secara global dan diakui industri. Selesaikan proyek dengan data perusahaan nyata dan jadilah Ilmuwan Data bersertifikat dalam waktu kurang dari 12 bulan.

Sebar plot di python matplotlib

Dapatkan Kursus Python Lengkap Gratis

Bangun karir ilmu data Anda dengan kualifikasi yang diakui secara global dan diakui industri. Dapatkan pola pikir, kepercayaan diri, dan keterampilan yang membuat Data Scientist begitu berharga

# 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()

Sebar plot di python matplotlib

Anda dapat melihat bahwa ada hubungan linier positif antara titik-titik tersebut. Artinya, saat X bertambah, Y juga bertambah, karena Y sebenarnya hanyalah X + bilangan_acak

Jika Anda ingin warna titik bervariasi tergantung pada nilai Y (atau variabel lain dengan ukuran yang sama), tentukan warna yang harus diambil setiap titik menggunakan argumen

# 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

Anda juga dapat memberikan variabel berbeda dengan ukuran yang sama dengan 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()

Sebar plot di python matplotlib

Mari kita membuat dataset dengan relasi yang meningkat secara eksponensial dan memvisualisasikan plotnya

# 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()
_

Sebar plot di python matplotlib

# 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()
7 digunakan untuk membuat dataset antara batas bawah dan batas atas dengan langkah 'interval' no. poin

Sekarang Anda dapat melihat bahwa ada hubungan eksponensial antara sumbu x dan y

Korelasi dengan plot Scatter

1) Jika nilai y bertambah dengan nilai x, maka dapat dikatakan bahwa variabel tersebut memiliki korelasi positif

2) Jika nilai y berkurang dengan nilai x, maka dapat dikatakan bahwa variabel-variabel tersebut berkorelasi negatif

3) Jika nilai y berubah secara acak bebas dari x, maka dikatakan berkorelasi nol

# 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()

Sebar plot di python matplotlib

Pada grafik di atas, Anda dapat melihat bahwa garis biru menunjukkan korelasi positif, garis oranye menunjukkan korelasi negatif dan titik hijau tidak menunjukkan hubungan dengan nilai x (berubah secara acak secara independen)

Mengubah warna kelompok titik

Gunakan perintah

# 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()
_8 untuk mengubah warna untuk mewakili plot pencar

# 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()

Sebar plot di python matplotlib

Mengubah Warna dan Penanda

Gunakan perintah

# 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 untuk mengubah jenis marker di scatter plot

['. ','o','v','^','>','<','s','p','*','h','H','D','d', .
# Scatterplot of different distributions. Color and Shape of Points.
x = np.random.randn(500)
y1 = np.random.randn(500)
y2 = np.random.chisquare(10, 500)
y3 = np.random.poisson(5, 500)

# Plot
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.scatter(x,y1,color='blue', marker= '*', label='Standard Normal')
plt.scatter(x,y2,color= 'red', marker='v', label='Chi-Square')
plt.scatter(x,y3,color= 'green', marker='.', label='Poisson')


# Decorate
plt.title('Distributions: Color and Shape change')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.legend(loc='best')
plt.show()

Sebar plot di python matplotlib

Scatter Plot dengan plot Linear fit menggunakan Seaborn

Mari kita coba untuk menyesuaikan dataset dengan garis pemasangan terbaik menggunakan fungsi

# 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 di seaborn

Mari kita gunakan dataset mtcars

Anda dapat mengunduh dataset dari alamat yang diberikan. https. // www. kaggle. com/ruiromanini/mtcars/download

Sekarang mari kita coba apakah ada kecocokan linier antara kolom

# 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 dan
# 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

# 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");

Sebar plot di python matplotlib

Anda dapat melihat bahwa kita mendapatkan korelasi negatif antara 2 kolom

# Scatter Plot with lowess line fit
url = 'https://gist.githubusercontent.com/seankross/a412dfbd88b3db70b74b/raw/5f23f993cd87c283ce766e7ac6b329ee7cc2e1d1/mtcars.csv'
df=pd.read_csv(url)
sns.lmplot(x='mpg', y='disp', data=df, lowess=True) 
plt.title("Scatter Plot with Lowess fit");

Sebar plot di python matplotlib

Scatter Plot dengan Histogram menggunakan seaborn

Gunakan fungsi petak bersama di seaborn untuk mewakili petak pencar bersama dengan distribusi nilai x dan y sebagai histogram

Gunakan fungsi

# 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 dengan x, y dan datset sebagai argumen

import seaborn as sns
x = np.random.randn(100)
y1 = np.random.randn(100)
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
sns.jointplot(x=x,y=y1);

Sebar plot di python matplotlib

Seperti yang Anda lihat, kami juga mendapatkan plot distribusi untuk nilai x dan y

Plot gelembung

Plot gelembung adalah sebar di mana dimensi ketiga ditambahkan. nilai variabel tambahan direpresentasikan melalui ukuran titik-titik

Anda perlu menambahkan perintah lain di scatter plot

# 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 yang mewakili ukuran poin

# 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

Sebar plot di python matplotlib

Ukuran gelembung mewakili nilai dimensi ketiga, jika ukuran gelembung lebih dari itu berarti nilai z besar pada titik tersebut.

Analisis Eksplorasi Dataset mtcars

mtcars dataset berisi jarak tempuh dan spesifikasi kendaraan dari beberapa model mobil. Dataset dapat diunduh di sini

Tujuan dari analisis eksplorasi adalah untuk memahami hubungan antara berbagai spesifikasi kendaraan dan jarak tempuh

# 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

Sebar plot di python matplotlib

Anda dapat melihat bahwa dataset berisi berbagai informasi tentang sebuah mobil

Pertama mari kita lihat sebar plot untuk melihat distribusi antara

# 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 dan
# 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 dan distribusi histogramnya. Anda dapat melakukannya dengan menggunakan fungsi
# 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 di seaborn

# 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

Sebar plot di python matplotlib

Multiple Line yang paling cocok

Jika Anda perlu melakukan penyesuaian regresi linier untuk beberapa kategori fitur antara x dan y, seperti dalam kasus ini, saya membagi lebih lanjut kategori menurut

# 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 dan mencoba menyesuaikan garis linier sesuai dengan itu. Untuk ini, gunakan argumen
# 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()
_9 dalam fungsi
# 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

# 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

Sebar plot di python matplotlib

Lihat bahwa fungsinya telah memasang 3 jalur berbeda untuk 3 kategori roda gigi dalam kumpulan data

Menyesuaikan warna dan gaya untuk berbagai kategori

Saya membagi dataset menurut kategori peralatan yang berbeda. Kemudian saya memplotnya secara terpisah menggunakan fungsi

# 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()
1

# 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
# 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

Sebar plot di python matplotlib

Anotasi Teks di Scatter Plot

Jika Anda perlu menambahkan teks apa pun dalam grafik Anda, gunakan fungsi

# 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 dengan teks dan koordinat tempat Anda perlu menambahkan teks sebagai argumen

# 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
# 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

Sebar plot di python matplotlib

Bubble Plot dengan Variabel Kategorikal

Biasanya Anda akan menggunakan 2 variabel untuk memplot grafik pencar (x dan y), lalu saya menambahkan variabel kategori lain

# 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 yang akan tersirat oleh warna titik, saya juga menambahkan variabel lain
# 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 yang nilainya akan tersirat sesuai dengan intensitas

# 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
# 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

Sebar plot di python matplotlib

Saya telah memplot nilai

# 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 vs
# 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 dan juga membaginya menjadi warna berbeda sehubungan dengan nilai
# 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()
7 dan ukuran setiap gelembung mewakili nilai
# 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()
8

Parameter

# 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()
_9 digunakan untuk mengubah intensitas warna plot. Lebih banyak alfa akan lebih banyak intensitas warna

Plot Kategoris

# 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

Sebar plot di python matplotlib

# Scatterplot of different distributions. Color and Shape of Points.
x = np.random.randn(500)
y1 = np.random.randn(500)
y2 = np.random.chisquare(10, 500)
y3 = np.random.poisson(5, 500)

# Plot
plt.rcParams.update({'figure.figsize':(10,8), 'figure.dpi':100})
plt.scatter(x,y1,color='blue', marker= '*', label='Standard Normal')
plt.scatter(x,y2,color= 'red', marker='v', label='Chi-Square')
plt.scatter(x,y3,color= 'green', marker='.', label='Poisson')


# Decorate
plt.title('Distributions: Color and Shape change')
plt.xlabel('X - value')
plt.ylabel('Y - value')
plt.legend(loc='best')
plt.show()
0 digunakan untuk memberikan akses ke beberapa fungsi tingkat sumbu yang menunjukkan hubungan antara numerik dan satu atau lebih variabel kategori menggunakan salah satu dari beberapa representasi visual

Apa itu plot pencar Matplotlib?

Plot sebar adalah diagram dengan setiap nilai dalam kumpulan data diwakili oleh titik . Modul Matplotlib memiliki metode untuk menggambar plot pencar, diperlukan dua larik dengan panjang yang sama, satu untuk nilai sumbu x, dan satu lagi untuk nilai sumbu y. x = [5,7,8,7,2,17,2,9,4,11,12,9,6]

Bagaimana cara membuat plot pencar dengan Python?

Terakhir, Anda membuat plot pencar dengan menggunakan plt. scatter() dengan dua variabel yang ingin Anda bandingkan sebagai argumen masukan . Saat Anda menggunakan skrip Python, Anda juga perlu menampilkan gambar secara eksplisit dengan menggunakan plt. menunjukkan().

Apa perbedaan antara plot () dan pencar () python?

Perbedaan kedua fungsi tersebut adalah. dengan pyplot. plot() properti apa pun yang Anda terapkan (warna, bentuk, ukuran titik) akan diterapkan di semua titik sedangkan di pyplot. scatter() Anda memiliki kontrol lebih dalam tampilan setiap titik .

Apa fungsi scatterplot di Python?

Matplot memiliki fungsi bawaan untuk membuat sebar yang disebut scatter() . Plot pencar adalah jenis plot yang menampilkan data sebagai kumpulan titik. Posisi suatu titik bergantung pada nilai dua dimensinya, dimana setiap nilai merupakan posisi baik pada dimensi horizontal maupun vertikal.