TypeError-Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe'

330 Views Asked by At

I have plotted two plots on the same axis using twinx() but the output isn't correct. The plots are not aligned with respect to zero axis. I am sharing the plot below.

Days percentage change is float and total traded quantityis integer.

fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)
ax2 = ax1.twinx()

ax1.set_xlabel('Date')
ax2.set_ylabel('Total Traded Quantity', color='red')
ax2.plot(df['Total Traded Quantity'], 'r-')

ax1.plot(df['Day_Perc_Change'], 'b-')
ax1.set_ylabel('Day_Perc_Change', color='blue')


def align_yaxis(ax1, v1, ax2, v2):
    y1 = ax1.transData.transform((0, v1))
    y2 = ax2.transData.transform((0, v2))
    inv = ax2.transData.inverted()
    dy = inv.transform((0, 0)) - inv.transform((0, y1-y2))
    mini, maxi = ax2.get_ylim()
    ax2.set_ylim(mini+dy, maxi+dy)
align_yaxis(ax1, 0, ax2, 0)

I'm also getting a typeError-Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe'. Please help if you can fix it and align the axis for proper visualization.

enter image description here

0

There are 0 best solutions below