Question

I am starting with matplotlib and would like to generate a bubble chart. I started with the the scatter demo but I cannot understand how to prepare my data. My aim is to get this (the surfaces are in proportions 1:2:3:4)

enter image description here

and my code was

import matplotlib.pyplot as plt

x = [1, 2]
y = [1, 2]
x_label = ["a", "b"]
y_label = ["x", "y"]
size = [100, 200, 300, 400] # I tried wild combinations here
plt.xticks(x, x_label)
plt.yticks(y, y_label)
plt.scatter(x, y, s=size, alpha=0.5)
plt.show()

This generates the following plot (correct axis, two bubbles of relative size 1:2)

enter image description here

I do not know how to format the data input, size should be made up of len(x)*len(y) elements but how to arrange them?

Was it helpful?

Solution

Your plot command was wrong; you need to specify x and y for each point you want to plot , so 4 values for both x and y.

See the docs for more info (specifically about the shape of x and y)

import matplotlib.pyplot as plt

x = [1, 2,1,2]
y = [1, 1,2,2]
x_label = ["a", "b"]
y_label = ["x", "y"]
size = [100, 200, 300, 400] # I tried wild combinations here
plt.xticks([1,2], x_label)
plt.yticks([1,2], y_label)
plt.scatter(x, y, s=size, alpha=0.5)
plt.show()

creates

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top