how to generate pie chart using dict_values in Python 3.4?

2024/11/15 12:02:11

I wanted the frequency of numbers in a list which I got using Counter library. Also, I got the keys and values using keys = Counter(list).keys() and values = Counter(list).values() respectively, where list is the list of numbers. Their output are in following form:

dict_keys(['0.2200', '0.2700', '0.6200', '0.3000', '0.2500', '0.3400', '0.5600', '0.3900', '0.5400', '0.5500', '0.7500', '0.4100', '0.2800', '0.4200', '0.3800', '0.6600', '0.2000', '0.5200', '0.7200'])

and,dict_values([15, 3, 6, 13, 6, 3, 1, 3, 3, 5, 4, 14, 8, 5, 3, 5, 11, 3, 9])

Now I want to generate a pie-chart using keys and values which i did it in following way:

import matplotlib.pyplot as pyplot
pyplot.axis("equal")
pyplot.pie(float(values),labels=float(keys),autopct=None)
pyplot.show()

But, I get the error as:

TypeError: float() argument must be a string or a number, not 'dict_values'

Is there any way to convert dict_values to string or number as the function used to generate pie chart only accepts string or number?

Answer

You need to loop through the values and convert them individually to floats:

pyplot.pie([float(v) for v in values], labels=[float(k) for k in keys],autopct=None)

The [expression for target in iterable] is called a list comprehension and produces a list object.

I'd not create the Counter() twice; create it just once and reuse:

counts = Counter(yourlist)
pyplot.pie([float(v) for v in counts.values()], labels=[float(k) for k in counts],autopct=None)
https://en.xdnf.cn/q/72254.html

Related Q&A

How can I make start_url in scrapy to consume from a message queue?

I am building a scrapy project in which I have multiple spiders( A spider for each domain). Now, the urls to be scraped come dynamically from a user given query. so basically I do not need to do broad…

Pip install results in this error cl.exe failed with exit code 2

Ive read all of the other questions on this error and frustratingly enough, none give a solution that works. If I run pip install sentencepiece in the cmd line, it gives me the following output.src/sen…

how to communicate two separate python processes?

I have two python programs and I want to communicate them. Both of them are system services and none of them is forked by parent process.Is there any way to do this without using sockets? (eg by cra…

Why does one use of iloc() give a SettingWithCopyWarning, but the other doesnt?

Inside a method from a class i use this statement:self.__datacontainer.iloc[-1][c] = valueDoing this i get a "SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a Data…

Tkinter color name to color object

I need to modify a widgets color in some way, for example, to make it darker, greener, to invert it. The widgets color is given by name, for example, orchid4. How do I get RGB values from a color name …

Creating a TfidfVectorizer over a text column of huge pandas dataframe

I need to get matrix of TF-IDF features from the text stored in columns of a huge dataframe, loaded from a CSV file (which cannot fit in memory). I am trying to iterate over dataframe using chunks but…

Automatically convert jupyter notebook to .py

I know there have been a few questions about this but I have not found anything robust enough.Currently I am using, from terminal, a command that creates .py, then moves them to another folder:jupyter …

Schematron validation with lxml in Python: how to retrieve validation errors?

Im trying to do some Schematron validation with lxml. For the specific application Im working at, its important that any tests that failed the validation are reported back. The lxml documentation menti…

Getting Query Parameters as Dictionary in FastAPI [duplicate]

This question already has answers here:How to get query params including keys with blank values using FastAPI?(2 answers)Closed 6 months ago.I spent last month learning Flask, and am now moving on to …

Python Generated Signature for S3 Post

I think Ive read nearly everything there is to read on base-64 encoding of a signature for in-browser, form-based post to S3: old docs and new docs. For instance:http://doc.s3.amazonaws.com/proposals/…