python - pandas.DataFrame.plot 创建具有双轴和不均匀索引的不正确图
问题描述
请考虑以下脚本生成的两个图。线图的位置不正确fig2
。
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.random.seed(0x5eed)
arr1 = np.random.randint(5, 10, 4)
arr2 = np.random.randint(15, 20, 4)
idx = [1, 3, 4, 5]
kwargs = {'marker': 'o', 'color': 'C1'}
fig1, ax11 = plt.subplots()
ax12 = ax11.twinx()
ax11.bar(idx, arr1)
ax12.plot(idx, arr2, **kwargs)
fig1.savefig('fig1.png')
df = pd.DataFrame({'foo': arr1, 'bar': arr2}, index=idx)
fig2, ax21 = plt.subplots()
ax22 = ax21.twinx()
df['foo'].plot.bar(ax=ax21)
df['bar'].plot.line(ax=ax22, **kwargs)
fig2.savefig('fig2.png')
fig1
(预期的):
fig2
(意外):
经过一番调查,我发现以下条件可以重现此问题。
- 使用条形图和折线图(
fig2
如果两个图都是折线图,则正确) - 索引不均匀(
idx = range(4)
解决问题)
我发现了一个类似的问题,但对我来说不是这样。df.index
是一流的Int64Index
。这是预期的行为还是我应该向问题跟踪器报告的事情?
解决方案
推荐阅读
- javascript - 在 Javascript 中使用 RegEx 解析嵌套的 HTML 自定义标签
- android - 支持设计库异常
- sql - 上一年年初至今与部分当年年初至今计算 SQL Server
- php - 页面 url 重定向到媒体文件
- php - 如何计算数据库中的结果?
- java - 将 UTC 时间 HH:mm:ss 转换为设备时间 HH:mm:ss 并添加夏令时(如果有)
- javascript - JQuery $(function).animate 不是函数
- html - XSL 到 HTML:当内部为空时如何保留关闭的 div 标签?
- javascript - FullCalendar v4 动态设置日历的高度
- pandas - 如何将仅包含数字的数据框中的列拆分为熊猫中的多列