Question: everyone! I'm using matplotlib and I have a field with randomly generated circles. Also I have button which has to generate new random circles in the field but every time I press it, circles are generated inside the BUTTON, but not in the field. Please show me what I'm doing wrong, I'm new to python(actually started learning it yesterday). Here is my code: 

```
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button

plt.subplots_adjust(bottom=0.2)

N = 10
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = np.pi * 0.2
l = plt.scatter(x, y, s=area, c=colors, alpha=0.8)

def gen(event):
    N = 10
    x = np.random.rand(N)
    y = np.random.rand(N)
    colors = np.random.rand(N)
    area = np.pi * 0.2
    plt.scatter(x, y, s=area, c=colors, alpha=0.8)
    plt.draw()

axgen = plt.axes([0.81, 0.05, 0.1, 0.075])
bgen = Button(axgen, 'Generate')
bgen.on_clicked(gen)

plt.show()

```

![enter image description here](https://i.stack.imgur.com/LUXKT.png)

Answer: 
What you have is efficient enough, really. In this context almost anything you could dream up is efficient enough, but for neatness? Well, I would've at least refactored the conversion into a function, and do you really need a long int to store the age levels? I doubt it as humans generally don't become *that* old. I would just go for a regular int.

Something like this:

```
int ConvertFromMaturityConfig(string maturityConfig)
{
    string ageString = maturityConfig.Split('=')[0];
    return int.Parse(ageString);
}

```

Usage:

```
string maturity = "0=Child|13=Teen|18=Adult";
string[] configuration = maturity.Split('|');

int childAgeMin = ConvertFromMaturityConfig(configuration[0]);
int teenAgeMin = ConvertFromMaturityConfig(configuration[1]);
int adultAgeMin = ConvertFromMaturityConfig(configuration[2]);

```

I would also consider trying to determine which configuration belongs to which value. As it is, you're expecting you get the child age, the teen age and the adult age, in that order. Personally, with the constraints you have, I would've put it into a dictionary so that you could look it up there. Then you could have something like this:

```
string maturity = "0=Child|13=Teen|18=Adult";
var config = maturity.Split('|')
    .Select(s => s.Split('='))
    .ToDictionary(
        c => c[1], // key selector
        c => int.Parse(c[0]));

```

Then you can use it like this:

```
if (age >= config["Child"] && age < config["Teen"])
    maturity = "Child";
else if (age >= config["Teen"] && age < config["Adult"])
    maturity = "Teen";
else if (age >= config["Adult"]) 
    maturity = "Adult";

```

You would be wise to consider what happens if the age is below the minimum child age, by the way.
Create a class like below:

```
public class PersonRecord
{
   public int MinAge { get; set; }
   public string Maturity { get; set; }
}

```

Create a method which will parse the string like below:

```
public static List<PersonRecord> Parse(string records)
{
   var splits = records.Split('|');
   var persons = splits.Select(p =>
   {
      int age;
      var split = p.Split('=');
      if(int.TryParse(split[0], out age))
      {
         return new PersonRecord { MinAge = age, Maturity = split[1] };
      }

      // Age was not a number so so whatever you want here
      // Or you can return a dummy person record
      throw new InvalidOperationException("Records is not valid.");

   }).ToList();

   return persons;
}

```

Use it like this:

```
string records = "0=Child|13=Teen|18=Adult";
var persons = Parse(records);

var p1Maturity = persons[0].Maturity;

```

---

**Performance**

Choose usability and code clarity firstly. Do not worry about performance if it is not an issue yet. Do performance testing and if the above code is the bottleneck then you can optimize it.

---

**Possible Enhancements**

You can add another property to the `PersonRecord` class `MaxAge` if you need/want and more methods and properties depending on your needs.