0

I am trying to plot price over date. But the density is too much so I want to use locator_params() to control it. My code is below.

import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
import matplotlib.dates as mdates

df = pd.read_csv(in_data)
y = df['Price']
x = df['DATE']

plt.plot(x, y)
plt.xlabel('Date')        
plt.xlabel('Yield')
plt.title('test_plot')
plt.gcf().autofmt_xdate()
plt.locator_params(nbins=20)

I am trying to put only 20 dates on the plot. However, the plot pick the first 20 dates in x and put it on the plot, rather than pick every (total_number_of_date/20) date. Could you guys tell me what I missed here?

I also tried added in the following code but it won't help either.

plt.IndexLocator(base = 35, offset=0)

Thank you!

A story-teller
  • 113
  • 1
  • 9
  • Matplotlib will by default not put more than 7 or 8 ticks on an axis. This in turn means that if you see much more than 20, something else is wrong. Most probably you don't plot actual dates, but strings. However, since strings are interpreted as categories you cannot subdivide them into bins (i.e. if you plot ["Apple", "Banana", "Cherry"] and tick only every second point, how should one know that the second entry is "Banana" and not "Ananas"? – ImportanceOfBeingErnest Oct 14 '18 at 22:00
  • is there a way to convert my "date" into actual date? – A story-teller Oct 14 '18 at 22:04
  • https://stackoverflow.com/questions/16852911/how-do-i-convert-dates-in-a-pandas-data-frame-to-a-date-data-type – ImportanceOfBeingErnest Oct 14 '18 at 22:10
  • @ImportanceOfBeingErnest Thanks this seems to solve to problem – A story-teller Oct 14 '18 at 23:07

0 Answers0