Home > Software engineering >  How create bar chart with 2d data bars with different scale axis
How create bar chart with 2d data bars with different scale axis

Time:05-10

I would like to create graph like this:

enter image description here

Where only first half of the graph is in log scale. I tried to use two scale y axis, but I was not able to shift new axis to start from 0. It is possible shift new axis ? Or is there some other way, how to obtain this graph ?

# dummy data
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
y = [10000, 200000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000, 100001, 12000]
y2 = [1, 2, 45, 4, 5, 6, 2, 8, 9, 10, 11, 1]

fig, ax1 = plt.subplots(figsize=(5, 5), dpi=80)
ax2 = ax1.twinx()

# plot data
ax1.bar(x, y, color=color, log=True)
ax2.bar(x, y2,color=color)
ax2.invert_yaxis()

CodePudding user response:

You could plot it by combining two plots, which seems to be what the provided output is doing:

# dummy data
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
y = [10000, 200000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000, 100001, 12000]
y2 = [1, 2, 45, 4, 5, 6, 2, 8, 9, 10, 11, 1]

fig = plt.figure()
gs = fig.add_gridspec(2, hspace=0)
axs = gs.subplots(sharex=True, sharey=False)
fig.suptitle('Your title')
axs[0].bar(x, y, log=True)
axs[1].bar(x, y2)
axs[1].invert_yaxis()

enter image description here

  • Related