How to stretch specific y axis intervals so the space between some values is larger than between others?

31 Views Asked by At

I'm trying to stretch my Y axis so that the dense bubbles don't overlap each other and the text is easier to read. Currently,the spacing on Y axis is standard and many bubbles overlap. My example code is:

import matplotlib.pyplot as plt
from matplotlib.transforms import offset_copy

data = {
    'cleanDate': [1000, 1100, 1200, 1230, 1250, 1260, 1600, 1700, 1800, 1900, 2000],
    'higherRange': [30000000, 25000000, 14000000, 14500000, 15000000, 15500000, 3600000, 3000000, 3100000, 3000000, 3500000],
    'War': ['War1', 'War2', 'War3', 'War4', 'War5', 'War6', 'War7', 'War8', 'War9', 'War10', 'War11'],
    'BC': [False] * 11
}

plt.figure(figsize=(10, 8))

# Create a copy of the transform with an offset for annotations
offset_transform = offset_copy(plt.gca().transData, fig=plt.gcf(), x=0, y=10, units='points')

# Scatter plot with bubble sizes based on 'higherRange'
plt.scatter(data['cleanDate'], data['higherRange'], s=[size / 100000 for size in data['higherRange']], alpha=0.7)

# Annotate each point with the respective 'War' label using offset_transform
for x, y, war in zip(data['cleanDate'], data['higherRange'], data['War']):
    plt.annotate(war, (x, y), textcoords=offset_transform, ha='center', fontsize=8, color='black')

# Add labels and title
plt.title('Bubble Plot of Wars Over Time')
plt.xlabel('Year')
plt.ylabel('Lower Range Value')

# Set custom y-axis limits to stretch the y-axis
plt.ylim(min(data['higherRange']) - 1000000, max(data['higherRange']) + 1000000)

# Show the plot
plt.grid(True)
plt.show()

Wars 3-6 are too close to each other on the graph, I'd like to stretch the interval of y axis so they have more space, like in the attached figure, you can see the distance between 0-10M is larger than between 10M and 20M.

A gtraph I'm trying to replicate

0

There are 0 best solutions below