Issue
I have a data frame of this format.
brand-var1-var2(date)-var3
A - 100 - 20/12 - 300
A - 110 - 12/12 - 132
B - 24 - 24/12 - 543
C - 235 - 3/12- 534
I want to print individual separate line chart in jupyter notebook for each brand such that
For brand A
x_axis = df.var2
y_axis = df.var1
then for Brand B
x_axis = df.var2
y_axis = df.var1
then for Brand C and so on...
I tried using Loop with matplotlib but in vain.
from pandas import *
import matplotlib.pyplot as plt
%matplotlib inline
ys = df['orders'], df['gmv']
x_ax = df['brand']
for y_ax in ys:
ts = Series(y_ax,index=x_ax)
ts.plot(kind='bar', figsize=(15,5))
plt.show()
This shows error - ValueError: cannot reindex from a duplicate axis
Thanks
Solution
I'm not sure what type of plot you want. At the beginning of your question, you ask for a line plot but in the code you call kind='bar'
. Since the dataframe looks like it contains times series data, I am going to assume you want a line plot.
I created a random dataframe with the following:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
def make_random_date():
year = np.random.choice(range(2000, 2010))
month = np.random.choice(range(1, 13))
day = np.random.choice(range(1, 29))
return datetime(year, month, day)
# Make a random dataframe
n_samples = 100
df = pd.DataFrame()
df['date'] = [make_random_date() for _ in range(n_samples)]
df['brand'] = [np.random.choice(['A','B','C']) for _ in range(n_samples)]
df['var1'] = np.random.randint(0, 100, size=(n_samples, 1))
df.head()
date brand var1
0 2003-03-27 B 16
1 2009-06-24 C 7
2 2008-04-17 A 82
3 2004-02-20 C 9
4 2007-05-10 B 69
Then to create individual line plots for each brand, do the following:
for brand in ['A', 'B', 'C']:
sub = df.query("brand == @brand")
ts = pd.Series(data=list(sub['var1']), index=sub['date'])
ts.plot()
plt.title(f'Brand: {brand}')
plt.show()
And this is the output:
Answered By - vivek
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.