1

I wrote a code that read an excel sheet and plots a scatter figure with the following code:

fig, ax = plt.subplots(figsize=(13, 8))

scatter = ax.scatter(df.Date, df.TopAcc, c="blue", s=df.Param / 10000, alpha=0.2)
plot = ax.plot(dfmax.Date, dfmax.TopAcc, marker="o", c="red")

handles, labels = scatter.legend_elements(num=5, prop="sizes", alpha=0.2, color="blue")
legend = ax.legend(handles, labels, loc="lower right", title="# Parameters", )

plt.grid()
plt.show()

And I got the following figure enter image description here

I have the following issues: How to prevent the legend balls from overlapping?

nechi
  • 92
  • 10
  • 2
    Can you please focus on the second question? The first is a duplicate of [this](https://stackoverflow.com/a/54870776/8881141), and you should not ask several questions rolled into one. – Mr. T Jan 25 '21 at 16:19

1 Answers1

3

You can set columnspacing in the legend object:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

fig, ax = plt.subplots(figsize=(13, 8))
df = pd.DataFrame(np.random.rand(20, 2), columns=['x', 'y'])
df['s'] = 5000 * np.random.rand(20)
scatter = ax.scatter(df.x, df.y, c="blue", s=df.s, alpha=0.2)
handles, labels = scatter.legend_elements(num=5, prop="sizes", alpha=0.2, color="blue")
legend = ax.legend(handles, labels, loc="lower right", title="# Parameters", ncol=6, columnspacing=3, bbox_to_anchor=(1, -0.12), frameon=False)
plt.grid()
plt.show()

enter image description here

jjsantoso
  • 1,586
  • 1
  • 12
  • 17