python - 更改线图中特定线的线宽 pandas/matplotlib

标签 python pandas dataframe matplotlib

我正在绘制一个看起来像这样的数据框。

Date    2011    2012    2013    2014    2015    2016    2017    2018    2019    2020
Date                                        
01 Jan  12.896  13.353  12.959  13.011  13.073  12.721  12.643  12.484  12.876  13.102
02 Jan  12.915  13.421  12.961  13.103  13.125  12.806  12.644  12.600  12.956  13.075
03 Jan  12.926  13.379  13.012  13.116  13.112  12.790  12.713  12.634  12.959  13.176
04 Jan  13.051  13.414  13.045  13.219  13.051  12.829  12.954  12.724  13.047  13.187
05 Jan  13.176  13.417  13.065  13.148  13.115  12.874  12.956  12.834  13.098  13.123

绘图代码在这里。
ice_data_dates.plot(figsize=(20,12), title='Arctic Sea Ice Extent', lw=3, fontsize=16, ax=ax, grid=True)

这为数据框中列出的每一年绘制了一年中每一天的线图。但是,我想让 2020 年的线条比其他线条粗得多,以便更清晰地突出。有没有办法使用这一行代码来做到这一点?或者我是否需要手动绘制所有年份,以便我可以分别控制每条线的粗细?附上一张当前图片,线条粗细都一样。

enter image description here

最佳答案

您可以遍历图中的线条,可以使用 ax.get_lines 检索这些线条。 ,并使用 set_linewidth 增加宽度如果它的 label匹配感兴趣的值:

fig, ax = plt.subplots()
df.plot(figsize=(20,12), title='Arctic Sea Ice Extent', 
        lw=3, fontsize=16, ax=ax, grid=True)

for line in ax.get_lines():
    if line.get_label() == '2020':
        line.set_linewidth(15)
plt.show()

enter image description here

关于python - 更改线图中特定线的线宽 pandas/matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62470743/

相关文章:

python - pywhatkit.sendwhatmsg_to_group 不工作

python - fillna() 使用第三个多个 pandas 列

python - (Py)SDL2 : Drawing a textured polygon

python - 创建列的内存高效方法,该列指示来自一组列的值的唯一组合

R 在动物园对象和数据框之间进行转换,不同列数的结果不一致?

python - 将列表序列化为 JSON

python - Pandas 提高效率

python - 用列表中的值替换 pandas.DataFrame 的 NaN 值

r - 行值增量增加,直到发现相邻列发生变化

python - 有条件地查找 DataFrame 中给定字段 1 的字段 2 中出现的百分比