pandas Matplotlib 的 fill_between 不适用于 plot_date,还有其他选择吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28091290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:51:54  来源:igfitidea点击:

Matplotlib's fill_between doesnt work with plot_date, any alternatives?

pythonmatplotlibpandas

提问by fccoelho

I want to create a plot just like this: This has integer x values

我想像这样创建一个情节: 这具有整数 x 值

The code:

编码:

P.fill_between(DF.start.index, DF.lwr, DF.upr, facecolor='blue',   alpha=.2)
P.plot(DF.start.index, DF.Rt, '.')

but with dates in the x axis, like this (without bands): with plot_date

但是在 x 轴上有日期,就像这样(没有带): 与 plot_date

the code:

编码:

P.plot_date(DF.start, DF.Rt, '.')

the problem is that fill_between fails when x values are date_time objects.

问题是当 x 值是 date_time 对象时 fill_between 失败。

Does anyone know of a workaround? DF is a pandas DataFrame.

有谁知道解决方法?DF 是一个Pandas数据帧。

回答by unutbu

It would help if you show how dfis defined. What does df.info()report? This will show us the dtypes of the columns.

如果您展示如何df定义会有所帮助。什么df.info()报告?这将向我们展示列的 dtypes。

There are many ways that dates can be represented: as strings, ints, floats, datetime.datetime, NumPy datetime64s, Pandas Timestamps, or Pandas DatetimeIndex. The correct way to plot it depends on what you have.

日期可以通过多种方式表示:字符串、整数、浮点数、datetime.datetime、NumPy datetime64s、Pandas Timestamps 或 Pandas DatetimeIndex。绘制它的正确方法取决于您拥有什么。

Here is an example showing your code works if df.indexis a DatetimeIndex:

这是一个示例,如果df.index是 DatetimeIndex,则显示您的代码有效:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats

index = pd.date_range(start='2000-1-1', end='2015-1-1', freq='M')
N = len(index)
poisson = (stats.poisson.rvs(1000, size=(N,3))/100.0)
poisson.sort(axis=1)
df = pd.DataFrame(poisson, columns=['lwr', 'Rt', 'upr'], index=index)

plt.fill_between(df.index, df.lwr, df.upr, facecolor='blue', alpha=.2)
plt.plot(df.index, df.Rt, '.')
plt.show()

enter image description here

在此处输入图片说明



If the index has string representations of dates, then (with Matplotlib version 1.4.2) you would get a TypeError:

如果索引有日期的字符串表示,那么(使用 Matplotlib 1.4.2 版)你会得到一个 TypeError:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats

index = pd.date_range(start='2000-1-1', end='2015-1-1', freq='M')
N = len(index)
poisson = (stats.poisson.rvs(1000, size=(N,3))/100.0)
poisson.sort(axis=1)
df = pd.DataFrame(poisson, columns=['lwr', 'Rt', 'upr'])

index = [item.strftime('%Y-%m-%d') for item in index]
plt.fill_between(index, df.lwr, df.upr, facecolor='blue', alpha=.2)
plt.plot(index, df.Rt, '.')
plt.show()

yields

产量

  File "/home/unutbu/.virtualenvs/dev/local/lib/python2.7/site-packages/numpy/ma/core.py", line 2237, in masked_invalid
    condition = ~(np.isfinite(a))
TypeError: Not implemented for this type

In this case, the fix is to convert the strings to Timestamps:

在这种情况下,修复方法是将字符串转换为时间戳:

index = pd.to_datetime(index)

回答by Shital Shah

I got similar error while using fill_between:

我在使用 fill_between 时遇到了类似的错误:

ufunc 'bitwise_and' not supported

However, in my case the cause of error was rather stupid. I was passing color parameter but without explicit argument name which caused it to be #4 parameter called where. So simply making sure keyword parameters has key solved the issue:

但是,就我而言,错误的原因相当愚蠢。我正在传递颜色参数,但没有明确的参数名称,这导致它被称为 #4 参数where。所以简单地确保关键字参数已经解决了这个问题:

ax.fill_between(xdata, highs, lows, color=color, alpha=0.2)

回答by H0R5E

Regarding the error reported by chilliq:

关于chilliq报的错误:

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs 
  could not be safely coerced to any supported types according to the casting 
  rule ''safe''

This can be produced if the DataFrame columns have "object" dtype when using fill_between. Changing the example column types and then trying to plot, as follows, results in the error above:

如果在使用 fill_between 时 DataFrame 列具有“对象”dtype,则可以产生这种情况。更改示例列类型,然后尝试绘制,如下所示,导致上述错误:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats

index = pd.date_range(start='2000-1-1', end='2015-1-1', freq='M')
N = len(index)
poisson = (stats.poisson.rvs(1000, size=(N,3))/100.0)
poisson.sort(axis=1)
df = pd.DataFrame(poisson, columns=['lwr', 'Rt', 'upr'], index=index)
dfo = df.astype(object)

plt.fill_between(df0.index, df0.lwr, df0.upr, facecolor='blue', alpha=.2)
plt.show()

From dfo.info() we see that the column types are "object":

从 dfo.info() 我们看到列类型是“对象”:

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 180 entries, 2000-01-31 to 2014-12-31
Freq: M
Data columns (total 3 columns):
lwr    180 non-null object
Rt     180 non-null object
upr    180 non-null object
dtypes: object(3)
memory usage: 5.6+ KB

Ensuring that the DataFrame has numerical columns will solve the problem. To do this we can use pandas.to_numeric to convert, as follows:

确保 DataFrame 具有数字列将解决问题。为此,我们可以使用 pandas.to_numeric 进行转换,如下所示:

dfn = dfo.apply(pd.to_numeric, errors='ignore')

plt.fill_between(dfn.index, dfn.lwr, dfn.upr, facecolor='blue', alpha=.2)
plt.show()