I am new to Pandas and its libraries. By using the following code I can make a scatter plot of my 'class' in the plane 'Month' vs 'Amount'. Because I consider more than one class I would like to use colors for distinguishing each class and to see a legend in the figure.
Below my first attempt can generate dots for each given class having a different color but it can not generate the right legend. On the contrary the second attempt can generate the right legend but labeling is not correct. I can indeed visualize the first letter of each class name. Moreover this second attempt plots as many figures as the number of classes. I would like to see how I can correct both my attempts. Any ideas? suggestions? Thanks in advance.
ps. I wanted to use
colors = itertools.cycle(['gold','blue','red','chocolate','mediumpurple','dodgerblue'])
as well, so that I could decide the colors. I could not make it though.
Attempts:
import pandas as pd
import numpy as np
import random
from matplotlib import pyplot as plt
import matplotlib.cm as cm
np.random.seed(176)
random.seed(16)
df = pd.DataFrame({'class': random.sample(['living room','dining room','kitchen','car','bathroom','office']*10, k=25),
'Amount': np.random.sample(25)*100,
'Year': random.sample(list(range(2010,2018))*50, k=25),
'Month': random.sample(list(range(1,12))*100, k=25)})
print(df.head(25))
print(df['class'].unique())
for cls1 in df['class'].unique():
test1= pd.pivot_table(df[df['class']==cls1], index=['class', 'Month', 'Year'], values=['Amount'])
print(test1)
colors = cm.rainbow(np.linspace(0,2,len(df['class'].unique())))
fig, ax = plt.subplots(figsize=(8,6))
for cls1,c in zip(df['class'].unique(),colors):
# SCATTER PLOT
test = pd.pivot_table(df[df['class']==cls1], index=['class', 'Month', 'Year'], values=['Amount'], aggfunc=np.sum).reset_index()
test.plot(kind='scatter', x='Month',y='Amount', figsize=(16,6),stacked=False,ax=ax,color=c,s=50).legend(df['class'].unique(),scatterpoints=1,loc='upper left',ncol=3,fontsize=10.5)
plt.show()
for cls2,c in zip(df['class'].unique(),colors):
# SCATTER PLOT
test = pd.pivot_table(df[df['class']==cls2], index=['class', 'Month', 'Year'], values=['Amount'], aggfunc=np.sum).reset_index()
test.plot(kind='scatter', x='Month',y='Amount', figsize=(16,6),stacked=False,color=c,s=50).legend(cls2,scatterpoints=1,loc='upper left',ncol=3,fontsize=10.5)
plt.show()
Up-to-date code
I would like to plot the following code via scatter plot.
for cls1 in df['class'].unique():
test3= pd.pivot_table(df[df['class']==cls1], index=['class', 'Month'], values=['Amount'], aggfunc=np.sum)
print(test3)
Unlike above here a class appears only once each month thanks to the sum over Amount.
Here my attempt:
for cls2 in df['class'].unique():
test2= pd.pivot_table(df[df['class']==cls2], index=['class','Year'], values=['Amount'], aggfunc=np.sum).reset_index()
print(test2)
sns.lmplot(x='Year' , y='Amount', data=test2, hue='class',palette='hls', fit_reg=False,size= 5, aspect=5/3, legend_out=False,scatter_kws={"s": 70})
plt.show()
This gives me one plot for each class. A part from the first one (class=car) which shows different colors, the others seem to be ok. Despite this, I would like to have only one plot with all classes..
After the Marvin Taschenberger's useful help here is up-to-date result:
I get a white dot instead a colorful one and the legend has a different place in the figure with respect to your figure. Moreover I can not see the year labels correctly. Why?
