Consider the following Python code using Seaborn and Matplotlib:
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="darkgrid")
data = sns.load_dataset("tips")
plot = sns.scatterplot(x="total_bill", y="tip", data=data)
plot.set_title("Total Bill vs Tip")
plot.set_xlabel("Bill Amount")
plot.set_ylabel("Tip Amount")
plt.show()What will be the title of the plot?
import seaborn as sns import matplotlib.pyplot as plt sns.set_theme(style="darkgrid") data = sns.load_dataset("tips") plot = sns.scatterplot(x="total_bill", y="tip", data=data) plot.set_title("Total Bill vs Tip") plot.set_xlabel("Bill Amount") plot.set_ylabel("Tip Amount") plt.show()
Look at the set_title method call on the plot object.
The set_title method sets the plot's title to the string provided. Here, it is set to "Total Bill vs Tip".
Given this code snippet:
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme()
data = sns.load_dataset("iris")
plot = sns.boxplot(x="species", y="sepal_length", data=data)
plot.set_xticks([0, 1, 2])
plt.show()How many tick labels will appear on the x-axis?
import seaborn as sns import matplotlib.pyplot as plt sns.set_theme() data = sns.load_dataset("iris") plot = sns.boxplot(x="species", y="sepal_length", data=data) plot.set_xticks([0, 1, 2]) plt.show()
Check the list passed to set_xticks.
The set_xticks method sets the positions of ticks on the x-axis. Here, three positions are set: 0, 1, and 2.
Which code snippet will produce a Seaborn line plot with the line colored red and dashed?
Check the correct parameter name for line style in Seaborn.
The correct parameter to set line style is linestyle. Option B uses linestyle='--' which means dashed line. Other options use incorrect parameter names or styles.
Examine this code:
import seaborn as sns
import matplotlib.pyplot as plt
data = sns.load_dataset("diamonds")
plot = sns.histplot(data["carat"])
plot.set_xlabel("Carat Weight")
plot.set_ylabel("Frequency")
plot.set_title("Carat Distribution")
plot.set_xticks([0.5, 1.0, 1.5, 2.0])
plt.show()What error will this code raise?
import seaborn as sns import matplotlib.pyplot as plt data = sns.load_dataset("diamonds") plot = sns.histplot(data["carat"]) plot.set_xlabel("Carat Weight") plot.set_ylabel("Frequency") plot.set_title("Carat Distribution") plot.set_xticks([0.5, 1.0, 1.5, 2.0]) plt.show()
Check if set_xticks is a valid method for the plot object.
The plot object is a Matplotlib AxesSubplot, which supports set_xticks with float values. So no error occurs.
Given this code:
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="whitegrid")
data = sns.load_dataset("penguins")
plot = sns.barplot(x="species", y="flipper_length_mm", data=data, color="skyblue")
for bar in plot.patches:
bar.set_facecolor("orange")
plt.show()What color will the bars appear in the final plot?
import seaborn as sns import matplotlib.pyplot as plt sns.set_theme(style="whitegrid") data = sns.load_dataset("penguins") plot = sns.barplot(x="species", y="flipper_length_mm", data=data, color="skyblue") for bar in plot.patches: bar.set_facecolor("orange") plt.show()
Consider the effect of the loop changing bar face colors after initial color setting.
The bars are first colored skyblue by the color parameter, but then each bar's face color is changed to orange in the loop, so the final color is orange.