Python pptx custom color for each category





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















I'm looking at example here:
https://python-pptx.readthedocs.org/en/latest/user/charts.html?highlight=color#pie-chart



chart_data = ChartData()
chart_data.categories = ['West', 'East', 'North', 'South', 'Other']
chart_data.add_series('Series 1', (0.135, 0.324, 0.180, 0.235, 0.126))

chart = slide.shapes.add_chart(
XL_CHART_TYPE.PIE, x, y, cx, cy, chart_data
).chart

chart.has_legend = True
chart.legend.position = XL_LEGEND_POSITION.BOTTOM
chart.legend.include_in_layout = False

chart.plots[0].has_data_labels = True
data_labels = chart.plots[0].data_labels
data_labels.number_format = '0%'
data_labels.position = XL_LABEL_POSITION.OUTSIDE_END


But I can't understand how to make each category with custom not automatic color:
west is yellow, east is blue, north is grey, south is red, other as brown for example.










share|improve this question

























  • Have you read this section in the documentation?

    – scanny
    Feb 24 '16 at 2:27


















2















I'm looking at example here:
https://python-pptx.readthedocs.org/en/latest/user/charts.html?highlight=color#pie-chart



chart_data = ChartData()
chart_data.categories = ['West', 'East', 'North', 'South', 'Other']
chart_data.add_series('Series 1', (0.135, 0.324, 0.180, 0.235, 0.126))

chart = slide.shapes.add_chart(
XL_CHART_TYPE.PIE, x, y, cx, cy, chart_data
).chart

chart.has_legend = True
chart.legend.position = XL_LEGEND_POSITION.BOTTOM
chart.legend.include_in_layout = False

chart.plots[0].has_data_labels = True
data_labels = chart.plots[0].data_labels
data_labels.number_format = '0%'
data_labels.position = XL_LABEL_POSITION.OUTSIDE_END


But I can't understand how to make each category with custom not automatic color:
west is yellow, east is blue, north is grey, south is red, other as brown for example.










share|improve this question

























  • Have you read this section in the documentation?

    – scanny
    Feb 24 '16 at 2:27














2












2








2








I'm looking at example here:
https://python-pptx.readthedocs.org/en/latest/user/charts.html?highlight=color#pie-chart



chart_data = ChartData()
chart_data.categories = ['West', 'East', 'North', 'South', 'Other']
chart_data.add_series('Series 1', (0.135, 0.324, 0.180, 0.235, 0.126))

chart = slide.shapes.add_chart(
XL_CHART_TYPE.PIE, x, y, cx, cy, chart_data
).chart

chart.has_legend = True
chart.legend.position = XL_LEGEND_POSITION.BOTTOM
chart.legend.include_in_layout = False

chart.plots[0].has_data_labels = True
data_labels = chart.plots[0].data_labels
data_labels.number_format = '0%'
data_labels.position = XL_LABEL_POSITION.OUTSIDE_END


But I can't understand how to make each category with custom not automatic color:
west is yellow, east is blue, north is grey, south is red, other as brown for example.










share|improve this question
















I'm looking at example here:
https://python-pptx.readthedocs.org/en/latest/user/charts.html?highlight=color#pie-chart



chart_data = ChartData()
chart_data.categories = ['West', 'East', 'North', 'South', 'Other']
chart_data.add_series('Series 1', (0.135, 0.324, 0.180, 0.235, 0.126))

chart = slide.shapes.add_chart(
XL_CHART_TYPE.PIE, x, y, cx, cy, chart_data
).chart

chart.has_legend = True
chart.legend.position = XL_LEGEND_POSITION.BOTTOM
chart.legend.include_in_layout = False

chart.plots[0].has_data_labels = True
data_labels = chart.plots[0].data_labels
data_labels.number_format = '0%'
data_labels.position = XL_LABEL_POSITION.OUTSIDE_END


But I can't understand how to make each category with custom not automatic color:
west is yellow, east is blue, north is grey, south is red, other as brown for example.







python python-pptx






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 14 '15 at 10:49







sevatster

















asked Dec 14 '15 at 10:29









sevatstersevatster

2619




