You want to create a Seaborn line plot with a custom figure size of 12x6 inches, a title 'Sales Over Time', X-axis label 'Month', Y-axis label 'Sales', and grid lines visible. Which code snippet correctly achieves this?
A<pre>import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(12,6))
sns.lineplot(x=[1,2,3], y=[100,200,300])
plt.title('Sales Over Time')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.grid(True)
plt.show()</pre>
B<pre>import seaborn as sns
import matplotlib.pyplot as plt
sns.lineplot(x=[1,2,3], y=[100,200,300], figsize=(12,6))
plt.title('Sales Over Time')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.grid(True)
plt.show()</pre>
C<pre>import seaborn as sns
import matplotlib.pyplot as plt
sns.set_figsize(12,6)
sns.lineplot(x=[1,2,3], y=[100,200,300])
plt.title('Sales Over Time')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.grid(True)
plt.show()</pre>
D<pre>import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(12,6))
sns.lineplot(x=[1,2,3], y=[100,200,300])
sns.title('Sales Over Time')
sns.xlabel('Month')
sns.ylabel('Sales')
sns.grid(True)
plt.show()</pre>
Step-by-Step Solution
Solution:
Step 1: Set figure size before plotting
Use plt.figure(figsize=(12,6)) to set the plot size before creating the plot.
Step 2: Use Matplotlib functions for title, labels, and grid
Matplotlib functions plt.title(), plt.xlabel(), plt.ylabel(), and plt.grid(True) customize the plot after creation.
Step 3: Verify code correctness
import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(12,6))
sns.lineplot(x=[1,2,3], y=[100,200,300])
plt.title('Sales Over Time')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.grid(True)
plt.show()
correctly uses plt.figure and Matplotlib functions; other options misuse parameters or functions.
Final Answer:
plt.figure(figsize=(12,6)); sns.lineplot(); plt.title('Sales Over Time'); plt.xlabel('Month'); plt.ylabel('Sales'); plt.grid(True) -> Option A
Quick Check:
Set figure size with plt.figure, customize with plt functions [OK]
Quick Trick:Set size with plt.figure, customize with plt.title/labels/grid [OK]
Common Mistakes:
Passing figsize to sns.lineplot (not supported)
Using sns.set_figsize (does not exist)
Calling sns.title or sns.xlabel (wrong library)
Master "Seaborn Integration" in Matplotlib
9 interactive learning modes - each teaches the same concept differently