labels' placing in pie chart










0














I am trying to make the following pie chart prettier:



In particular, I have an obvious problem with the labels. I want to rotate them and center them above their corresponding pie slice, but I am only able to rotate them. Moreover, the rotation requires a lot of tweaking by setting
pie_properties[1][0].set_rotation(30) with a different degrees values for every label.



Is there a way to make it automatically, or at least in a simpler way?





Here is my code:



import matplotlib.pyplot as plt
import numpy as np


fig, ax = plt.subplots()

### Data structure:
# Network type:
group_names = ['Group 1', 'Group 2', 'Group 3']
group_vals = [43,49,38]
group_colors = ['w']*3

#Information level:
info_names = ['Subgroup 1', 'Subgroup 2',
'Subgroup 1', 'Subgroup 2',
'SSubgroup 1', 'Subgroup 2']
info_vals = np.array([[27,16],[26,23],[14,24]])
my_blue = [x/255 for x in [30,144,255]]
info_colors = [my_blue,'gray',
my_blue,'gray',
my_blue,'gray']

corr_vals = np.array([[10,3,7,3,4], [4,4,4,2,2],
[10,5,5,3,3], [10,5,5,1,2],
[4,4,2,2,2], [12,2,2,1,7]])
pale_green = [x/255 for x in [152,251,152]]
pale_red = [x/255 for x in [240,128,128]]
pale_gray = [x/255 for x in [169,169,169]]
corr_colors = ['green',pale_green,pale_gray,pale_red,'red', 'green',pale_green,pale_gray,pale_red,'red',
'green',pale_green,pale_gray,pale_red,'red', 'green',pale_green,pale_gray,pale_red,'red',
'green',pale_green,pale_gray,pale_red,'red', 'green',pale_green,pale_gray,pale_red,'red',]

#inner layer
pie_properties = ax.pie(group_vals, radius=1, colors=group_colors,
labels=group_names, labeldistance=0.7,
wedgeprops=dict(width=0.3, edgecolor='k'))
pie_properties[1][0].set_rotation(-45) #<===rotation
pie_properties[1][1].set_rotation(90)
pie_properties[1][2].set_rotation(-135)

#middle layer
pie_properties = ax.pie(info_vals.flatten(), radius=1+0.4, colors=info_colors,
labels=info_names, labeldistance=0.7,
wedgeprops=dict(width=0.4, edgecolor='w'))
pie_properties[1][0].set_rotation(-45)
pie_properties[1][1].set_rotation(15)
pie_properties[1][2].set_rotation(65)
pie_properties[1][3].set_rotation(125)
pie_properties[1][4].set_rotation(-160)
pie_properties[1][5].set_rotation(-125)

#outer layer
ax.pie(corr_vals.flatten(), radius=1+0.4+0.5, colors=corr_colors,
wedgeprops=dict(width=0.5, edgecolor='w'))


ax.set(aspect="equal")
plt.show()









share|improve this question


























    0














    I am trying to make the following pie chart prettier:



    In particular, I have an obvious problem with the labels. I want to rotate them and center them above their corresponding pie slice, but I am only able to rotate them. Moreover, the rotation requires a lot of tweaking by setting
    pie_properties[1][0].set_rotation(30) with a different degrees values for every label.



    Is there a way to make it automatically, or at least in a simpler way?





    Here is my code:



    import matplotlib.pyplot as plt
    import numpy as np


    fig, ax = plt.subplots()

    ### Data structure:
    # Network type:
    group_names = ['Group 1', 'Group 2', 'Group 3']
    group_vals = [43,49,38]
    group_colors = ['w']*3

    #Information level:
    info_names = ['Subgroup 1', 'Subgroup 2',
    'Subgroup 1', 'Subgroup 2',
    'SSubgroup 1', 'Subgroup 2']
    info_vals = np.array([[27,16],[26,23],[14,24]])
    my_blue = [x/255 for x in [30,144,255]]
    info_colors = [my_blue,'gray',
    my_blue,'gray',
    my_blue,'gray']

    corr_vals = np.array([[10,3,7,3,4], [4,4,4,2,2],
    [10,5,5,3,3], [10,5,5,1,2],
    [4,4,2,2,2], [12,2,2,1,7]])
    pale_green = [x/255 for x in [152,251,152]]
    pale_red = [x/255 for x in [240,128,128]]
    pale_gray = [x/255 for x in [169,169,169]]
    corr_colors = ['green',pale_green,pale_gray,pale_red,'red', 'green',pale_green,pale_gray,pale_red,'red',
    'green',pale_green,pale_gray,pale_red,'red', 'green',pale_green,pale_gray,pale_red,'red',
    'green',pale_green,pale_gray,pale_red,'red', 'green',pale_green,pale_gray,pale_red,'red',]

    #inner layer
    pie_properties = ax.pie(group_vals, radius=1, colors=group_colors,
    labels=group_names, labeldistance=0.7,
    wedgeprops=dict(width=0.3, edgecolor='k'))
    pie_properties[1][0].set_rotation(-45) #<===rotation
    pie_properties[1][1].set_rotation(90)
    pie_properties[1][2].set_rotation(-135)

    #middle layer
    pie_properties = ax.pie(info_vals.flatten(), radius=1+0.4, colors=info_colors,
    labels=info_names, labeldistance=0.7,
    wedgeprops=dict(width=0.4, edgecolor='w'))
    pie_properties[1][0].set_rotation(-45)
    pie_properties[1][1].set_rotation(15)
    pie_properties[1][2].set_rotation(65)
    pie_properties[1][3].set_rotation(125)
    pie_properties[1][4].set_rotation(-160)
    pie_properties[1][5].set_rotation(-125)

    #outer layer
    ax.pie(corr_vals.flatten(), radius=1+0.4+0.5, colors=corr_colors,
    wedgeprops=dict(width=0.5, edgecolor='w'))


    ax.set(aspect="equal")
    plt.show()









    share|improve this question
























      0












      0








      0







      I am trying to make the following pie chart prettier:



      In particular, I have an obvious problem with the labels. I want to rotate them and center them above their corresponding pie slice, but I am only able to rotate them. Moreover, the rotation requires a lot of tweaking by setting
      pie_properties[1][0].set_rotation(30) with a different degrees values for every label.



      Is there a way to make it automatically, or at least in a simpler way?





      Here is my code:



      import matplotlib.pyplot as plt
      import numpy as np


      fig, ax = plt.subplots()

      ### Data structure:
      # Network type:
      group_names = ['Group 1', 'Group 2', 'Group 3']
      group_vals = [43,49,38]
      group_colors = ['w']*3

      #Information level:
      info_names = ['Subgroup 1', 'Subgroup 2',
      'Subgroup 1', 'Subgroup 2',
      'SSubgroup 1', 'Subgroup 2']
      info_vals = np.array([[27,16],[26,23],[14,24]])
      my_blue = [x/255 for x in [30,144,255]]
      info_colors = [my_blue,'gray',
      my_blue,'gray',
      my_blue,'gray']

      corr_vals = np.array([[10,3,7,3,4], [4,4,4,2,2],
      [10,5,5,3,3], [10,5,5,1,2],
      [4,4,2,2,2], [12,2,2,1,7]])
      pale_green = [x/255 for x in [152,251,152]]
      pale_red = [x/255 for x in [240,128,128]]
      pale_gray = [x/255 for x in [169,169,169]]
      corr_colors = ['green',pale_green,pale_gray,pale_red,'red', 'green',pale_green,pale_gray,pale_red,'red',
      'green',pale_green,pale_gray,pale_red,'red', 'green',pale_green,pale_gray,pale_red,'red',
      'green',pale_green,pale_gray,pale_red,'red', 'green',pale_green,pale_gray,pale_red,'red',]

      #inner layer
      pie_properties = ax.pie(group_vals, radius=1, colors=group_colors,
      labels=group_names, labeldistance=0.7,
      wedgeprops=dict(width=0.3, edgecolor='k'))
      pie_properties[1][0].set_rotation(-45) #<===rotation
      pie_properties[1][1].set_rotation(90)
      pie_properties[1][2].set_rotation(-135)

      #middle layer
      pie_properties = ax.pie(info_vals.flatten(), radius=1+0.4, colors=info_colors,
      labels=info_names, labeldistance=0.7,
      wedgeprops=dict(width=0.4, edgecolor='w'))
      pie_properties[1][0].set_rotation(-45)
      pie_properties[1][1].set_rotation(15)
      pie_properties[1][2].set_rotation(65)
      pie_properties[1][3].set_rotation(125)
      pie_properties[1][4].set_rotation(-160)
      pie_properties[1][5].set_rotation(-125)

      #outer layer
      ax.pie(corr_vals.flatten(), radius=1+0.4+0.5, colors=corr_colors,
      wedgeprops=dict(width=0.5, edgecolor='w'))


      ax.set(aspect="equal")
      plt.show()









      share|improve this question













      I am trying to make the following pie chart prettier:



      In particular, I have an obvious problem with the labels. I want to rotate them and center them above their corresponding pie slice, but I am only able to rotate them. Moreover, the rotation requires a lot of tweaking by setting
      pie_properties[1][0].set_rotation(30) with a different degrees values for every label.



      Is there a way to make it automatically, or at least in a simpler way?





      Here is my code:



      import matplotlib.pyplot as plt
      import numpy as np


      fig, ax = plt.subplots()

      ### Data structure:
      # Network type:
      group_names = ['Group 1', 'Group 2', 'Group 3']
      group_vals = [43,49,38]
      group_colors = ['w']*3

      #Information level:
      info_names = ['Subgroup 1', 'Subgroup 2',
      'Subgroup 1', 'Subgroup 2',
      'SSubgroup 1', 'Subgroup 2']
      info_vals = np.array([[27,16],[26,23],[14,24]])
      my_blue = [x/255 for x in [30,144,255]]
      info_colors = [my_blue,'gray',
      my_blue,'gray',
      my_blue,'gray']

      corr_vals = np.array([[10,3,7,3,4], [4,4,4,2,2],
      [10,5,5,3,3], [10,5,5,1,2],
      [4,4,2,2,2], [12,2,2,1,7]])
      pale_green = [x/255 for x in [152,251,152]]
      pale_red = [x/255 for x in [240,128,128]]
      pale_gray = [x/255 for x in [169,169,169]]
      corr_colors = ['green',pale_green,pale_gray,pale_red,'red', 'green',pale_green,pale_gray,pale_red,'red',
      'green',pale_green,pale_gray,pale_red,'red', 'green',pale_green,pale_gray,pale_red,'red',
      'green',pale_green,pale_gray,pale_red,'red', 'green',pale_green,pale_gray,pale_red,'red',]

      #inner layer
      pie_properties = ax.pie(group_vals, radius=1, colors=group_colors,
      labels=group_names, labeldistance=0.7,
      wedgeprops=dict(width=0.3, edgecolor='k'))
      pie_properties[1][0].set_rotation(-45) #<===rotation
      pie_properties[1][1].set_rotation(90)
      pie_properties[1][2].set_rotation(-135)

      #middle layer
      pie_properties = ax.pie(info_vals.flatten(), radius=1+0.4, colors=info_colors,
      labels=info_names, labeldistance=0.7,
      wedgeprops=dict(width=0.4, edgecolor='w'))
      pie_properties[1][0].set_rotation(-45)
      pie_properties[1][1].set_rotation(15)
      pie_properties[1][2].set_rotation(65)
      pie_properties[1][3].set_rotation(125)
      pie_properties[1][4].set_rotation(-160)
      pie_properties[1][5].set_rotation(-125)

      #outer layer
      ax.pie(corr_vals.flatten(), radius=1+0.4+0.5, colors=corr_colors,
      wedgeprops=dict(width=0.5, edgecolor='w'))


      ax.set(aspect="equal")
      plt.show()






      python matplotlib label






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 '18 at 7:43









      shamalaia

      1,33121222




      1,33121222






















          1 Answer
          1






          active

          oldest

          votes


















          1














          I suppose you can calculate the angle needed with a little bit of triginometry. Since the label is placed at the mean angular extent of the pie wedge, calculating the angle is easy. Depending on whether the text is in the lower or upper half, you may add or subtract 90 degrees.



          import matplotlib.pyplot as plt
          import numpy as np

          fig, ax = plt.subplots()

          group_names = ['Group 1', 'Group 2', 'Group 3']
          group_vals = [43,49,38]
          group_colors = ['w']*3

          info_names = ['Subgroup 1', 'Subgroup 2',
          'Subgroup 1', 'Subgroup 2',
          'Subgroup 1', 'Subgroup 2']
          info_vals = np.array([[27,16],[26,23],[14,24]])
          my_blue = np.array([30,144,255])/255
          info_colors = [my_blue,'gray']*3

          corr_vals = np.array([[10,3,7,3,4], [4,4,4,2,2],
          [10,5,5,3,3], [10,5,5,1,2],
          [4,4,2,2,2], [12,2,2,1,7]])
          pale_green = np.array([152,251,152])/255
          pale_red = np.array([240,128,128])/255
          pale_gray = np.array([169,169,169])/255
          corr_colors = ['green',pale_green,pale_gray,pale_red,'red']*6

          def rotatetext(text):
          angle = np.rad2deg(np.arctan2(*text.get_position()[::-1]))
          text.set(rotation = angle - 90*np.sign(angle),
          rotation_mode="anchor", ha="center", va="center")

          #inner layer
          pie_properties = ax.pie(group_vals, radius=1/1.9, colors=group_colors,
          labels=group_names, labeldistance=.8,
          wedgeprops=dict(width=0.3/1.9, edgecolor='k'))

          for text in pie_properties[1]:
          rotatetext(text)

          #middle layer
          pie_properties = ax.pie(info_vals.flatten(), radius=(1+0.4)/1.9, colors=info_colors,
          labels=info_names, labeldistance=.82,
          wedgeprops=dict(width=0.4/1.9, edgecolor='w'))

          for text in pie_properties[1]:
          rotatetext(text)

          #outer layer
          ax.pie(corr_vals.flatten(), radius=(1+0.4+0.5)/1.9, colors=corr_colors,
          wedgeprops=dict(width=0.5/1.9, edgecolor='w'))

          ax.set(xlim=(-1,1), ylim=(-1,1))
          ax.set(aspect="equal")
          plt.show()


          enter image description here



          The best radial position however needs manual tweaking. Here I chose 0.80 and 0.82 respectively, but that'll sure depend on the figure- and font-size.






          share|improve this answer




















          • Hi, do you know how to add trailing space to labels? I added label with numbers in the outer layer too. To center them better I need to add some trailing spaces to some of them. I tried various solutions: '2t', '2'.ljust(2), ' '+'2', '$ $2, but none of them worked. The trailing space is always removed.
            – shamalaia
            Nov 16 '18 at 1:36











          • This sounds like it's unrelated to a nested pie chart. If there is no solution to this found anywhere else on this site, can you ask a new question about it with a simple example showing the issue?
            – ImportanceOfBeingErnest
            Nov 16 '18 at 10:29










          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%2f53236994%2flabels-placing-in-pie-chart%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          I suppose you can calculate the angle needed with a little bit of triginometry. Since the label is placed at the mean angular extent of the pie wedge, calculating the angle is easy. Depending on whether the text is in the lower or upper half, you may add or subtract 90 degrees.



          import matplotlib.pyplot as plt
          import numpy as np

          fig, ax = plt.subplots()

          group_names = ['Group 1', 'Group 2', 'Group 3']
          group_vals = [43,49,38]
          group_colors = ['w']*3

          info_names = ['Subgroup 1', 'Subgroup 2',
          'Subgroup 1', 'Subgroup 2',
          'Subgroup 1', 'Subgroup 2']
          info_vals = np.array([[27,16],[26,23],[14,24]])
          my_blue = np.array([30,144,255])/255
          info_colors = [my_blue,'gray']*3

          corr_vals = np.array([[10,3,7,3,4], [4,4,4,2,2],
          [10,5,5,3,3], [10,5,5,1,2],
          [4,4,2,2,2], [12,2,2,1,7]])
          pale_green = np.array([152,251,152])/255
          pale_red = np.array([240,128,128])/255
          pale_gray = np.array([169,169,169])/255
          corr_colors = ['green',pale_green,pale_gray,pale_red,'red']*6

          def rotatetext(text):
          angle = np.rad2deg(np.arctan2(*text.get_position()[::-1]))
          text.set(rotation = angle - 90*np.sign(angle),
          rotation_mode="anchor", ha="center", va="center")

          #inner layer
          pie_properties = ax.pie(group_vals, radius=1/1.9, colors=group_colors,
          labels=group_names, labeldistance=.8,
          wedgeprops=dict(width=0.3/1.9, edgecolor='k'))

          for text in pie_properties[1]:
          rotatetext(text)

          #middle layer
          pie_properties = ax.pie(info_vals.flatten(), radius=(1+0.4)/1.9, colors=info_colors,
          labels=info_names, labeldistance=.82,
          wedgeprops=dict(width=0.4/1.9, edgecolor='w'))

          for text in pie_properties[1]:
          rotatetext(text)

          #outer layer
          ax.pie(corr_vals.flatten(), radius=(1+0.4+0.5)/1.9, colors=corr_colors,
          wedgeprops=dict(width=0.5/1.9, edgecolor='w'))

          ax.set(xlim=(-1,1), ylim=(-1,1))
          ax.set(aspect="equal")
          plt.show()


          enter image description here



          The best radial position however needs manual tweaking. Here I chose 0.80 and 0.82 respectively, but that'll sure depend on the figure- and font-size.






          share|improve this answer




















          • Hi, do you know how to add trailing space to labels? I added label with numbers in the outer layer too. To center them better I need to add some trailing spaces to some of them. I tried various solutions: '2t', '2'.ljust(2), ' '+'2', '$ $2, but none of them worked. The trailing space is always removed.
            – shamalaia
            Nov 16 '18 at 1:36











          • This sounds like it's unrelated to a nested pie chart. If there is no solution to this found anywhere else on this site, can you ask a new question about it with a simple example showing the issue?
            – ImportanceOfBeingErnest
            Nov 16 '18 at 10:29















          1














          I suppose you can calculate the angle needed with a little bit of triginometry. Since the label is placed at the mean angular extent of the pie wedge, calculating the angle is easy. Depending on whether the text is in the lower or upper half, you may add or subtract 90 degrees.



          import matplotlib.pyplot as plt
          import numpy as np

          fig, ax = plt.subplots()

          group_names = ['Group 1', 'Group 2', 'Group 3']
          group_vals = [43,49,38]
          group_colors = ['w']*3

          info_names = ['Subgroup 1', 'Subgroup 2',
          'Subgroup 1', 'Subgroup 2',
          'Subgroup 1', 'Subgroup 2']
          info_vals = np.array([[27,16],[26,23],[14,24]])
          my_blue = np.array([30,144,255])/255
          info_colors = [my_blue,'gray']*3

          corr_vals = np.array([[10,3,7,3,4], [4,4,4,2,2],
          [10,5,5,3,3], [10,5,5,1,2],
          [4,4,2,2,2], [12,2,2,1,7]])
          pale_green = np.array([152,251,152])/255
          pale_red = np.array([240,128,128])/255
          pale_gray = np.array([169,169,169])/255
          corr_colors = ['green',pale_green,pale_gray,pale_red,'red']*6

          def rotatetext(text):
          angle = np.rad2deg(np.arctan2(*text.get_position()[::-1]))
          text.set(rotation = angle - 90*np.sign(angle),
          rotation_mode="anchor", ha="center", va="center")

          #inner layer
          pie_properties = ax.pie(group_vals, radius=1/1.9, colors=group_colors,
          labels=group_names, labeldistance=.8,
          wedgeprops=dict(width=0.3/1.9, edgecolor='k'))

          for text in pie_properties[1]:
          rotatetext(text)

          #middle layer
          pie_properties = ax.pie(info_vals.flatten(), radius=(1+0.4)/1.9, colors=info_colors,
          labels=info_names, labeldistance=.82,
          wedgeprops=dict(width=0.4/1.9, edgecolor='w'))

          for text in pie_properties[1]:
          rotatetext(text)

          #outer layer
          ax.pie(corr_vals.flatten(), radius=(1+0.4+0.5)/1.9, colors=corr_colors,
          wedgeprops=dict(width=0.5/1.9, edgecolor='w'))

          ax.set(xlim=(-1,1), ylim=(-1,1))
          ax.set(aspect="equal")
          plt.show()


          enter image description here



          The best radial position however needs manual tweaking. Here I chose 0.80 and 0.82 respectively, but that'll sure depend on the figure- and font-size.






          share|improve this answer




















          • Hi, do you know how to add trailing space to labels? I added label with numbers in the outer layer too. To center them better I need to add some trailing spaces to some of them. I tried various solutions: '2t', '2'.ljust(2), ' '+'2', '$ $2, but none of them worked. The trailing space is always removed.
            – shamalaia
            Nov 16 '18 at 1:36











          • This sounds like it's unrelated to a nested pie chart. If there is no solution to this found anywhere else on this site, can you ask a new question about it with a simple example showing the issue?
            – ImportanceOfBeingErnest
            Nov 16 '18 at 10:29













          1












          1








          1






          I suppose you can calculate the angle needed with a little bit of triginometry. Since the label is placed at the mean angular extent of the pie wedge, calculating the angle is easy. Depending on whether the text is in the lower or upper half, you may add or subtract 90 degrees.



          import matplotlib.pyplot as plt
          import numpy as np

          fig, ax = plt.subplots()

          group_names = ['Group 1', 'Group 2', 'Group 3']
          group_vals = [43,49,38]
          group_colors = ['w']*3

          info_names = ['Subgroup 1', 'Subgroup 2',
          'Subgroup 1', 'Subgroup 2',
          'Subgroup 1', 'Subgroup 2']
          info_vals = np.array([[27,16],[26,23],[14,24]])
          my_blue = np.array([30,144,255])/255
          info_colors = [my_blue,'gray']*3

          corr_vals = np.array([[10,3,7,3,4], [4,4,4,2,2],
          [10,5,5,3,3], [10,5,5,1,2],
          [4,4,2,2,2], [12,2,2,1,7]])
          pale_green = np.array([152,251,152])/255
          pale_red = np.array([240,128,128])/255
          pale_gray = np.array([169,169,169])/255
          corr_colors = ['green',pale_green,pale_gray,pale_red,'red']*6

          def rotatetext(text):
          angle = np.rad2deg(np.arctan2(*text.get_position()[::-1]))
          text.set(rotation = angle - 90*np.sign(angle),
          rotation_mode="anchor", ha="center", va="center")

          #inner layer
          pie_properties = ax.pie(group_vals, radius=1/1.9, colors=group_colors,
          labels=group_names, labeldistance=.8,
          wedgeprops=dict(width=0.3/1.9, edgecolor='k'))

          for text in pie_properties[1]:
          rotatetext(text)

          #middle layer
          pie_properties = ax.pie(info_vals.flatten(), radius=(1+0.4)/1.9, colors=info_colors,
          labels=info_names, labeldistance=.82,
          wedgeprops=dict(width=0.4/1.9, edgecolor='w'))

          for text in pie_properties[1]:
          rotatetext(text)

          #outer layer
          ax.pie(corr_vals.flatten(), radius=(1+0.4+0.5)/1.9, colors=corr_colors,
          wedgeprops=dict(width=0.5/1.9, edgecolor='w'))

          ax.set(xlim=(-1,1), ylim=(-1,1))
          ax.set(aspect="equal")
          plt.show()


          enter image description here



          The best radial position however needs manual tweaking. Here I chose 0.80 and 0.82 respectively, but that'll sure depend on the figure- and font-size.






          share|improve this answer












          I suppose you can calculate the angle needed with a little bit of triginometry. Since the label is placed at the mean angular extent of the pie wedge, calculating the angle is easy. Depending on whether the text is in the lower or upper half, you may add or subtract 90 degrees.



          import matplotlib.pyplot as plt
          import numpy as np

          fig, ax = plt.subplots()

          group_names = ['Group 1', 'Group 2', 'Group 3']
          group_vals = [43,49,38]
          group_colors = ['w']*3

          info_names = ['Subgroup 1', 'Subgroup 2',
          'Subgroup 1', 'Subgroup 2',
          'Subgroup 1', 'Subgroup 2']
          info_vals = np.array([[27,16],[26,23],[14,24]])
          my_blue = np.array([30,144,255])/255
          info_colors = [my_blue,'gray']*3

          corr_vals = np.array([[10,3,7,3,4], [4,4,4,2,2],
          [10,5,5,3,3], [10,5,5,1,2],
          [4,4,2,2,2], [12,2,2,1,7]])
          pale_green = np.array([152,251,152])/255
          pale_red = np.array([240,128,128])/255
          pale_gray = np.array([169,169,169])/255
          corr_colors = ['green',pale_green,pale_gray,pale_red,'red']*6

          def rotatetext(text):
          angle = np.rad2deg(np.arctan2(*text.get_position()[::-1]))
          text.set(rotation = angle - 90*np.sign(angle),
          rotation_mode="anchor", ha="center", va="center")

          #inner layer
          pie_properties = ax.pie(group_vals, radius=1/1.9, colors=group_colors,
          labels=group_names, labeldistance=.8,
          wedgeprops=dict(width=0.3/1.9, edgecolor='k'))

          for text in pie_properties[1]:
          rotatetext(text)

          #middle layer
          pie_properties = ax.pie(info_vals.flatten(), radius=(1+0.4)/1.9, colors=info_colors,
          labels=info_names, labeldistance=.82,
          wedgeprops=dict(width=0.4/1.9, edgecolor='w'))

          for text in pie_properties[1]:
          rotatetext(text)

          #outer layer
          ax.pie(corr_vals.flatten(), radius=(1+0.4+0.5)/1.9, colors=corr_colors,
          wedgeprops=dict(width=0.5/1.9, edgecolor='w'))

          ax.set(xlim=(-1,1), ylim=(-1,1))
          ax.set(aspect="equal")
          plt.show()


          enter image description here



          The best radial position however needs manual tweaking. Here I chose 0.80 and 0.82 respectively, but that'll sure depend on the figure- and font-size.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 '18 at 3:05









          ImportanceOfBeingErnest

          125k10128204




          125k10128204











          • Hi, do you know how to add trailing space to labels? I added label with numbers in the outer layer too. To center them better I need to add some trailing spaces to some of them. I tried various solutions: '2t', '2'.ljust(2), ' '+'2', '$ $2, but none of them worked. The trailing space is always removed.
            – shamalaia
            Nov 16 '18 at 1:36











          • This sounds like it's unrelated to a nested pie chart. If there is no solution to this found anywhere else on this site, can you ask a new question about it with a simple example showing the issue?
            – ImportanceOfBeingErnest
            Nov 16 '18 at 10:29
















          • Hi, do you know how to add trailing space to labels? I added label with numbers in the outer layer too. To center them better I need to add some trailing spaces to some of them. I tried various solutions: '2t', '2'.ljust(2), ' '+'2', '$ $2, but none of them worked. The trailing space is always removed.
            – shamalaia
            Nov 16 '18 at 1:36











          • This sounds like it's unrelated to a nested pie chart. If there is no solution to this found anywhere else on this site, can you ask a new question about it with a simple example showing the issue?
            – ImportanceOfBeingErnest
            Nov 16 '18 at 10:29















          Hi, do you know how to add trailing space to labels? I added label with numbers in the outer layer too. To center them better I need to add some trailing spaces to some of them. I tried various solutions: '2t', '2'.ljust(2), ' '+'2', '$ $2, but none of them worked. The trailing space is always removed.
          – shamalaia
          Nov 16 '18 at 1:36





          Hi, do you know how to add trailing space to labels? I added label with numbers in the outer layer too. To center them better I need to add some trailing spaces to some of them. I tried various solutions: '2t', '2'.ljust(2), ' '+'2', '$ $2, but none of them worked. The trailing space is always removed.
          – shamalaia
          Nov 16 '18 at 1:36













          This sounds like it's unrelated to a nested pie chart. If there is no solution to this found anywhere else on this site, can you ask a new question about it with a simple example showing the issue?
          – ImportanceOfBeingErnest
          Nov 16 '18 at 10:29




          This sounds like it's unrelated to a nested pie chart. If there is no solution to this found anywhere else on this site, can you ask a new question about it with a simple example showing the issue?
          – ImportanceOfBeingErnest
          Nov 16 '18 at 10:29

















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53236994%2flabels-placing-in-pie-chart%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

          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

          Crossroads (UK TV series)

          ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế