xxxxxxxxxx
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
# Plotting with 2 variables
ax.scatter(df1["col1"], df1["col2"], color="red", label="df1")
ax.scatter(df2["col1"], df2["col2"], color="blue", label="df2")
ax.set_xlabel("X axis label")
ax.set_ylabel("Y axis label")
ax.legend()
plt.show()
# Plotting with 3 variables
ax.scatter(df["col1"], df["col2"], c=df.col3) # c represents the third variable
ax.set_xlabel("X axis label")
ax.set_ylabel("Y axis label")
plt.show()
xxxxxxxxxx
import matplotlib.pyplot as plt
x_axis = ['value_1', 'value_2', 'value_3', ]
y_axis = ['value_1', 'value_2', 'value_3', ]
plt.scatter(x_axis, y_axis)
plt.title('title name')
plt.xlabel('x_axis name')
plt.ylabel('y_axis name')
plt.show()
xxxxxxxxxx
import numpy as npimport matplotlib.pyplot as plt
# Create data
N = 500x = np.random.rand(N)
y = np.random.rand(N)
colors = (0,0,0)
area = np.pi*3
# Plot
plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.title('Scatter plot pythonspot.com')
plt.xlabel('x')
plt.ylabel('y')
plt.show()
xxxxxxxxxx
df.plot(x="col1", y="col2", kind="scatter", title = "Some Title")
plt.show()
xxxxxxxxxx
import matplotlib.pyplot as plt
height = [66, 64, 30, 67, 32, 3.9, 3.1, 8.9, 7.7]
diameter = [10.2, 11, 6.9, 12, 2.8, 3.9, 3.1, 8.9, 7.7]
plt.scatter(height, diameter)
plt.show()
xxxxxxxxxx
import numpy as np
import matplotlib.pyplot as plt
# Fixing random state for reproducibility
np.random.seed(19680801)
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = (30 * np.random.rand(N))**2 # 0 to 15 point radii
plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.show()
xxxxxxxxxx
import matplotlib.pyplot as plt
# Example data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Creating a scatter plot
plt.scatter(x, y)
# Adding labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Scatter Plot')
# Displaying the plot
plt.show()
Scatter plots are the graphs that present the relationship between two variables in a data-set. It represents data points on a two-dimensional plane or on a Cartesian system. The independent variable or attribute is plotted on the X-axis, while the dependent variable is plotted on the Y-axis.
xxxxxxxxxx
# Convert cyl column from a numeric to a factor variable
mtcars$cyl <- as.factor(mtcars$cyl)
head(mtcars)