我正在绘制一个看起来像这样的数据框。
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 年的线条比其他线条粗得多,以便更清晰地突出。有没有办法使用这一行代码来做到这一点?或者我是否需要手动绘制所有年份,以便我可以分别控制每条线的粗细?附上一张当前图片,线条粗细都一样。
最佳答案
您可以遍历图中的线条,可以使用 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()
关于python - 更改线图中特定线的线宽 pandas/matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62470743/