PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, June 27, 2022

[FIXED] How create bar chart with 2d data bars with different scale axis

 June 27, 2022     data-visualization, graph, matplotlib, python, python-3.x     No comments   

Issue

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

Solution

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



Answered By - Z Li
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing