Matplotlib basics Flashcards
“What is a histogram in Matplotlib?”
“A histogram is a graphical representation of data distribution using bins.”
“How do you create a histogram in Matplotlib?”
“Use plt.hist(data, bins=10)
where data
is a numerical dataset.”
“How to change the number of bins in a histogram?”
“Use the bins
parameter in plt.hist()
, e.g., plt.hist(data, bins=20)
.”
“How to normalize a histogram?”
“Set density=True
in plt.hist()
, e.g., plt.hist(data, bins=10, density=True)
.”
“What is a bar chart used for?”
“A bar chart represents categorical data using rectangular bars.”
“How do you create a vertical bar chart in Matplotlib?”
“Use plt.bar(categories, values)
where categories
is a list of labels and values
is their corresponding heights.”
“How to create a horizontal bar chart?”
“Use plt.barh(categories, values)
instead of plt.bar()
.”
“What is a pie chart in Matplotlib?”
“A pie chart represents proportions of a whole using slices.”
“How do you create a basic pie chart?”
“Use plt.pie(sizes, labels=labels)
where sizes
represents proportions.”
“How to add percentage labels to a pie chart?”
“Use autopct="%1.1f%%"
in plt.pie()
, e.g., plt.pie(sizes, labels=labels, autopct="%1.1f%%")
.”
“What is a scatter plot used for?”
“A scatter plot is used to visualize relationships between two numerical variables.”
“How to create a scatter plot in Matplotlib?”
“Use plt.scatter(x, y)
where x
and y
are lists of numerical values.”
“How to change the marker style in a scatter plot?”
“Use the marker
parameter, e.g., plt.scatter(x, y, marker="o")
.”
“How do you create multiple subplots in Matplotlib?”
“Use plt.subplots(rows, cols)
, e.g., fig, ax = plt.subplots(2, 2)
.”
“How to add a title to a Matplotlib plot?”
“Use plt.title("Title Here")
.”
“How to add labels to the x-axis and y-axis?”
“Use plt.xlabel("X-axis Label")
and plt.ylabel("Y-axis Label")
.”
“How to save a Matplotlib figure?”
“Use plt.savefig("filename.png")
to save the plot as an image file.”