Seaborn Catplot not showing text labels in x axis - python

i am trying to make a bar catplot with a long dataframe that has three columns: property, value and playlist. Basically it was a wide dataframe that i converted to long format using pd.melt(). the problem is that whenever i try to plot it with a bar catplot the categorical data that should be on the x axis that corresponds to the property column, is just showing up as numbers.
Here is an image of how my dataframe looks:
()
and here are my code and how the plot currently looks:
code:
#bar catplot
bar_catplot = sns.catplot(
kind="bar", x="property", y="value", hue="playlist", legend=True, data=long_frame2
)
bar_catplot_figure = bar_catplot.fig
catplot_render = mpld3.fig_to_html(bar_catplot_figure)
and how the plot currently looks:
.
Thank you in advance!

Related

Stacked bar plot in subplots using pandas .plot()

I created a hypothetical DataFrame containing 3 measurements for 20 experiments. Each experiment is associated with a Subject (3 possibilities).
import random
random.seed(42) #set seed
tuples = list(zip(*[list(range(20)),random.choices(['Jean','Marc','Paul'], k = 20)]))#index labels
index=pd.MultiIndex.from_tuples(tuples, names=['num_exp','Subject'])#index
test= pd.DataFrame(np.random.randint(0,100,size=(20, 3)),index=index,columns=['var1','var2','var3']) #DataFrame
test.head() #first lines
head
I succeeded in constructing stacked bar plots with the 3 measurements (each bar is an experiment) for each subject:
test.groupby('Subject').plot(kind='bar', stacked=True,legend=False) #plots
plot1 plot2 plot3
Now, I would like to put each plot (for each subject) in a subplot. If I use the "subplots" argument, it gives me the following :
test.groupby('Subject').plot(kind='bar', stacked=True,legend=False,subplots= True) #plot with subplot
plotsubplot1 plotsubplot2 plotsubplot3
It created a subplot for each measurment because they correspond to columns in my DataFrame.
I don't know how I could do otherwise because I need them as columns to create stacked bars.
So here is my question :
Is it possible to construct this kind of figure with stacked bar plots in subplots (ideally in an elegant way, without iterating) ?
Thanks in advance !
I solved my problem with a simple loop without using anything else than pandas .plot()
Pandas .plot() has an ax parameters for matplotlib axes object.
So, starting from the list of distinct subjects :
subj= list(dict.fromkeys(test.index.get_level_values('Subject')))
I define my subplots :
fig, axs = plt.subplots(1, len(subj))
Then, I have to iterate for each subplot :
for a in range(len(subj)):
test.loc[test.index.get_level_values('Subject') == subj[a]].unstack(level=1).plot(ax= axs[a], kind='bar', stacked=True,legend=False,xlabel='',fontsize=10) #Plot
axs[a].set_title(subj[a],pad=0,fontsize=15) #title
axs[a].tick_params(axis='y', pad=0,size=1) #yticks
And it works well ! :finalresult

add additional bar chart to each facet for each animation frame in plotly

