laitimes

python 100 days 61 pie function to draw pie chart effect control

author:Hong Guan did not tune the programmers

Continuing the basics of pie charts above, if you want to continue to control the effect color of pie charts, etc

python 100 days 61 pie function to draw pie chart effect control

The default color is like this

python 100 days 61 pie function to draw pie chart effect control

At this point we need to define a color for the first pie chart area

python 100 days 61 pie function to draw pie chart effect control

Add a color configuration item.

mycolors = ["black",
                    "hotpink",
                    "b",
                    "#4CAF50"
           ]

plt.pie(y, labels = mylabels, \
          startangle = 90, \
          explode = myexplode
          ,colors = mycolors
)           

What if you want to achieve such an effect?

python 100 days 61 pie function to draw pie chart effect control
plt.pie(y, labels = mylabels, \
startangle = 90, \
explode = myexplode
,colors = mycolors
)
plt.legend()
           
  • plt.legend() plus this method adds a summary to the diagram

If you want to achieve the following effect?

python 100 days 61 pie function to draw pie chart effect control

Finally, the complete code

'''
Created on 2023年1月14日
@author: admin
'''
import matplotlib.pyplot as plt
import numpy as np
from pandas._libs.reshape import explode
from openpyxl.utils.cell import col
y = np.array([35, 25, 25, 15])
import matplotlib.pyplot as plt
import numpy as np
mylabels = ["Apples",
                "Bananas",
                "Cherries",
                "Dates"]

myexplode = [0.2, 0, 0, 0]
mycolors = ["black",
            "hotpink",
            "b",
            "#4CAF50"]
plt.pie(y, labels = mylabels, \
                    startangle = 90, \
                    explode = myexplode
                    ,colors = mycolors
                    )
plt.legend(title = "Four Fruits :")

plt.show()           

Finally, what effect would it have if Chinese title was added? You can try it in the cloud.