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’
Answers:
You can’t plot a dict type with a dataframe function. Thus two possibilities :
-
use your dictionnary and follow Plot a scatter plot in python with matplotlib with dictionary (use of matplotlib.pyplot.scatter
)
-
convert your dictionnary to dataframe with pd.DataFrame.from_dict(data)
and then use pandas.DataFrame.plot.scatter(x=x, y=y)
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)
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’
You can’t plot a dict type with a dataframe function. Thus two possibilities :
-
use your dictionnary and follow Plot a scatter plot in python with matplotlib with dictionary (use of
matplotlib.pyplot.scatter
) -
convert your dictionnary to dataframe with
pd.DataFrame.from_dict(data)
and then usepandas.DataFrame.plot.scatter(x=x, y=y)
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)