为什么在进行多个绘图时绘图图例会丢失标记?

fre*_*cal 5 python matplotlib pandas statsmodels

一个简单的 pandas 图会产生预期的输出,图例上有一个圆形标记:

import io
import pandas
import matplotlib
import statsmodels
import matplotlib.pyplot
import statsmodels.tsa.api

cause = "Malignant neoplasms"
csv_data = """Year,CrudeRate
1999,197.0
2000,196.5
2001,194.3
2002,193.7
2003,192.0
2004,189.2
2005,189.3
2006,187.6
2007,186.9
2008,186.0
2009,185.0
2010,186.2
2011,185.1
2012,185.6
2013,185.0
2014,185.6
2015,185.4
2016,185.1
2017,183.9
"""

df = pandas.read_csv(io.StringIO(csv_data), index_col="Year", parse_dates=True)
df.plot(color="black", marker="o", legend=True)
matplotlib.pyplot.show()
Run Code Online (Sandbox Code Playgroud)

简单的熊猫情节

请注意,“CrudeRate”图例项是带有圆形标记的直线,这是正确的。

但是,如果我为 Holt 线性指数平滑函数添加一些附加图,图例就会丢失圆形标记:

import io
import pandas
import matplotlib
import statsmodels
import matplotlib.pyplot
import statsmodels.tsa.api

cause = "Malignant neoplasms"
csv_data = """Year,CrudeRate
1999,197.0
2000,196.5
2001,194.3
2002,193.7
2003,192.0
2004,189.2
2005,189.3
2006,187.6
2007,186.9
2008,186.0
2009,185.0
2010,186.2
2011,185.1
2012,185.6
2013,185.0
2014,185.6
2015,185.4
2016,185.1
2017,183.9
"""

def ets_non_seasonal(df, color, predict, exponential=False, damped=False, damping_slope=0.98):
  fit = statsmodels.tsa.api.Holt(df, exponential=exponential, damped=damped).fit(damping_slope=damping_slope if damped else None)
  fit.fittedvalues.plot(color=color, style="--")
  title = "ETS(A,{}{},N)".format("M" if exponential else "A", "_d" if damped else "")
  forecast = fit.forecast(predict).rename("${}$".format(title))
  forecast.plot(color=color, legend=True, style="--")

df = pandas.read_csv(io.StringIO(csv_data), index_col="Year", parse_dates=True)
df.plot(color="black", marker="o", legend=True)
ets_non_seasonal(df, "red", 5, exponential=False, damped=False, damping_slope=0.98)
matplotlib.pyplot.show()
Run Code Online (Sandbox Code Playgroud)

图例缺少标记

请注意,“CrudeRate”图例项只是一条没有圆形标记的直线。

是什么导致第二种情况下的图例失去了主要情节的圆形标记?

Ted*_*ddy 4

使用matplotlib.pyplot.legend()之前matplotlib.pyplot.show()可以解决您的问题。

由于您正在绘制 3 个图表,并且根据我的理解,您只需要图例中的 2 个标签,因此我们传递label='_nolegend_'fit.fittedvalues.plot(). 如果我们不这样做,图表图例中将出现第三个标签,其值为None

import io
import pandas
import matplotlib
import statsmodels
import matplotlib.pyplot
import statsmodels.tsa.api

cause = "Malignant neoplasms"
csv_data = """Year,CrudeRate
1999,197.0
2000,196.5
2001,194.3
2002,193.7
2003,192.0
2004,189.2
2005,189.3
2006,187.6
2007,186.9
2008,186.0
2009,185.0
2010,186.2
2011,185.1
2012,185.6
2013,185.0
2014,185.6
2015,185.4
2016,185.1
2017,183.9
"""

def ets_non_seasonal(df, color, predict, exponential=False, damped=False, damping_slope=0.98):
  fit = statsmodels.tsa.api.Holt(df, exponential=exponential, damped=damped).fit(damping_slope=damping_slope if damped else None)
  fit.fittedvalues.plot(color=color, style="--", label='_nolegend_')
  title = "ETS(A,{}{},N)".format("M" if exponential else "A", "_d" if damped else "")
  forecast = fit.forecast(predict).rename("${}$".format(title))
  forecast.plot(color=color, legend=True, style="--")

df = pandas.read_csv(io.StringIO(csv_data), index_col="Year", parse_dates=True)
df.plot(color="black", marker="o", legend=True)
ets_non_seasonal(df, "red", 5, exponential=False, damped=False, damping_slope=0.98)
matplotlib.pyplot.legend()
matplotlib.pyplot.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

附带说明一下,为了让您更轻松地编写代码,最好按matplotlib.pyplot如下方式导入import matplotlib.pyplot as plt