UserWarning: marker is redundantly defined by the 'marker' keyword argument when using plot_date function

Viewed 2173

I am using

plt.plot_date([dates[index], dates[index]+timeD], [pivots[index],pivots[index]], linestyle = "-", linewidth=2, marker = ",", )

to draw some lines on a chart. This used to work, but now after doing some upgrades, a warning is displayed:

UserWarning: marker is redundantly defined by the 'marker' keyword argument and the fmt string "o" (-> marker='o'). The keyword argument will take precedence.
plt.plot_date([dates[index], dates[index]+timeD], [pivots[index],pivots[index]], linestyle = "-", linewidth=2, marker = ",", )

If I just omit marker = "," , the warning disappears, but there are two dots at the beginning and end of the line which I don't like.

What could I do to get rid of the warning without changing the plot?

1 Answers

plt.plot_date takes the keyword argument fmt, which is the matplotlib's shorthand for defining the marker and linestyle.

If you do not provide that fmt kwarg, a default value is used: "o"

One option option would be to use fmt to define both your marker and linestyle, instead of using the marker and linestyle kwargs:

plt.plot_date([dates[index], dates[index]+timeD], [pivots[index],pivots[index]],
              fmt=",-", linewidth=2)

For more info on fmt, see the Notes at the bottom the docs for plt.plot; note that it appears that fmt is used slightly differently for plot_date and plot, in that it is always defined for plot_date, while it is optional for plot.


As noted by @TomJohnson in comments, if you want a custom linestyle you can't use the fmt kwarg. One option here is to use fmt to define only the colour, and then use the linestyle and marker kwargs to define the rest of your desired format:

For example, instead of:

plt.plot_date(x, y, color='red', linestyle=(0, (5, 5)), marker=None)

Try using this, and the warning should go away:

plt.plot_date(x, y, fmt='r', linestyle=(0, (5, 5)), marker=None)
Related