Skip to content
Advertisement

Pandas – How to modify the width of the hist plots in a plot bar?

I wanted to know how to modify the width of the lines in a plot bar?

posts_created = {'Jan.': {2011: 0, 2012: 92, 2013: 129},
 'Feb.': {2011: 0, 2012: 72, 2013: 99},
 'Mar.': {2011: 0, 2012: 62, 2013: 107},
 'Apr.': {2011: 0, 2012: 88, 2013: 92},
 'May': {2011: 0, 2012: 93, 2013: 130},
 'Jun.': {2011: 0, 2012: 105, 2013: 120},
 'Jul.': {2011: 0, 2012: 105, 2013: 134},
 'Aug.': {2011: 0, 2012: 81, 2013: 143},
 'Sep.': {2011: 0, 2012: 60, 2013: 64},
 'Oct.': {2011: 1, 2012: 75, 2013: 0},
 'Nov.': {2011: 15, 2012: 77, 2013: 0},
 'Dec.': {2011: 140, 2012: 155, 2013: 0}}

pd.DataFrame(posts_created).plot.bar(width=0.7, color={'Jan.': "red", 'Feb.': "blue", 'Mar.': "brown", 'Apr.': "gray",
                                                       'May': "purple", 'Jun.': "olive",
                                                       'Jul.': "green", 'Aug.': "yellow", 'Sep.': "navy",
                                                       'Oct.': "Orange", 'Nov.': "pink", 'Dec.': "teal"},
                                                 figsize=(10,10),
                                                 title = "Number of Posts for Each Year and Month")
plt.show()

This is my bar right now:

enter image description here

Advertisement

Answer

  • you can dim the color using alpha
  • define border with edgecolor
d = pd.date_range("30-Apr-2012","1-jan-2015", freq="M")
df = pd.DataFrame({"Date":d,"posts_created":np.random.randint(70,120, len(d))})

df = df.set_index(pd.MultiIndex.from_arrays([df.Date.dt.year, df.Date.dt.month], names=["Year","Month"])).drop(columns="Date")
color = {1: "red", 2: "blue", 3: "brown", 4: "gray", 5: "purple", 6: "olive", 
 7: "green", 8: "yellow", 9: "navy", 10: "Orange", 11: "pink", 12: "teal"}
fig, ax = plt.subplots(1, figsize=[14,5])
dfp = df.unstack("Month").droplevel(0, axis=1)
dfp.plot.bar(color=color, ax=ax, alpha=0.5, edgecolor="black")

enter image description here

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement