1

I would like to plot a graph but pandas keeps reordering my index (N).

I want the order to be N= 50, 100, 200 where there are three columns for each N namely 2x2 3x3 4x4

f1 = pd.DataFrame({"User": ["2 x 2 x 2","3 x 3 x 3", "4 x 4 x 4","2 x 2 x 2","3 x 3 x 3", "4 x 4 x 4","2 x 2 x 2","3 x 3 x 3", "4 x 4 x 4"],\
                   "clm2": profit_comparison[0:len(profit_comparison)],\
                   "N": ["N=50","N=50","N=50","N=100","N=100","N=100","N=200","N=200","N=200"]})

with PdfPages('profit(n,p).pdf') as pdf:
        ax1= df1.pivot(index = "N", columns = "User", values = "clm2").plot.bar(edgecolor = "white")
        ax1.set_ylabel("Profit")
        pdf.savefig()
        plt.close()
Massifox
  • 4,369
  • 11
  • 31
Bryan Lwy
  • 45
  • 1
  • 6
  • You could use [seaborn.barplot](https://seaborn.pydata.org/generated/seaborn.barplot.html) to do the plot. You can better control the order with it – Nakor Jul 14 '19 at 03:55

1 Answers1

0

I'm guessing that the incorrect order you're seeing is 100, 200, 50. If that's the case, what's happening is that Pandas is sorting your index alphabetically.

In that case, you have two options: the first is to sort the index from its numeric information, and you can check this question for seeing how.

The second is to just change your data to insert a zero or blank space before 50, so it falls in line:

profit_comparison = range (9)

f1 = pd.DataFrame (
    {
        "User": [ 
            "2 x 2 x 2","3 x 3 x 3", "4 x 4 x 4",
            "2 x 2 x 2","3 x 3 x 3", "4 x 4 x 4",
            "2 x 2 x 2","3 x 3 x 3", "4 x 4 x 4"
        ],
        "clm2": profit_comparison[:],
        "N": [
            "N= 50","N= 50","N= 50",
            "N=100","N=100","N=100",
            "N=200","N=200","N=200"
        ]
    },
)


ax1 = f1.pivot(
    index = "N", 
    columns = "User", 
    values = "clm2"
).plot.bar(edgecolor = "white")

ax1.set_ylabel("Profit")

enter image description here

Some additional notes:

  • When within parenthesis, you don't need the backslash to continue in the following line
  • If you want to copy profit_comparison, you don't need to use [:len(profit_comparison)]; you can use just profit_comparison[:]
  • If the index is still not sorted, you can use sort_index() on the dataframe generated by pivot before plotting
caxcaxcoatl
  • 8,472
  • 1
  • 13
  • 21