Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Plot the 2 graphs with same y scale

I need to draw 2 bar graphs with the same x axis. It was done as shown below. Buy their heights are not comparable, since the y axis have been drawn with le8 in the left hand side and le9 with the right hand side. Cannot I bring them to the same scale? for an example loth are into le8? Following is the code I used.

def extract_top_20_movie_details(dataframe):
    top_20_domestic_movies = dataframe.nlargest(10,'Domestic Sales (in $)')
    top_20_international_movies = dataframe.nlargest(10,'International Sales (in $)')
    
    plt.figure(figsize=(13,7))
    # who v/s fare barplot
    ax=sns.barplot(x = 'Title',
                y = 'Domestic Sales (in $)',
                data = top_20_domestic_movies)
    plt.xticks(rotation=75)
    
    width_scale = 0.45
    for bar in ax.containers[0]:
        bar.set_width(bar.get_width() * width_scale)

    ax2 = ax.twinx()
    sns.barplot(y = 'International Sales (in $)', x = 'Title', data=top_20_domestic_movies, alpha=0.7, hatch='xx')
    for bar in ax2.containers[0]:
        x = bar.get_x()
        w = bar.get_width()
        bar.set_x(x + w * (1- width_scale))
        bar.set_width(w * width_scale)

    plt.ticklabel_format(useOffset=False)

    # Show the plot
    plt.show()

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

  • ax.set_ylim() sets the y limits of ax
  • ax2.get_ylim() gets the current y limits of ax2

With this in mind, you can just write:

ax.set_ylim(ax2.get_ylim())

Thins will make the data in ax look way smaller since it’s a order of magnitude less.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading