Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pie chart how to sum values when labels are the same

I made this.

query = "SELECT towar, sztuki, zysk FROM sklep"
mycr.execute(query)
wyniki = mycr.fetchall()
nazwy = []
sztuki = []
for a in wyniki:
 nazwy.append(a[0])
 sztuki.append(a[1])
plt.pie(sztuki, labels=nazwy, autopct='%.1f%%')
plt.show()


Everythink works fine but how to sum percentages when labels are the same?
For example how to sum percentages of "woda" labels?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I’d prefer to use dictionaries. In your case, nazwy is the key (labels) and sztuki are the values stored in dictionary. Here is your modified code:

query = "SELECT towar, sztuki, zysk FROM sklep"
mycr.execute(query)
wyniki = mycr.fetchall()
data_dict = {}
for a in wyniki:
    if a[0] in data_dict.keys():
        data_dict[a[0]] = data_dict[a[0]]+a[1]
    else:
        data_dict[a[0]] = a[1]

nazwy = list(data_dict.keys())
sztuki = list(data_dict.values())
# plotting
plt.pie(sztuki, labels=nazwy, autopct='%.1f%%')
plt.show()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading