Stacked barchart from pivot table

Hi everyone,

How are you?

This is my first time posting here, so if the post format is not good, I apologize and ask for guidance. I wrote the questions and uploaded the images to show the problem. If it is difficult to see them, please tell me.

I have a very beginner doubt, could anyone help me?

I’m trying to plot a stacked barchart from a pivot table.

I have a wine sales database with daily data.

Then I did a pivot table to aggregate data from daily to yearly, this way:

I want to plot a stacked barchart from this pivot table, so the x-axis is the year, and the y-axis is the amount of sales by country. And I want the colors classified by country. If possible, I want to add my own colors.

The I did this. The y-axis didn’t accept y=‘country’ - the message error was ‘The dataframe contains no ‘country’.’. Then I added y=[‘Argentina’], and it worked somehow, plotting a stacked barchart.

But then, the colors are the pattern, I cannot change them.

  1. I wonder if there is a better way to plot aggregated data. Is the pivot_table a bad choice to be plotted later?

  2. If it is possible to plot from this pivot table, how to do it correctly and in a way I can change other features like color, template and so on?

Thank you!

Hello @rlimani, elcome to the forums.

It really all depends on your data, which chart to use, how to prepare the data (by sorting, grouping…). This makes it difficult to give you a general answer. Since you mentioned, you are just starting your plotly journey, I would recommend start building some basic charts and add complexity step by step. I think this would be a good starting point:

As for the format of your question keep in mind, that

  • posting your code as formatted code instead of images
  • adding (dummy) data to make it possible reproduce your problem

makes the lifes of the people willing to help you a lot easier.

Asking specific questions also increases the chances to get your questions answered.

@adamschroeder wrote a nice post also:

1 Like