2619













  • Have you read this section in the documentation?

    – scanny
    Feb 24 '16 at 2:27



















  • Have you read this section in the documentation?

    – scanny
    Feb 24 '16 at 2:27

















Have you read this section in the documentation?

– scanny
Feb 24 '16 at 2:27





Have you read this section in the documentation?

– scanny
Feb 24 '16 at 2:27












3 Answers
3






active

oldest

votes


















0














Custom coloring on a series is accomplished using the .fill attribute on the series.



Unfortunately that attribute hasn't been implemented yet for pie charts, only for bar and column charts.
http://python-pptx.readthedocs.org/en/latest/api/chart.html#barseries-objects



It is possible to change the default coloring though, in the "template" .pptx file you start with, which accomplishes the same thing for many folks. All the charts in the file will have the same coloring, but it doesn't have to be the built-in defaults.






share|improve this answer
























  • Thanks. I have seen this issue for pie chart.

    – sevatster
    Feb 25 '16 at 7:42











  • Can you show how to use .fill attribute?

    – idazuwaika
    Aug 5 '16 at 17:05











  • Some examples at python-pptx.readthedocs.io/en/latest/user/autoshapes.html#fill. API documentation at: python-pptx.readthedocs.io/en/latest/api/…

    – scanny
    Aug 5 '16 at 17:10











  • thanks... funny i didn't find it when googling earlier

    – idazuwaika
    Aug 6 '16 at 9:29



















2














I've responded to this question on Github, it is now possible to modify pie charts colors.



As the pie chart is only one serie of multiple points, you'll need to modify every point individually. This can be done by itering through each points of the first Serie (as it's the only one you have in a pie chart) and changing the color with whatever you like. The point color is in the .format.fill argument, that you can interact with easily using the links scanny provided above.



Here is a simple snippet for your use case:



        # [yellow, blue, grey, red, brown]
color_list = ["ffff00", "0000ff", "D3D3D3", "ff0000", "A52A2A"]
# Go through every point of the first serie and modify the color
for idx, point in enumerate(chart.series[0].points):
col_idx = idx % len(color_list)
point.format.fill.solid()
point.format.fill.fore_color.rgb = RGBColor.from_string(color_list[col_idx])


Cheers !






share|improve this answer































    1














    It is possible to change the color of the line in a line chart because I tried many suggestions without success like this code:



                _green = RGBColor(156, 213, 91)
    plot = chart.plots[0]
    series = plot.series[0]
    line = series.format.line
    line.fill.rgb = _green





    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34264715%2fpython-pptx-custom-color-for-each-category%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Custom coloring on a series is accomplished using the .fill attribute on the series.



      Unfortunately that attribute hasn't been implemented yet for pie charts, only for bar and column charts.
      http://python-pptx.readthedocs.org/en/latest/api/chart.html#barseries-objects



      It is possible to change the default coloring though, in the "template" .pptx file you start with, which accomplishes the same thing for many folks. All the charts in the file will have the same coloring, but it doesn't have to be the built-in defaults.






      share|improve this answer
























      • Thanks. I have seen this issue for pie chart.

        – sevatster
        Feb 25 '16 at 7:42











      • Can you show how to use .fill attribute?

        – idazuwaika
        Aug 5 '16 at 17:05











      • Some examples at python-pptx.readthedocs.io/en/latest/user/autoshapes.html#fill. API documentation at: python-pptx.readthedocs.io/en/latest/api/…

        – scanny
        Aug 5 '16 at 17:10











      • thanks... funny i didn't find it when googling earlier

        – idazuwaika
        Aug 6 '16 at 9:29
















      0














      Custom coloring on a series is accomplished using the .fill attribute on the series.



      Unfortunately that attribute hasn't been implemented yet for pie charts, only for bar and column charts.
      http://python-pptx.readthedocs.org/en/latest/api/chart.html#barseries-objects



      It is possible to change the default coloring though, in the "template" .pptx file you start with, which accomplishes the same thing for many folks. All the charts in the file will have the same coloring, but it doesn't have to be the built-in defaults.






      share|improve this answer
























      • Thanks. I have seen this issue for pie chart.

        – sevatster
        Feb 25 '16 at 7:42











      • Can you show how to use .fill attribute?

        – idazuwaika
        Aug 5 '16 at 17:05











      • Some examples at python-pptx.readthedocs.io/en/latest/user/autoshapes.html#fill. API documentation at: python-pptx.readthedocs.io/en/latest/api/…

        – scanny
        Aug 5 '16 at 17:10











      • thanks... funny i didn't find it when googling earlier

        – idazuwaika
        Aug 6 '16 at 9:29














      0












      0








      0







      Custom coloring on a series is accomplished using the .fill attribute on the series.



      Unfortunately that attribute hasn't been implemented yet for pie charts, only for bar and column charts.
      http://python-pptx.readthedocs.org/en/latest/api/chart.html#barseries-objects



      It is possible to change the default coloring though, in the "template" .pptx file you start with, which accomplishes the same thing for many folks. All the charts in the file will have the same coloring, but it doesn't have to be the built-in defaults.






      share|improve this answer













      Custom coloring on a series is accomplished using the .fill attribute on the series.



      Unfortunately that attribute hasn't been implemented yet for pie charts, only for bar and column charts.
      http://python-pptx.readthedocs.org/en/latest/api/chart.html#barseries-objects



      It is possible to change the default coloring though, in the "template" .pptx file you start with, which accomplishes the same thing for many folks. All the charts in the file will have the same coloring, but it doesn't have to be the built-in defaults.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Feb 24 '16 at 2:34









      scannyscanny

      10.6k12245




      10.6k12245













      • Thanks. I have seen this issue for pie chart.

        – sevatster
        Feb 25 '16 at 7:42











      • Can you show how to use .fill attribute?

        – idazuwaika
        Aug 5 '16 at 17:05











      • Some examples at python-pptx.readthedocs.io/en/latest/user/autoshapes.html#fill. API documentation at: python-pptx.readthedocs.io/en/latest/api/…

        – scanny
        Aug 5 '16 at 17:10











      • thanks... funny i didn't find it when googling earlier

        – idazuwaika
        Aug 6 '16 at 9:29



















      • Thanks. I have seen this issue for pie chart.

        – sevatster
        Feb 25 '16 at 7:42











      • Can you show how to use .fill attribute?

        – idazuwaika
        Aug 5 '16 at 17:05











      • Some examples at python-pptx.readthedocs.io/en/latest/user/autoshapes.html#fill. API documentation at: python-pptx.readthedocs.io/en/latest/api/…

        – scanny
        Aug 5 '16 at 17:10











      • thanks... funny i didn't find it when googling earlier

        – idazuwaika
        Aug 6 '16 at 9:29

















      Thanks. I have seen this issue for pie chart.

      – sevatster
      Feb 25 '16 at 7:42





      Thanks. I have seen this issue for pie chart.

      – sevatster
      Feb 25 '16 at 7:42













      Can you show how to use .fill attribute?

      – idazuwaika
      Aug 5 '16 at 17:05





      Can you show how to use .fill attribute?

      – idazuwaika
      Aug 5 '16 at 17:05













      Some examples at python-pptx.readthedocs.io/en/latest/user/autoshapes.html#fill. API documentation at: python-pptx.readthedocs.io/en/latest/api/…

      – scanny
      Aug 5 '16 at 17:10





      Some examples at python-pptx.readthedocs.io/en/latest/user/autoshapes.html#fill. API documentation at: python-pptx.readthedocs.io/en/latest/api/…

      – scanny
      Aug 5 '16 at 17:10













      thanks... funny i didn't find it when googling earlier

      – idazuwaika
      Aug 6 '16 at 9:29





      thanks... funny i didn't find it when googling earlier

      – idazuwaika
      Aug 6 '16 at 9:29













      2














      I've responded to this question on Github, it is now possible to modify pie charts colors.



      As the pie chart is only one serie of multiple points, you'll need to modify every point individually. This can be done by itering through each points of the first Serie (as it's the only one you have in a pie chart) and changing the color with whatever you like. The point color is in the .format.fill argument, that you can interact with easily using the links scanny provided above.



      Here is a simple snippet for your use case:



              # [yellow, blue, grey, red, brown]
      color_list = ["ffff00", "0000ff", "D3D3D3", "ff0000", "A52A2A"]
      # Go through every point of the first serie and modify the color
      for idx, point in enumerate(chart.series[0].points):
      col_idx = idx % len(color_list)
      point.format.fill.solid()
      point.format.fill.fore_color.rgb = RGBColor.from_string(color_list[col_idx])


      Cheers !






      share|improve this answer




























        2














        I've responded to this question on Github, it is now possible to modify pie charts colors.



        As the pie chart is only one serie of multiple points, you'll need to modify every point individually. This can be done by itering through each points of the first Serie (as it's the only one you have in a pie chart) and changing the color with whatever you like. The point color is in the .format.fill argument, that you can interact with easily using the links scanny provided above.



        Here is a simple snippet for your use case:



                # [yellow, blue, grey, red, brown]
        color_list = ["ffff00", "0000ff", "D3D3D3", "ff0000", "A52A2A"]
        # Go through every point of the first serie and modify the color
        for idx, point in enumerate(chart.series[0].points):
        col_idx = idx % len(color_list)
        point.format.fill.solid()
        point.format.fill.fore_color.rgb = RGBColor.from_string(color_list[col_idx])


        Cheers !






        share|improve this answer


























          2












          2








          2







          I've responded to this question on Github, it is now possible to modify pie charts colors.



          As the pie chart is only one serie of multiple points, you'll need to modify every point individually. This can be done by itering through each points of the first Serie (as it's the only one you have in a pie chart) and changing the color with whatever you like. The point color is in the .format.fill argument, that you can interact with easily using the links scanny provided above.



          Here is a simple snippet for your use case:



                  # [yellow, blue, grey, red, brown]
          color_list = ["ffff00", "0000ff", "D3D3D3", "ff0000", "A52A2A"]
          # Go through every point of the first serie and modify the color
          for idx, point in enumerate(chart.series[0].points):
          col_idx = idx % len(color_list)
          point.format.fill.solid()
          point.format.fill.fore_color.rgb = RGBColor.from_string(color_list[col_idx])


          Cheers !






          share|improve this answer













          I've responded to this question on Github, it is now possible to modify pie charts colors.



          As the pie chart is only one serie of multiple points, you'll need to modify every point individually. This can be done by itering through each points of the first Serie (as it's the only one you have in a pie chart) and changing the color with whatever you like. The point color is in the .format.fill argument, that you can interact with easily using the links scanny provided above.



          Here is a simple snippet for your use case:



                  # [yellow, blue, grey, red, brown]
          color_list = ["ffff00", "0000ff", "D3D3D3", "ff0000", "A52A2A"]
          # Go through every point of the first serie and modify the color
          for idx, point in enumerate(chart.series[0].points):
          col_idx = idx % len(color_list)
          point.format.fill.solid()
          point.format.fill.fore_color.rgb = RGBColor.from_string(color_list[col_idx])


          Cheers !







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 1 at 15:29









          Milo ParigiMilo Parigi

          214




          214























              1














              It is possible to change the color of the line in a line chart because I tried many suggestions without success like this code:



                          _green = RGBColor(156, 213, 91)
              plot = chart.plots[0]
              series = plot.series[0]
              line = series.format.line
              line.fill.rgb = _green





              share|improve this answer




























                1














                It is possible to change the color of the line in a line chart because I tried many suggestions without success like this code:



                            _green = RGBColor(156, 213, 91)
                plot = chart.plots[0]
                series = plot.series[0]
                line = series.format.line
                line.fill.rgb = _green





                share|improve this answer


























                  1












                  1








                  1







                  It is possible to change the color of the line in a line chart because I tried many suggestions without success like this code:



                              _green = RGBColor(156, 213, 91)
                  plot = chart.plots[0]
                  series = plot.series[0]
                  line = series.format.line
                  line.fill.rgb = _green





                  share|improve this answer













                  It is possible to change the color of the line in a line chart because I tried many suggestions without success like this code:



                              _green = RGBColor(156, 213, 91)
                  plot = chart.plots[0]
                  series = plot.series[0]
                  line = series.format.line
                  line.fill.rgb = _green






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 3 at 11:00









                  asmatrkasmatrk

                  324




                  324






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34264715%2fpython-pptx-custom-color-for-each-category%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      Npm cannot find a required file even through it is in the searched directory