Skip to content Skip to sidebar Skip to footer

Python - Dual Y Axis Chart, Align Zero

I'm trying to create a horizontal bar chart, with dual x axes. The 2 axes are very different in scale, 1 set goes from something like -5 to 15 (positive and negative value), the o

Solution 1:

I followed the link from a question and eventually ended up at this answer : https://stackoverflow.com/a/10482477/5907969

The answer has a function to align the y-axes and I have modified the same to align x-axes as follows:

defalign_xaxis(ax1, v1, ax2, v2):
    """adjust ax2 xlimit so that v2 in ax2 is aligned to v1 in ax1"""
    x1, _ = ax1.transData.transform((v1, 0))
    x2, _ = ax2.transData.transform((v2, 0))
    inv = ax2.transData.inverted()
    dx, _ = inv.transform((0, 0)) - inv.transform((x1-x2, 0))
    minx, maxx = ax2.get_xlim()
    ax2.set_xlim(minx+dx, maxx+dx)

And then use it within the code as follows:

import pandas as pd
import matplotlib.pyplot as plt

d = {'col1':['Test 1','Test 2','Test 3','Test 4'],'col 2' [1.4,-3,1.3,5],'Col3':[900,750,878,920]}
df = pd.DataFrame(data=d)

fig = plt.figure()  # Create matplotlib figure

ax = fig.add_subplot(111)  # Create matplotlib axes
ax2 = ax.twiny()  # Create another axes that shares the same y-axis as ax.

width = 0.4

df['col 2'].plot(kind='barh', color='darkblue', ax=ax, width=width, position=1,fontsize =4, figsize=(3.0, 5.0))
df['Col3'].plot(kind='barh', color='orange', ax=ax2, width=width, position=0, fontsize =4, figsize=(3.0, 5.0))

ax.set_yticklabels(df.col1)
ax.set_xlabel('Positive and Neg',color='darkblue')
ax2.set_xlabel('Positive Only',color='orange')

align_xaxis(ax,0,ax2,0)
ax.invert_yaxis()
plt.show()

This will give you what you're looking for enter image description here

Post a Comment for "Python - Dual Y Axis Chart, Align Zero"