Reputation: 13
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 :
Upvotes: 1
Views: 540
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
Reputation: 10545
ax.twiny()
, see below.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()
Upvotes: 1