helpless
helpless

Reputation: 99

How to set the size of a bar graph in matplotlib

I have managed to draw clustered bar graphs with matplotlib but the problem I am facing is that some figures are just too small compared to others and when drawn by matplotlib, they look non-existent. Is it possible to perhaps change the y-axis or are there any methods to make the smaller bar graphs look more visible?

This is my code for visualization.

# Setting the positions and width for the bars
pos = list(range(len(df3['Air']))) 
width = 0.25 

# Plotting the bars
fig, ax = plt.subplots(figsize=(10,5))

# Create a bar with pre_score data,
# in position pos,
plt.bar(pos, 
        #using df['pre_score'] data,
        df3['Air'], 
        # of width
        width, 
        # with alpha 0.5
        alpha=0.5, 
        # with color
        color='#EE3224', 
        # with label the first value in first_name
        label=df3['Region'][0]) 

# Create a bar with mid_score data,
# in position pos + some width buffer,
plt.bar([p + width for p in pos], 
        #using df['mid_score'] data,
        df3['Land'],
        # of width
        width, 
        # with alpha 0.5
        alpha=0.5, 
        # with color
        color='#F78F1E', 
        # with label the second value in first_name
        label=df3['Region'][1]) 

# Create a bar with post_score data,
# in position pos + some width buffer,
plt.bar([p + width*2 for p in pos], 
        #using df['post_score'] data,
        df3['Sea'], 
        # of width
        width, 
        # with alpha 0.5
        alpha=0.5, 
        # with color
        color='#FFC222', 
        # with label the third value in first_name
        label=df3['Region'][2]) 

# Set the y axis label
ax.set_ylabel('Arrival count')

# Set the chart's title
ax.set_title('Total arrival count by mode of transport by region in 2014')

# Set the position of the x ticks
ax.set_xticks([p + 1.5 * width for p in pos])

# Set the labels for the x ticks
ax.set_xticklabels(df3['Region'])

# Setting the x-axis and y-axis limits
plt.xlim(min(pos)-width, max(pos)+width*4)
plt.ylim([0, max(df3['Air'] + df3['Land'] + df3['Sea'])] )

# Adding the legend and showing the plot
plt.legend(['Air', 'Land', 'Sea'], loc='upper left')
plt.grid()
plt.show()

This is the graph that I have. As you can observe, most graphs cannot be seen.

This is the graph that I have. As you can observe, most graphs cannot be seen.

Upvotes: 1

Views: 2963

Answers (1)

snapcrack
snapcrack

Reputation: 1811

Try using a log scale:

plt.yscale('log')

plt.bar([p + width*2 for p in pos],
    ....) 

Upvotes: 1

Related Questions