With the following data that has been minimised for the example:
df = {"date":{"0":1635724800000,"1":1635724800000,"2":1635724800000,"3":1635724800000,"4":1635724800000,"5":1635724800000,"6":1635724800000,"7":1635724800000,"8":1635724800000,"9":1635724800000,"10":1635724800000,"11":1635724800000,"12":1635811200000,"13":1635811200000,"14":1635811200000,"15":1635811200000,"16":1635811200000,"17":1635811200000,"18":1635811200000,"19":1635811200000,"20":1635811200000,"21":1635811200000,"22":1635811200000,"23":1635811200000,"24":1635897600000,"25":1635897600000,"26":1635897600000,"27":1635897600000,"28":1635897600000,"29":1635897600000,"30":1635897600000,"31":1635897600000,"32":1635897600000,"33":1635897600000,"34":1635897600000,"35":1635897600000},"consumption":{"0":11.296,"1":11.296,"2":11.296,"3":11.296,"4":11.296,"5":11.296,"6":11.296,"7":11.296,"8":11.296,"9":11.296,"10":11.296,"11":11.296,"12":30.034,"13":30.034,"14":30.034,"15":30.034,"16":30.034,"17":30.034,"18":30.034,"19":30.034,"20":30.034,"21":30.034,"22":30.034,"23":30.034,"24":26.496,"25":26.496,"26":26.496,"27":26.496,"28":26.496,"29":26.496,"30":26.496,"31":26.496,"32":26.496,"33":26.496,"34":26.496,"35":26.496},"training date":{"0":1635465600000,"1":1635552000000,"2":1635638400000,"3":1635465600000,"4":1635552000000,"5":1635638400000,"6":1635465600000,"7":1635552000000,"8":1635638400000,"9":1635465600000,"10":1635552000000,"11":1635638400000,"12":1635552000000,"13":1635638400000,"14":1635724800000,"15":1635552000000,"16":1635638400000,"17":1635724800000,"18":1635552000000,"19":1635638400000,"20":1635724800000,"21":1635552000000,"22":1635638400000,"23":1635724800000,"24":1635638400000,"25":1635724800000,"26":1635811200000,"27":1635638400000,"28":1635724800000,"29":1635811200000,"30":1635638400000,"31":1635724800000,"32":1635811200000,"33":1635638400000,"34":1635724800000,"35":1635811200000},"horizon":{"0":2.0,"1":1.0,"2":0.0,"3":2.0,"4":1.0,"5":0.0,"6":2.0,"7":1.0,"8":0.0,"9":2.0,"10":1.0,"11":0.0,"12":2.0,"13":1.0,"14":0.0,"15":2.0,"16":1.0,"17":0.0,"18":2.0,"19":1.0,"20":0.0,"21":2.0,"22":1.0,"23":0.0,"24":2.0,"25":1.0,"26":0.0,"27":2.0,"28":1.0,"29":0.0,"30":2.0,"31":1.0,"32":0.0,"33":2.0,"34":1.0,"35":0.0},"variable":{"0":"normal_xgb","1":"normal_xgb","2":"normal_xgb","3":"dynamic_xgb","4":"dynamic_xgb","5":"dynamic_xgb","6":"dynamic_regression","7":"dynamic_regression","8":"dynamic_regression","9":"hpopt_dynamic_regression","10":"hpopt_dynamic_regression","11":"hpopt_dynamic_regression","12":"normal_xgb","13":"normal_xgb","14":"normal_xgb","15":"dynamic_xgb","16":"dynamic_xgb","17":"dynamic_xgb","18":"dynamic_regression","19":"dynamic_regression","20":"dynamic_regression","21":"hpopt_dynamic_regression","22":"hpopt_dynamic_regression","23":"hpopt_dynamic_regression","24":"normal_xgb","25":"normal_xgb","26":"normal_xgb","27":"dynamic_xgb","28":"dynamic_xgb","29":"dynamic_xgb","30":"dynamic_regression","31":"dynamic_regression","32":"dynamic_regression","33":"hpopt_dynamic_regression","34":"hpopt_dynamic_regression","35":"hpopt_dynamic_regression"},"value":{"0":30.5324859619,"1":30.5324859619,"2":30.5324859619,"3":30.5541016053,"4":27.745900401,"5":28.6977168159,"6":22.8562196668,"7":15.6343935113,"8":11.9716831707,"9":26.2932791042,"10":19.3785207891,"11":16.6992309714,"12":33.7919235229,"13":33.7919235229,"14":33.7919235229,"15":31.9040133007,"16":28.8557660894,"17":29.524924057,"18":24.8241943235,"19":14.9217208869,"20":14.845629512,"21":20.9837741005,"22":14.5165446043,"23":13.8412872229,"24":37.2903747559,"25":37.2903747559,"26":37.2903747559,"27":33.4694865505,"28":31.3264693922,"29":31.3068936097,"30":27.0967811924,"31":26.8910611794,"32":28.6014841166,"33":20.2269447496,"34":18.5970186305,"35":20.6206687179},"modelid":{"0":"AhmedOctopusEnergy202201231950","1":"AhmedOctopusEnergy202201231950","2":"AhmedOctopusEnergy202201231950","3":"AhmedOctopusEnergy202201231950","4":"AhmedOctopusEnergy202201231950","5":"AhmedOctopusEnergy202201231950","6":"AhmedOctopusEnergy202201231950","7":"AhmedOctopusEnergy202201231950","8":"AhmedOctopusEnergy202201231950","9":"AhmedOctopusEnergy202201231950","10":"AhmedOctopusEnergy202201231950","11":"AhmedOctopusEnergy202201231950","12":"AhmedOctopusEnergy202201231950","13":"AhmedOctopusEnergy202201231950","14":"AhmedOctopusEnergy202201231950","15":"AhmedOctopusEnergy202201231950","16":"AhmedOctopusEnergy202201231950","17":"AhmedOctopusEnergy202201231950","18":"AhmedOctopusEnergy202201231950","19":"AhmedOctopusEnergy202201231950","20":"AhmedOctopusEnergy202201231950","21":"AhmedOctopusEnergy202201231950","22":"AhmedOctopusEnergy202201231950","23":"AhmedOctopusEnergy202201231950","24":"AhmedOctopusEnergy202201231950","25":"AhmedOctopusEnergy202201231950","26":"AhmedOctopusEnergy202201231950","27":"AhmedOctopusEnergy202201231950","28":"AhmedOctopusEnergy202201231950","29":"AhmedOctopusEnergy202201231950","30":"AhmedOctopusEnergy202201231950","31":"AhmedOctopusEnergy202201231950","32":"AhmedOctopusEnergy202201231950","33":"AhmedOctopusEnergy202201231950","34":"AhmedOctopusEnergy202201231950","35":"AhmedOctopusEnergy202201231950"},"mape":{"0":1.7029467034,"1":1.7029467034,"2":1.7029467034,"3":1.7048602696,"4":1.4562588882,"5":1.5405202564,"6":1.0233905512,"7":0.3840645814,"8":0.0598161447,"9":1.3276628102,"10":0.7155206081,"11":0.4783313537,"12":0.1251223121,"13":0.1251223121,"14":0.1251223121,"15":0.0622632117,"16":0.039230003,"17":0.0169499881,"18":0.1734635971,"19":0.5031723751,"20":0.5057058829,"21":0.3013326863,"22":0.5166629618,"23":0.5391460604,"24":0.4073963902,"25":0.4073963902,"26":0.4073963902,"27":0.2631901627,"28":0.1823093823,"29":0.181570562,"30":0.0226744109,"31":0.0149102196,"32":0.0794642254,"33":0.2366038364,"34":0.2981197679,"35":0.2217440852},"variance":{"0":19.2364859619,"1":19.2364859619,"2":19.2364859619,"3":19.2581016053,"4":16.449900401,"5":17.4017168159,"6":11.5602196668,"7":4.3383935113,"8":0.6756831707,"9":14.9972791042,"10":8.0825207891,"11":5.4032309714,"12":3.7579235229,"13":3.7579235229,"14":3.7579235229,"15":1.8700133007,"16":1.1782339106,"17":0.509075943,"18":5.2098056765,"19":15.1122791131,"20":15.188370488,"21":9.0502258995,"22":15.5174553957,"23":16.1927127771,"24":10.7943747559,"25":10.7943747559,"26":10.7943747559,"27":6.9734865505,"28":4.8304693922,"29":4.8108936097,"30":0.6007811924,"31":0.3950611794,"32":2.1054841166,"33":6.2690552504,"34":7.8989813695,"35":5.8753312821}}
I have used the following code
df = pd.read_json(df)
#fig = px.line(data_frame=df[~df['variance'].isnull()].reset_index(drop=True), x='date', y='variance', facet_col='variable',facet_col_wrap=2, animation_frame='horizon',hover_name='training date', color='variable')
#fig.write_html('variance_by_horizon.html')
#the graph i want below:
fig = px.line(data_frame=df[~df['horizon'].isnull()].reset_index(drop=True), x='date', y='value', facet_col='variable', color='variable', facet_col_wrap=2, animation_frame='horizon',hover_name='training date')
#fig.add_trace()
fig.write_html('predictions.html')
to produce a plotly figure faceted by my desired column and animation frame. The figure 'fig' represents the chart without the added bar chart. I intend for the bar chart to have a separate y axis but overlayed on the line chart.
I have attempted to add a bar chart with a new y value: 'variance' to each facet/frame. without the frame dimension, it seems like it's just a case of looping an 'add_trace' with row/col arguments, but i am unsure how to align the charts correctly so that the data matches with the frame too.
what is the correct way to do this?

