You never knew you needed Plotly! You can’t stop using Plotly for visualisation. Best library features the most user-friendly charts. This article will discuss the different Bar charts available in Plotly.
The data is from Kaggle. You can find the entire notebook here. This is the idea behind creating a chart. The way you present data is more powerful than the charts that you use.
However, it’s also important to choose the right chart. Let’s get started! Data. The purpose of creating a chart is to show which TV Shows or Movies have the highest ratings on Netflix.
First Plot Plotted the stacked bar chart to compare TV Shows with Movies. The chart shows that Netflix’s viewers prefer movies to TV Shows. The Highest rating is given to Movies and TV Show having TV-MA tag which tell us that most of the content on Netflix is for the mature audience and not for age 17 years and under.
Code: fig=px.histogram (df, rating=y, color=type=’type’). Stacked Bar chart — Although I like the Stacked Bar Chart, it can be difficult to find TV shows on the chart. Sometimes, the TV Show Value is not visible at first glance. There is another way. Code for the Group Bar Chart: fig =.px.histogram (df), y=’rating’, color=’type’, barmode=’group’) The Group Bar Chart — Plotly I now have the ability to clearly compare ratings between TV shows and movies.
It looks quite elegant now but in TV Show ‘R’ rating have a very small value and, ‘PG-13’, ‘PG’, etc do not have any value. This information is likely to be overlooked by the audience because its importance is so low. Is there a better chart? Let’s see. The Bidirectional bar chart — Plotly.
This chart is one of the most useful. It allows you to easily compare TV shows and movies based on their ratings. Code: # making a copy of dfdff = df.copy #1. making a df one for tv show with ratingdf_tv_show = dff[dff[‘type’]==’TV Show’][[‘rating’, ‘type’]].rename(columns=’type’:’tv_show’) # making a df for movie with ratingdf_movie = dff[dff[‘type’]==’Movie’][[‘rating’, ‘type’]].rename(columns=’type’:’movie’) Making a data frame for each TV Show and Movie with Ratings. Renaming the column to “tv_show” from the Type=TV Show data.
This Data Frame shows the df_tv_show. Select the Type= Movie data with a rating, and then rename the column to “movie”. This Data Frame shows the df_movie. Now, let’s see how we can find the rating value in df_tv_show or df_movie. # 2.df_tv_show = df_tv_show.rating.value_countsdf_tv_show = pd.DataFrame(df_tv_show).reset_index.rename(columns=’index’:’tv_show’)df_tv_show[‘rating_final’] = df_tv_show[‘rating’] # making rating column value negativedf_tv_show[‘rating’] *= -1 df_movie = df_movie.rating.value_countsdf_movie = pd.DataFrame(df_movie).reset_index.rename(columns=’index’:’movie’) After finding out the value count of the rating rename the columns to tv_show.
To build a Bidirectional Bar Chart, one of the “type” must set the rating values on the negative side of the x-axis. Let’s create a new column called rating_final, where we multiply the initial rating values by (-1). This is the df_tv_show data. Once you have determined the rating value, rename each column to match the movie. This is the df_movie data. The preprocessing has been completed. Let’s make the Bidirectional Bar Chart Code: fig = make_subplots(rows=1, cols=2, specs=[[{}, {}]], shared_yaxes=True, horizontal_spacing=0) # bar plot for tv showsfig.append_trace(go.Bar(x=df_tv_show.rating, y=df_tv_show.tv_show, orientation=’h’, showlegend=True, text=df_tv_show.rating_final, name=’TV Show’, marker_color=’#221f1f’), 1, 1) # bar plot for moviesfig.append_trace(go.Bar(x=df_movie.rating, y=df_movie.movie, orientation=’h’, showlegend=True, text=df_movie.rating, name=’Movie’, marker_color=’#b20710′), 1, 2) Making 2 subplots of bar chart one bar chart having df_tv_show and another one with df_movie. Parameters: shared_yaxis= True: To make a bidirectional bar chart; the bar chart needs to share the y-axis horizontal_spacing=0: Space between both the bar charts should be 0.
This parameter will specify how much space you need between each bar chart. orientation: You can view the bar charts in two formats: one horizontally and one vertically. The text parameter displays the value that we see on the bar. Name: This will give the title of the legends (Movie or TV Show). marker_color:
The bars can be any color that you like. This blog provides more information about the parameters. To make the bar chart look more appealing, these are additional parameters. fig.update_xaxes(showgrid=False)fig.update_yaxes(showgrid=False, categoryorder=’total ascending’, ticksuffix=’ ‘, showline=False) fig.update_traces(hovertemplate=None) fig.update_layout(title=’Which has the highest rating TV shows or Movies? ‘, margin=dict(t=80, b=0, l=70, r=40), hovermode=”y unified”, xaxis_title=’ ‘, yaxis_title=” “, plot_bgcolor=’#333′, paper_bgcolor=’#333′, title_font=dict(size=25, color=’#8a8d93′, family=”Lato, sans-serif”), font=dict(color=’#8a8d93’), legend=dict(orientation=”h”, yanchor=”bottom”, y=1, xanchor=”center”, x=0.5), hoverlabel=dict(bgcolor=”black”, font_size=13, font_family=”Lato, sans-serif”)) fig.add_annotation(dict(x=0. 81, y=0.6, ax=0, ay=0, xref = “paper”, yref = “paper”, text= “97% people prefer Movies over TV Shows on Netflix.
A large number of people view
movies that are for adults. )) fig.add_annotation(dict(x=0.2, y=0.2, ax=0, ay=0, xref = “paper”, yref = “paper”, text= “3% people prefer TV Shows on Netflix.
There is no inappropriate content for ages 17 and under in TV Shows.” )) # bar plot for moviesfig.append_trace(go.Bar(x=df_movie.rating, y=df_movie.movie, orientation=’h’, showlegend=True, text=df_movie.rating, name=’Movie’, marker_color=’#b20710′), 1, 2)fig.update_xaxes(showgrid=False)fig.update_yaxes(showgrid=False, categoryorder=’total ascending’, ticksuffix=’ ‘, showline=False)fig.update_traces(hovertemplate=None)fig.update_layout(title=’Which has the highest rating TV shows or Movies? ‘, margin=dict(t=80, b=0, l=70, r=40), hovermode=”y unified”, xaxis_title=’ ‘, yaxis_title=” “, plot_bgcolor=’#333′, paper_bgcolor=’#333′, title_font=dict(size=25, color=’#8a8d93′, family=”Lato, sans-serif”), font=dict(color=’#8a8d93’), legend=dict(orientation=”h”, yanchor=”bottom”, y=1, xanchor=”center”, x=0.5), hoverlabel=dict(bgcolor=”black”, font_size=13, font_family=”Lato, sans-serif”))fig.add_annotation(dict(x=0. 81, y=0.6, ax=0, ay=0, xref = “paper”, yref = “paper”, text= “97% people prefer Movies over TV Shows on Netflix.
A large number of people view
movies that are for mature audiences. ))fig.add_annotation(dict(x=0.2, y=0.2, ax=0, ay=0, xref = “paper”, yref = “paper”, text= “3% people prefer TV Shows on Netflix.
There is no inappropriate content for ages 17 and under in TV Shows.” Steps to modify this chart: The appropriate color choices, such as red and black that are shown in the Netflix logo. While you can use any color, you should choose a contrast color if you are using charts such as the one shown above. The background color should be chosen.
The background color and the bar color must be different. In this example, the background color is darker than the bar for TV Shows. Both the background and bar can be distinguished easily.
The chart should have a proper Title. A common title for a chart is “Type vs Rating”. While this is fine, it is important to have a Title that tells you everything about the chart. We use the title “Which TV Show or Movie has the highest Rating on Netflix?” This automatically informs us that we will have a chart that compares TV Shows to Movies for Ratings. It also indicates that we can only access the Netflix data. It depends on what use case you are using. Take the chart above as an example.