Bird
0
0

Which code snippet correctly achieves this?

hard📝 Application Q15 of 15
Matplotlib - Seaborn Integration
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:
  1. Step 1: Set figure size before plotting

    Use plt.figure(figsize=(12,6)) to set the plot size before creating the plot.
  2. 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.
  3. 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.
  4. 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
  5. 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)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes