[英]Adding a circle on specific date in a matplotlib plot
I have a matpltolib plot made using this code:我有一个使用此代码制作的 matpltolib 图:
ax.plot(df_c.index, y1, color='b')
Here df_c.index
is:这里df_c.index
是:
DatetimeIndex(['2019-10-31', '2019-11-01', '2019-11-02', '2019-11-03',
'2019-11-04', '2019-11-05', '2019-11-06', '2019-11-07',
'2019-11-08', '2019-11-09',
...
'2020-04-04', '2020-04-05', '2020-04-06', '2020-04-07',
'2020-04-08', '2020-04-09', '2020-04-10', '2020-04-11',
'2020-04-12', '2020-04-13'],
dtype='datetime64[ns]', length=166, freq=None)
The above code makes a lineplot.上面的代码制作了一个线图。
I want to add a circle on this date '2020-04-12'
with a value of 100
.我想在这个日期'2020-04-12'
添加一个值为100
的圆圈。 How do I do that?我怎么做? I tried:我试过:
ax.plot(datetime.date(2020, 04, 12), 100, 'bo')
but it does not work.但它不起作用。 How can I fix it?我该如何解决?
I'm not entirely certain where you want to draw your circle, but I present here three different circle positions, and a simpler fourth alternative just for highlighting a specific date.我不完全确定你想在哪里画你的圆圈,但我在这里展示了三个不同的圆圈位置,还有一个更简单的第四个选择,只是为了突出一个特定的日期。 A demo image is shown at the bottom.演示图像显示在底部。 First, let's just plot some data:首先,让我们绘制一些数据:
import matplotlib.pyplot as plt
dates = ['2019-10-31', '2019-11-01', '2019-11-02', '2019-11-03']
y = [i*i for i in range(len(dates))] # some random y values
# An arbitrary date value to encircle
encircled_date = dates[1]
val_of_encircled_date = y[1]
# Plot the graph
fig, ax = plt.subplots()
ax.plot_date(dates,y,'-')
bottom, top = plt.ylim() # Later, we'll need the min value of the y-axis for correct positioning of circle
Now, if you just want a circle at the graph, as it passes through your specific date, the simplest and (imho) best approach is to simply replot that specific value, but with markerstyle='o'
.现在,如果您只想在图表上markerstyle='o'
一个圆圈,当它通过您的特定日期时,最简单且(恕我直言)最好的方法是简单地重新markerstyle='o'
该特定值,但使用markerstyle='o'
。 Adjust marker size, line width and color to your preferences:根据您的喜好调整标记大小、线宽和颜色:
# Plot a circle around specific graph value
ax.plot_date(encircled_date, val_of_encircled_date,
'og', # marker style 'o', color 'g'
fillstyle='none', # circle is not filled (with color)
ms=10.0) # size of marker/circle
Then, if you instead wanted a circle around the date-tick, on the x-axis for your specific date, it is a little more tricky depending on what details you need to encircle.然后,如果您想要一个围绕日期刻度的圆圈,在您的特定日期的 x 轴上,根据您需要圈出的细节,这有点棘手。 Of course, you could use the approach above to get a small circle around the tick only, but I'll show a more advanced approach based on another SO-question :当然,您可以使用上面的方法仅在刻度周围获得一个小圆圈,但我将展示基于另一个SO 问题的更高级方法:
# Plot a circle around the 'tick' of specific date on the x-axis
circle1 = plt.Circle((encircled_date, bottom), # position
1.0 / len(dates), # radius
color='r',
clip_on=False, # allow drawing outside of axes
fill=False)
ax.add_artist(circle1)
The above solution only encircles the tick, and not the date label itself.上述解决方案仅环绕刻度,而不是日期标签本身。 We may micro adjust the circle to fit the date-label inside, by tuning two offset parameters,我们可以微调圆圈以适应里面的日期标签,通过调整两个偏移参数,
# Plot a circle around the specific date's label on the x-axis
pos_offset = 0.5
len_offset = 0.4
circle2 = plt.Circle((encircled_date, bottom-pos_offset), # position
(1.0+len_offset) / len(dates), # radius
color='purple',
clip_on=False, # allow drawing outside of axis
fill=False)
ax.add_artist(circle2)
However, this tuning may be a tedious task.然而,这种调整可能是一项乏味的任务。 If your objective is only to emphasize this particular date, it may be better to simply reconfigure the x-label.如果您的目标只是强调这个特定日期,那么简单地重新配置 x 标签可能会更好。 You may for instance change the color of the label like this,例如,您可以像这样更改标签的颜色,
ax.get_xticklabels()[2].set_color("red")
ax.get_xticklabels()[2].set_weight("bold")
The four different approaches are shown in the image below.四种不同的方法如下图所示。 I hope this helps.我希望这有帮助。
One final remark: When you get a densely populated x-axis of dates, it might be worthwhile looking into more advanced formatting of date-labels which you can read all about in the official documentation .最后一句话:当您得到一个密集的日期 x 轴时,可能值得研究更高级的日期标签格式,您可以在官方文档中阅读所有相关内容。 For instance, they show how to neatly rotate the labels to fit them closer together without overlapping.例如,他们展示了如何巧妙地旋转标签以使它们更靠近而不重叠。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.