0

I'm working on creating a bar chart for a skewed data set using python matplotlib. While I'm able to generate the graph without any issue, In the graph generated, the bar related to the skewed data is covering the majority of the bar chart and making the other nonskewed data look relatively small and negligible.

Below is the code used to generate the bar graph.

import numpy as np
import matplotlib.pyplot as plt
x=["A","B","C","D","E","F"]
y=[25,11,46,895,68,5]
fig,ax = plt.subplots()
r1=plt.barh(y=x,
        width=y,
        height=0.8)

#ht = [x.get_width() for x in r1.get_children()]
r1y = np.asarray([x.get_y() for x in r1.get_children()])
r1h = np.asarray([x.get_height() for x in r1.get_children()])

for i in range(5):
    plt.text(y[i],r1y[i]+r1h[i]/2, '%s'% (y[i]), ha='left', va='center')

plt.xticks([0,10,100,1000])
plt.show()

enter image description here

The above code would create a bar chart with 0,10,100 and 1000 as xtick values and they are placed at a relative distance based on their value.

While this is valid and expected behvaior, one single skewed bar is impacting the entire bar chart.

So,is it possible to place these xtick values at equidistant so that the skewed data doesn't occupy the majority of the space in the final output?

In the expected output, values related 0-10-100 should occupy around 66.6% of the space and 100-1000 should occupy the rest of the 33.3% of the space.

Example: enter image description here

Krishna
  • 471
  • 2
  • 7

1 Answers1

1

Try to add plt.xscale('log'):

x=["A","B","C","D","E","F"]
y=[25,11,46,895,68,5]
fig,ax = plt.subplots()
r1=plt.barh(y=x,
        width=y,
        height=0.8)

r1y = np.asarray([x.get_y() for x in r1.get_children()])
r1h = np.asarray([x.get_height() for x in r1.get_children()])

for i in range(5):
    plt.text(y[i],r1y[i]+r1h[i]/2, '%s'% (y[i]), ha='left', va='center')

plt.xscale('log')

plt.show()

Output:

enter image description here

Quang Hoang
  • 146,074
  • 10
  • 56
  • 74