Home > Net >  Horizontal bar chart visualize one column
Horizontal bar chart visualize one column

Time:10-17

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()

bar plot with second x axis

CodePudding user response:

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()
  • Related