0

I am trying to plot a bar graph using a dataframe, and I used the below code:

def add_line(ax, xpos, ypos):
    line = plt.Line2D([xpos, xpos], [ypos + .1, ypos],
                  transform=ax.transAxes, color='gray')
    line.set_clip_on(False)
    ax.add_line(line)

def label_len(my_index,level):
    labels = my_index.get_level_values(level)
    return [(k, sum(1 for i in g)) for k,g in groupby(labels)]

def label_group_bar_table(ax, df):
    ypos = -.1
    scale = 1./df.index.size
    for level in range(df.index.nlevels)[::-1]:
        pos = 0
        for label, rpos in label_len(df.index,level):
            lxpos = (pos + .5 * rpos)*scale
            ax.text(lxpos, ypos, label, ha='center', transform=ax.transAxes)
            add_line(ax, pos*scale, ypos)
            pos += rpos
        add_line(ax, pos*scale , ypos)
        ypos -= .1

from matplotlib.pyplot import figure

ax = my_df.plot(kind='bar')
ax.set_xticklabels('State')
ax.set_xlabel('Electricity consumed by every resource')
ax.plot([1,2,3])
#plt.xticks(rotation=90)

label_group_bar_table(ax, my_df)

My question is: How do I change the size of the plot and how can I make sure that the ticks are displayed vertically on the x-axis and ensure that the title of the x-axis and the ticks on the x-axis don't overlap? While using 'figure', I know that the 'rotation' parameter can be changed to 90 to ensure that x ticks are vertical. I also understand that the 'figsize' can be used to set the size while using figure. But I am not sure how we should work with 'ax'. Why are my y-axis ticks in decimal and what is that 'le11'? My data contains numbers that are 7 digit or 8 digits. Is there a way to ensure the y-axis also contains 7 or 8 digit numbers instead? My graph looks like: enter image description here

DeepMeh
  • 23
  • 3
  • For the scientfic notation problem, see here: https://stackoverflow.com/a/28373421/8881141 – Mr. T Dec 04 '20 at 10:40
  • As for figsize: `fig, my_ax = plt.subplots(figsize=(20,15)`, then `my_df.plot(kind='bar', ax=my_ax)`. – Mr. T Dec 04 '20 at 10:42
  • Thank you, this works!. But the scientific notation issue couldn't be sorted even after using style = 'plain'. Any other suggestions? – DeepMeh Dec 04 '20 at 11:23

0 Answers0