- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
一个简单的 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()
请注意,“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()
请注意,“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()
附带说明一下,为了让您更轻松地编写代码,最好按照 import matplotlib.pyplot as plt
导入 matplotlib.pyplot
.
关于python - 为什么在进行多个情节时情节图例会丢失标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54791323/
我有这样一个数据框 from datetime import date import random start_date = date.today().replace(day=1, month=1).
我想使用 fiddle 图比较技术化的几个维度的分布(维度由 1 到 5 的指数衡量)。 6 个 fiddle 图中有 5 个看起来与您预期的一样,但其中一个具有未反射(reflect)在数据中的非常
我正在将向量列表绘制为一系列 fiddle 图。我会使用 pandas 数据框,但列表的长度不等。 这有效: python g = sns.violinplot (data=res, cut=0, i
以下代码为我提供了一个非常漂亮的 fiddle 图(以及其中的箱线图)。 import numpy as np import seaborn as sns import matplotlib.pypl
我有以下代码来显示堆叠条 handles = df.toPandas().set_index('x').T.plot(kind='bar', stacked=True, figsize=(11,11)
目前我在 2x2 子图中绘制了 3 个图。它看起来像这样: fig, axes = plt.subplots(nrows=2, ncols=2) df1.plot('type_of_plot', ax
以下代码为我提供了一个非常漂亮的 fiddle 图(以及其中的箱线图)。 import numpy as np import seaborn as sns import matplotlib.pypl
我正在使用带有以下代码的 ggplot 的 python 版本并附加了图像,但颜色不足以为所有类别着色。我想知道有什么方法可以获得具有更多颜色的调色板,但仍然保留较低数字颜色较浅而较高数字颜色较深的关
我正在查看示例,但一切都需要数据框。 如果我有以下数据框: x = ["G","F","E","D","C","B"] y = [3,14,45,47,34,15] df = pd.DataFrame
我有很多数据框正在绘制用于演示。这些都有不同的列,但都包含相同的附加列 foobar。目前,我正在使用绘制这些不同的数据框 df.plot(secondary_y='foobar') 不幸的是,由于这
我无法让 Bokeh 显示我的绘图。这是我的 Python 代码。 import pandas as pd from bokeh.plotting import figure, ColumnDataS
我使用 matplot 库在 python 中绘制绘图。我必须生成的数字非常大,所以轴上的刻度也是很大的数字并且占用大量空间。我试图将它们呈现为幂(例如,我想要 10^8 而不是勾选 10000000
我使用 matplot 库在 python 中绘制绘图。我必须生成的数字非常大,所以轴上的刻度也是很大的数字并且占用大量空间。我试图将它们呈现为幂(例如,我想要 10^8 而不是勾选 10000000
我只想制作一个地毯图,其中每个系列都被绘制为水平的刻度带,类似于所示的图 on this page .我也见过这叫做“光栅图”。地毯图看起来很相似,但看起来 geom_rug 只适用于边距。我可以在
我正在使用此处示例中的代码绘制一个图: http://bl.ocks.org/mbostock/3887118 我是 HTML、CSS 和 JS 的新手,但了解一些基础知识。我想知道如何将绘图置于浏览
有一个 df : import random import pandas as pd def get_row(): row = {"date" : random.choice(range(10
第一个加载的绘图在 X 轴上有太多刻度(参见 image01)。 如果我在 X 轴上使用缩放操作,绘图现在已加载完毕。 你能给我一些建议,我可以在哪里搜索,因为 Plot 构造函数参数看起来不错。 d
我正在尝试为我的情节添加标题,但收到错误消息: "AttributeError: 'Figure' object has no attribute 'update_layout'" 该情节在没有 up
我想在 python 中使用 networkx 绘制有向网络图。当使用不同于 1 的 alpha 值时,边缘的起点也在节点内绘制;但是箭头很好。 如何让边远离我的节点? 我没有在文档中找到任何关于它的
出于视觉效果的目的,我希望我可以删除圆圈外的网格,只保留圆圈内的网格。 顺便说一句,如何用红色填充单元格 ([8,9],[9,10]),我的意思是,x=8 右侧和 y=9 下方的单元格。 我的代码在下
我是一名优秀的程序员,十分优秀!