Error trying to plot pie chart, how to fix?

Question:

I was trying out plotting graphs for the first time and tried to do a bit of code where you can enter what parameter you want to graph, and then it graphs it as a pie chart. But when I tried running it, it returns a bunch of errors

import pandas as pd
import matplotlib

plot = input()
plot = str(plot)

df = pd.DataFrame({'Perimeter': [8, 16, 20],
                   'Area': [2, 16, 25]},
                  index=['Square 1', 'Square 2', 'Square 3'])
plot = df.plot.pie(y={plot}, figsize=(5, 5))

The errors are:

C:UsersKIANPycharmProjectspythonProjectk.py:12: FutureWarning:
Passing a set as an indexer is deprecated and will raise in a future
version. Use a list instead. plot = df.plot.pie(y={plot},
figsize=(5, 5))

Traceback (most recent call last):

File "C:UsersKIANPycharmProjectspythonProjectk.py", line 12, in

plot = df.plot.pie(y={plot}, figsize=(5, 5))

File
"C:UsersKIANPycharmProjectspythonProjectvenvlibsite-packagespandasplotting_core.py",
line 1613, in pie
return self(kind="pie", **kwargs)

File
"C:UsersKIANPycharmProjectspythonProjectvenvlibsite-packagespandasplotting_core.py",
line 960, in call
data.index.name = y

File
"C:UsersKIANPycharmProjectspythonProjectvenvlibsite-packagespandascoreindexesbase.py",
line 1751, in name
maybe_extract_name(value, None, type(self))

File
"C:UsersKIANPycharmProjectspythonProjectvenvlibsite-packagespandascoreindexesbase.py",
line 7421, in maybe_extract_name
raise TypeError(f"{cls.name}.name must be a hashable type")

TypeError: Index.name must be a hashable type

Any idea how to fix these or what is causing them?

Asked By: Kian Murray

||

Answers:

The variable plot shouldn’t be in a set, you don’t even need to assign it to a str, input will be already string even if you enter numbers:

plot = input()

df = pd.DataFrame({'Perimeter': [8, 16, 20],
                   'Area': [2, 16, 25]},
                  index=['Square 1', 'Square 2', 'Square 3'])
plot = df.plot.pie(y=plot, figsize=(5, 5))

Output for Area:

image

Answered By: Nuri Taş