gpt4 book ai didi

python - 为什么在进行多个情节时情节图例会丢失标记?

转载 作者:太空宇宙 更新时间:2023-11-03 10:49:06 25 4
gpt4 key购买 nike

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

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()

Simple pandas plot

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

但是,如果我为 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()

Legend missing marker

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

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

最佳答案

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

由于您正在绘制 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()

enter image description here

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

关于python - 为什么在进行多个情节时情节图例会丢失标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54791323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com