熊猫散点图 – DataFrame.plot.scatter()
散点图是一种数据可视化技术,可显示两个数值变量之间的关系。为了使用 Pandas 绘制散点图,有 DataFrame 类,这个类有一个名为 plot 的成员。调用 plot成员上的 scatter() 方法在两个变量或 Pandas DataFrame 的两列之间绘制一个图。
Syntax: DataFrame.plot.scatter(x, y, s = none, c = none)
Parameter:
x: column name to be used as horizontal coordinates for each point
y: column name to be used as vertical coordinates for each point
s: size of dots
c: color of dots
脚步:
- 导入必要的库。
- 准备一个数据
- 将准备好的数据转换为DataFrame
- 绘制散点图
示例 1:在此示例中,我们将使用数据框绘制散点图,在这里我们将创建数据框并使用不同的列绘制散点图。
Python3
# Program to draw scatter plot using Dataframe.plot
# Import libraries
import pandas as pd
# Prepare data
data={'Name':['Dhanashri', 'Smita', 'Rutuja',
'Sunita', 'Poonam', 'Srushti'],
'Age':[20, 18, 27, 50, 12, 15]}
# Load data into DataFrame
df = pd.DataFrame(data = data);
# Draw a scatter plot
df.plot.scatter(x = 'Name', y = 'Age', s = 100);
Python3
# Program to draw scatter plot using Dataframe.plot
# Import libraries
import pandas as pd
# Prepare data
data={'Name':['Dhanashri', 'Smita', 'Rutuja',
'Sunita', 'Poonam', 'Srushti'],
'Age':[ 20, 18, 27, 50, 12, 15]}
# Load data into DataFrame
df = pd.DataFrame(data = data);
# Draw a scatter plot and here size of dots determined by age of person
df.plot.scatter(x = 'Name', y = 'Age', s = 'Age', c = 'red');
输出:
示例 2:在这里,我们将创建数据框并使用带有样式数据点的不同列绘制散点图。
蟒蛇3
# Program to draw scatter plot using Dataframe.plot
# Import libraries
import pandas as pd
# Prepare data
data={'Name':['Dhanashri', 'Smita', 'Rutuja',
'Sunita', 'Poonam', 'Srushti'],
'Age':[ 20, 18, 27, 50, 12, 15]}
# Load data into DataFrame
df = pd.DataFrame(data = data);
# Draw a scatter plot and here size of dots determined by age of person
df.plot.scatter(x = 'Name', y = 'Age', s = 'Age', c = 'red');
输出: