Lenda Marwan
Lenda Marwan

Reputation: 13

Horizontal bar chart visualize one column

I am trying to draw a horizontal bar chart, but the second data columns does not appear at all!

df = pd.DataFrame(dict(graph=["Indian","Egyptian","Emirati","Philippino","Syrian","Pakistani","Jordanian",
          "British","Lebanese","American","Chinese","Ukrainian","Canadian","Russian",
          "French","Saudi","Sudanese","Moroccan","Nigerian","Palestinian","German","Iranian",
          "Italian","Turkish","Tunisian"],
                           n=[33060621,28185470,25327222,14033365,13895316,13335555,13067230,9065737,8871022,3914119,3875486,
         3505868,3421358,3407623,3067701,2940321,2824929,2620922,2510789,2439167,1874219,1800495,1852571,1415189,1657462], 
                           m=[13504,13762,9671,4568,4995,8016,6808,5559,5456,4137,2802,1011,2386,1806,4408,2002,1141,1530,916,992,1933,1651,
          1628,1469,839])) 

ind = np.arange(len(df))
width = 0.4

fig, ax = plt.subplots(figsize=(15,20))
ax.barh(ind, df.n, width, color='red', label='N')
ax.barh(ind + width, df.m, width, color='green', label='M')

ax.set(yticks=ind + width, yticklabels=df.graph, ylim=[2*width - 1, len(df)])
ax.legend()

plt.show()

https://drive.google.com/file/d/12TB_pArFWLbCI_ZhePkEfVMpnWxTj885/view?usp=sharing

I would like some help :

  1. Why does only one data column is appear and the other is hidden ?
  2. Can I have different range for each column? For example, put upper x-axis for the fist column and the lower x-axis is for the second column?
  3. Why are the country's names are shifted?

Upvotes: 1

Views: 540

Answers (2)

tzinie
tzinie

Reputation: 772

import plotly.graph_objects as go
from plotly.subplots import make_subplots

fig=make_subplots()   

fig.update_layout(xaxis2={ 'overlaying': 'x', 'side': 'top'})

fig.add_trace(
    go.Bar(name='M', y=df['graph'], x=df.m, orientation='h'))
fig.add_trace(
    go.Bar(name='N', y=df['graph'], x=df.n, orientation='h'))
fig.data[1].update(xaxis='x2')
fig.update_layout(width=700, height=475)

fig.show()

Upvotes: 1

Arne
Arne

Reputation: 10545

  1. The other data column is not visible in the plot, because its values are so much smaller. To change that, you could scale them up for the plot, e.g. by a factor of 1000.
  2. You can add a second x-axis to the plot with ax.twiny(), see below.
  3. Where the y labels appear relative to the bars depends on the width values you add to the index. The code below shows a way to centralize them for each pair of bars.
ind = np.arange(len(df))
width = 0.4
scale = 1000

xticks = [0, 10_000_000, 20_000_000, 30_000_000]
limits = [0, 35_000_000]

fig, ax = plt.subplots(figsize=(15,20))

ax.barh(ind + 0.5 * width, df.n, width, color='red', label='N')
ax.barh(ind + 1.5 * width, scale * df.m, width, color='green', label='M')
ax.xaxis.get_major_formatter().set_useOffset(False)
ax.set(yticks=ind + width, yticklabels=df.graph, ylim=[2*width - 1, len(df)],
       xticks=xticks, xticklabels=['0', '10 M', '20 M', '30 M'], xlim=limits)

ax2 = ax.twiny()
ax2.xaxis.get_major_formatter().set_useOffset(False)
ax2.set(xticks=xticks, xticklabels=['0', '10 K', '20 K', '30 K'], xlim=limits)

ax.legend()
plt.show()

bar plot with second x axis

Upvotes: 1

Related Questions