plot a scatter graph for a dictionary

Question:

Please I need help I want to plot a graph using

pandas.dataframe.plot(kind='scatter' , x=x , y=y )

for a dictionary type {‘2022-01-30’: 50, ‘2022-01-31’: 152, ‘2022-02-01’: 41 }

Please how can I use dictionary since i need a title

It is giving me the error x is not defined
x is the key and
y is the value

but on the graph x should be ‘Date’ and y should be ‘Quantity’

Asked By: William Page

||

Answers:

You have to create a DataFrame first:

import pandas as pd
import matplotlib.pyplot as plt

data = {'2022-01-30': 50, '2022-01-31': 152, '2022-02-01': 41}
df = pd.Series(data, name='y').rename_axis('x').reset_index()
df.plot(kind='scatter', x='x', y='y')
plt.show()

Output:
enter image description here

Answered By: Corralien

You can’t plot a dict type with a dataframe function. Thus two possibilities :

Answered By: A Francois

Just take out the keys & values

import matplotlib.pyplot as plt

data = {'2022-01-30': 50, '2022-01-31': 152, '2022-02-01': 41}
x, y = data.keys(), data.values()

plt.scatter(x, y)
Answered By: JunXiang
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.