0

I have a dataframe that looks like this:

No._trees  prop._robin    prop._dove
1          0.5            0.6
2          0.6            0.2

The mean number of birds whether it is robins or doves will only ever be between 0 and 1. I want to create a graph where the number of trees would be the x-axis variable, the y-axis variable would go between 0 and 1 and there would be two lines, one for the robins and one for the doves. I am struggling because all of the examples I have read have the y-variable as a dataframe column, whereas in my case I just want it to go from 0 to 1.

Reproducible dataframe:

d = {'No._trees': [1, 2], 'prop._robin': [0.5, 0.6], 'prop._dove':[0.6,0.2]}
df = pd.DataFrame(data=d)
Niam45
  • 552
  • 2
  • 16
  • There is no reason to use `set_index`. `df.columns = df.columns.str.replace('prop._', '')` and `ax = df.plot(x='No._trees', ylim=(0, 1), xticks=(1, 2))` – Trenton McKinney Jun 18 '23 at 19:49

1 Answers1

2

One option is to adjust the df header's and plot the resulting DataFrame afterwards :

out = (
    df.set_index("No._trees")
        .rename(lambda x: x.removeprefix("prop._"), axis=1)
)

out.plot(ylim=(0, 1), xticks=out.index, legend=True); # add more kwargs if needed

Output :

enter image description here

Timeless
  • 22,580
  • 4
  • 12
  • 30