Skip to content

Instantly share code, notes, and snippets.

@singularitti
Created March 16, 2023 08:34
Show Gist options
  • Save singularitti/48994adad53431a8ec5adc4b78e431ee to your computer and use it in GitHub Desktop.
Save singularitti/48994adad53431a8ec5adc4b78e431ee to your computer and use it in GitHub Desktop.
Bubble Plot Generator using ChatGPT #GPT #Python #plotting
import matplotlib.pyplot as plt
import numpy as np
import random
# Generate random colors
def random_color():
return [random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1)]
# Generate coordinates for small bubbles around a big bubble with a fixed distance
def small_bubbles(x, y, radius, distance, num_bubbles, min_radius, max_radius):
angle = np.linspace(0, 2 * np.pi, num_bubbles) + np.random.rand(num_bubbles) / 2
small_bubbles = []
for a in angle[0:-1]:
r = random.uniform(min_radius, max_radius)
x_offset = (radius + distance) * np.cos(a)
y_offset = (radius + distance) * np.sin(a)
small_bubbles.append((x + x_offset, y + y_offset, r))
return small_bubbles
# Set random seed for reproducibility
random.seed(42)
# Create a new figure
fig, ax = plt.subplots()
# Generate big bubbles
num_big_bubbles = 6
big_bubbles = []
big_bubble_radius = 0.05
for i in range(num_big_bubbles):
x = 0.5 + 0.3 * np.cos(2 * np.pi * i / num_big_bubbles)
y = 0.5 + 0.3 * np.sin(2 * np.pi * i / num_big_bubbles)
color = random_color()
big_bubbles.append((x, y, big_bubble_radius, color))
# Generate and plot small bubbles around the big bubbles
for big_bubble in big_bubbles:
x, y, radius, color = big_bubble
num_small_bubbles = random.randint(5, 8)
distance = 1.1 * radius
min_radius = 0.01
max_radius = 0.03
small_bubbles_coords = small_bubbles(x, y, radius, distance, num_small_bubbles, min_radius, max_radius)
for small_bubble in small_bubbles_coords:
x_small, y_small, radius_small = small_bubble
circle = plt.Circle((x_small, y_small), radius_small, color=color, alpha=0.5)
ax.add_artist(circle)
# Add an arrow pointing from the small bubble to the big bubble
arrow = plt.Arrow(x, y, x_small - x, y_small - y, color=color, alpha=0.5, width=0.02)
ax.add_artist(arrow)
# Plot big bubble
circle = plt.Circle((x, y), radius, color=color, alpha=1)
ax.add_artist(circle)
# Set axis limits and aspect ratio
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_aspect('equal', adjustable='box')
# Remove frames, ticks, and labels
plt.axis('off')
# Show the plot
plt.show()
@singularitti
Copy link
Author

singularitti commented Mar 16, 2023

My instruction was:

Use Python to plot many bubbles, where there are several big bubbles (with different colors) scattered in the figure, and around each big bubble, there are several small bubbles with similar colors. I want there to be no overlaps between the big bubbles and the small bubbles. Of course, the same condition applies to each of the big bubbles. And don't put the big bubbles too far away from each other.

Put each of the small bubbles around the big bubble with a fixed distance to the big bubble's center, where the distance is larger than the radius of the big bubble. Let all big bubbles have the same radius for simplicity. Make the big bubbles take up most of the space of the figure. Do not plot frames and ticks, and labels.

Now add arrows pointing from each big bubble to the centers of small bubbles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment