-1

I have data where I have names, proportions and total. I want to show all 3 variables in one plot. Ideally I want to have everything like plot 1 but inside I want to show total as in plot 2

In first plot I don't get line right also this is not my plot of choice.

import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns

df = pd.DataFrame({"name": list("ABCDEFGHIJ"), "proportion": [0.747223, 0.785883, 0.735542, 0.817368, 0.565193, 0.723029, 0.723004, 0.722595, 0.783929, 0.55152], 
                   "total": [694327, 309681, 239384, 201646, 192267, 189399, 181974, 163483, 157902, 153610]})

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
sns.barplot(data=df, x="name", y="total", color="lightblue", ax=ax1)
sns.lineplot(data=df, x="name", y= "proportion", color="black", lw=3, ls="--", ax=ax2)

enter image description here

 # Plot the figure.
    df["male"] = df.proportion * df.total
    ax = sns.barplot(data = df, x= "name", y = 'total', color = "lightblue")
    sns.barplot(data = df, x="name", y = "male", color = "blue", ax = ax)
    ax.set_ylabel("male/no_of_streams")

enter image description here

Is there a way I can achieve my goal of effective plot where

  • I can show total split
  • I also want to add proportions values to plot as well

Any help would be appreciated

Thanks in advance

James Taylor
  • 484
  • 1
  • 8
  • 23
  • 1
    "I don't get line right" --> why, what's wrong with the line in plot1?; "I also want to add proportions values" --> if a combined bar/line chart is not the plot of your choice, you could annotate the dark blue bars in plot2 with the proportion values – Stef Jan 14 '21 at 10:22

1 Answers1

1

If my understanding is right, for the first plot, I guess you wanna to know why the line is dashed. Just remove argument ls="--", you will get solid line.

The second, following code can work, if you want percentage of "man-number" / "total". If the percentage is computed using other numbers, you can adjust the equation in the for statement:

import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns

if __name__ == '__main__':
    df = pd.DataFrame({"name": list("ABCDEFGHIJ"), "proportion": [0.747223, 0.785883, 0.735542, 0.817368, 0.565193, 0.723029, 0.723004, 0.722595, 0.783929, 0.55152], "total": [694327, 309681, 239384, 201646, 192267, 189399, 181974, 163483, 157902, 153610]})

    # fig, ax1 = plt.subplots()
    # ax2 = ax1.twinx()
    # sns.barplot(data=df, x="name", y="total", color="lightblue", ax=ax1)
    # # remove ls='--' 
    # sns.lineplot(data=df, x="name", y="proportion", color="black", lw=3, ax=ax2)

    # Plot the figure.
    df["male"] = df.proportion * df.total
    ax = sns.barplot(data = df, x= "name", y = 'total', color = "lightblue")
    sns.barplot(data = df, x="name", y = "male", color = "blue", ax = ax)
    ax.set_ylabel("proportion(male/no_of_streams)")

    # this is code block to add percentage
    for i, v in enumerate(df['proportion']):
        p = ax.patches[i]
        height = p.get_height()
        ax.text(p.get_x()+p.get_width()/2.,
                height + 3,
                '{:1.0f}%'.format(v * 100),
                ha="center")
    plt.show()

enter image description here

enter image description here BTW, I learn at this page, FYI.

Yang Liu
  • 346
  • 1
  • 6
  • g can't i have right legend 'proportion` instead. right, now I have same legends on both sides of the plot. cheers – James Taylor Jan 14 '21 at 15:06
  • Comment out line 8-12, and add"proportion" into line 18, right legend will disappear. – Yang Liu Jan 14 '21 at 15:15
  • no I mean I want to have proportion on right side ?? – James Taylor Jan 14 '21 at 15:17
  • sorry bro. I don't know. It sounds another story. Maybe you can tweak those two barplots like the first plot you did, to get two legends. Maybe other guy will jump into to give a hand. – Yang Liu Jan 14 '21 at 15:28
  • You can add different `ax` for different `barplots`, like the first plot. But as their scale is different, so the area in figure could become misleading then, while you can get a right legend. Sounds tricky. – Yang Liu Jan 14 '21 at 15:39