Complete the code to create a basic bubble chart using matplotlib.
import matplotlib.pyplot as plt x = [1, 2, 3, 4] y = [10, 20, 25, 30] sizes = [100, 200, 300, 400] plt.scatter(x, y, s=[1]) plt.show()
The s parameter in plt.scatter sets the size of the bubbles. We use the sizes list to control bubble sizes.
Complete the code to add colors to the bubbles in the chart.
import matplotlib.pyplot as plt x = [1, 2, 3, 4] y = [10, 20, 25, 30] sizes = [100, 200, 300, 400] colors = ['red', 'blue', 'green', 'purple'] plt.scatter(x, y, s=sizes, c=[1]) plt.show()
The c parameter in plt.scatter sets the colors of the bubbles. We use the colors list to assign colors.
Fix the error in the code to correctly display the bubble chart with labels.
import matplotlib.pyplot as plt x = [1, 2, 3, 4] y = [10, 20, 25, 30] sizes = [100, 200, 300, 400] labels = ['A', 'B', 'C', 'D'] plt.scatter(x, y, s=sizes) for i, label in enumerate(labels): plt.text(x[i], y[i], [1]) plt.show()
labels[i] instead of label inside the loop.labels which is the whole list, not a single label.Inside the loop, label holds the current label. Use it to place text on the chart.
Fill both blanks to create a bubble chart with transparency and a title.
import matplotlib.pyplot as plt x = [1, 2, 3, 4] y = [10, 20, 25, 30] sizes = [100, 200, 300, 400] plt.scatter(x, y, s=sizes, alpha=[1]) plt.[2]('My Bubble Chart') plt.show()
xlabel instead of title for the plot title.The alpha parameter controls transparency; 0.5 means 50% transparent. The title function adds a title to the plot.
Fill all three blanks to create a bubble chart with edge colors, a grid, and axis labels.
import matplotlib.pyplot as plt x = [1, 2, 3, 4] y = [10, 20, 25, 30] sizes = [100, 200, 300, 400] plt.scatter(x, y, s=sizes, edgecolors=[1]) plt.grid([2]) plt.xlabel([3]) plt.show()
'none' for edgecolors which removes edges.plt.grid which hides the grid.plt.xlabel.edgecolors='black' adds black edges to bubbles. plt.grid(True) shows grid lines. plt.xlabel('X Axis') labels the x-axis.