DataViz Guide Day 8 - Mastering Bar Charts with Python Plotly

DataViz Guide Day 8 - Mastering Bar Charts with Python Plotly

Hey, data enthusiasts!


Ever felt like your bar charts could use a little boost? Enter Plotly Python – the tool that makes turning basic charts into stunning visuals a breeze! No need to be a coding expert; we're here to keep it straightforward and fun! ??


What's Plotly Python All About? ??

Simply put, rather than the interactivity in Plotly charts, Plotly Python is like a magical paintbrush for your charts. It takes them from ordinary to extraordinary without the fuss.


What to Expect in This Post? ??

We're breaking down the process of making your bar charts look awesome. Forget the tech talk; we're keeping it simple and enjoyable! ??


Ready to Transform Your Charts? Let's Go!


Step 1: Setting the Stage

  • We begin by importing the necessary libraries, including Plotly Express (px) for simplified chart creation.
  • We created a sample dataset (df) to work with.
  • The initial bar chart (fig) is created using px.bar, specifying the y-axis and the x-axis.

import plotly.express as px
import plotly.graph_objects as go
import pandas as pd

# Sample Data
x = ['A', 'B', 'C', 'D', 'E']
y = [10, 20, 30, 40, 50]
df = pd.DataFrame({"x": x, "y": y})

# Initial Bar Chart
fig = px.bar(df, y= y, x = x)
        

The initial bar plot looks like the following:


Step 2: Basic Layout Adjustments

  • We updated the layout of our chart to enhance its visual appeal.
  • yaxis={'categoryorder': 'total ascending'} ensures categories are ordered for a more logical presentation.
  • showlegend=False removes the legend for a cleaner look.
  • Title adjustments included the title text as "Simple Bar Chart with Plotly", the title font size as 18px, the title color as black, and the title x position in the middle of the chart using title_x=0.5.
  • General Properties of the chart text were customized using the font parameter, as we set the text size as 14px, and the color as black.

fig.update_layout(
    yaxis={'categoryorder': 'total ascending'},
    showlegend=False,
    title = {"text": "Simple Bar Chart with Plotly","font":{"size":18,  "color": "black"}},
    title_x=0.5,
    font={"size":14, "color": "black"}
    
)        

Step 3: Additional Customization for Visual Impact

  • We enhanced the individual traces (bars) by placing text inside (textposition='inside').
  • texttemplate formats the displayed text, to include the category name for each bar plus the value for each bar inside parenthesis, providing a cleaner look.
  • marker_color='#8db1eb' defines a pleasing color for our bars.
  • Additional layout adjustments include hiding the tick labels for both the x and the y axes, and hiding the x-axis line for a polished finish.

fig.update_traces(
    textposition='inside',
    texttemplate='%{y}<br>(%{text})',
    marker_color='#8db1eb'
)


fig.update_yaxes(showticklabels=False, title_text="", nticks=len(df) + 1)
fig.update_xaxes(showticklabels=False, showline=False, tickvals=[-1], title="")
        


Final result

After performing all those customizations, the final bar chart looks way better than the first one, as you can see below:

?? Question for You:

What's your favorite technique for enhancing bar charts? Share your insights in the comments! ???


?? Join the DataViz Journey!

Stay tuned for more insights, tips, and hands-on guides. Whether you're a seasoned pro or just starting, our DataViz adventure is for everyone! ???




要查看或添加评论,请登录

Mouhssine AKKOUH的更多文章

社区洞察

其他会员也浏览了