Home > other >  Error trying to plot pie chart, how to fix?
Error trying to plot pie chart, how to fix?

Time:10-08

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:\Users\KIAN\PycharmProjects\pythonProject\k.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:\Users\KIAN\PycharmProjects\pythonProject\k.py", line 12, in plot = df.plot.pie(y={plot}, figsize=(5, 5))

File "C:\Users\KIAN\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\plotting_core.py", line 1613, in pie return self(kind="pie", **kwargs)

File "C:\Users\KIAN\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\plotting_core.py", line 960, in call data.index.name = y

File "C:\Users\KIAN\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\core\indexes\base.py", line 1751, in name maybe_extract_name(value, None, type(self))

File "C:\Users\KIAN\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\core\indexes\base.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?

CodePudding user response:

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

  • Related