Matplotlib plot without linear ordered

Is it possible to draw matplotlib chart without using a pandas plot to draw a chart without linear ordering the values on the left?
df = pd.DataFrame({
'x':[3,0,5],
'y':[10,4,20]
})
Chart made with the help of DataFrame:
plt.barh(df['x'],df['y'])
Without dataframe:
x = [3,0,5]
y= [10,4,20]
plt.barh(x,y)
it gives me the same result
Matplotlib chart
Output chart:
df.plot.barh('x','y')
Pandas output chart:
I would like to get such an output only with normal numbers and not numbers as the type of str
plt.barh(['3','0','5'],[10,4,20])
Is it possible? How could i get it?
You can use the index of the dataframe as y parameter and use the x values of the dataframe as tick_label:
plt.barh(df.index, width=df['y'], tick_label=df['x'])

Python Stacked barchart with dataframe

I'm trying to visualize a data frame I have with a stacked barchart, where the x is websites, the y is frequency and then the groups on the barchart are different groups using them.
This is the dataframe:
This is the plot created just by doing this:
web_data_roles.plot(kind='barh', stacked=True, figsize=(20,10))
As you can see its not what I want, vie tried changing the plot so the axes match up to the different columns of the dataframe but it just says no numerical data to plot, Not sure how to go about this anymore. so all help is appreciated
You need to organise your dataframe so that role is a column.
set_index() initial preparation
unstack() to move role out of index and make a column
droplevel() to clean up multi index columns
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1,1, figsize=[10,5],
sharey=False, sharex=False, gridspec_kw={"hspace":0.3})
df = pd.read_csv(io.StringIO("""website,role,freq
www.bbc.co.uk,director,2000
www.bbc.co.uk,technical,500
www.twitter.com,director,4000
www.twitter.com,technical,1500
"""))
df.set_index(["website","role"]).unstack(1).droplevel(0,axis=1).plot(ax=ax, kind="barh", stacked=True)

Python Pandas Matplotlib : How to Plot Graph without Numerics?

I want to plot bar graph or graphs in python using a Pandas dataframe using two columns that don't contain numeric. One column is Operating System, another is computer name, I want to plot a graph between them showing which OS is running over how many Systems, the sample data is like below.
How can I plot bar graph or other graphs for these two colums. When I try the code below:
ax = dfdefault[['Operating System','Computer Name']].plot(kind='bar')
ax.set_xlabel("Hour", fontsize=12)
ax.set_ylabel("V", fontsize=12)
plt.show()
I get this error:
Error:
TypeError: Empty 'DataFrame': no numeric data to plot
You will need to count the occurrence of each operating system first and then plot using a bar graph or pie chart. bar expects numeric data already, which you don't have. Counting will take care of this. Here is an example using a pie chart:
df = pd.DataFrame(
[['asd', 'win'],
['sdf', 'mac'],
['aww', 'win'],
['dd', 'linux']],
columns=['computer', 'os']
)
df['os'].value_counts().plot.pie()
A bar chart would work similarly. Just change pie to bar.

Categories