matplotlib - TypeError - 无法根据规则“安全”将数组数据从 dtype('O') 转换为 dtype('float64')
问题描述
我在同一轴上绘制了两个图,twinx()
但输出不正确。这些图未相对于零轴对齐。我正在分享下面的情节。
Days percentage change
是float
和total traded quantity
是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)
我也得到一个typeError-Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe'
. 如果您可以修复它并对齐轴以进行正确的可视化,请提供帮助。
解决方案
推荐阅读
- testng - 如何在 Allure 报告中添加@Link 测试用例失败
- linux - 如何窃取在 linux shell 上运行的任何命令的输出?
- python - 如何为 python 子文件夹中的所有图像运行 mogrify 脚本?
- c++ - 你如何为 clang 和 gcc 编写一个 makefile?
- python - 如何使用 Python 从 Excel 复制单元格值而不是公式
- html - 从spring mvc以纯html传递参数
- node.js - transparent image background change to black with Jimp
- java - 使用 ContributesAndroidInjector 提供 Activity
- python - 绘制具有多个类的 ROC 曲线
- javafx - 使用 javafx 应用系统设置