1

I'm trying to plot a stock chart. My DataFrame structure looks like this:

<class 'pandas.core.frame.DataFrame'>
Int64Index: 503 entries, 0 to 506
Data columns (total 6 columns):
Date      503 non-null object
Open      503 non-null float64
High      503 non-null float64
Low       503 non-null float64
Close     503 non-null float64
Volume    503 non-null float64
dtypes: float64(5), object(1)
memory usage: 27.5+ KB

When I try to plot the data using the Close-column I get numbers on the x axis, but I want to plot it with the dates using 3 month ticks. How can I achieve that?

At the moment the code for the plot looks like this:

dax_data["Close"].plot(label = "Dax Close", figsize = (16,8), title = "Dax Prices")
plt.legend();

Here you can see the plot

1 Answer 1

2

That's because "DataFrame.plot()' use by default the dataframe index as x values. You should do like this :

dax_data.plot(x = 'Date', y = 'Close', label = "Dax Close", figsize = (16,8), title = "Dax Prices")

And to have 3 month ticks :

import matplotlib.dates as mdates

### Your code to get dax_data

ax = dax_data.plot(x = 'Date', y = 'Close', label = "Dax Close", figsize = (16,8), title = "Dax Prices")
ax.xaxis.set_major_locator(mdates.MonthLocator(interval = 3)) # to display ticks every 3 months
ax.xaxis.set_major_formatter(mdates.DateFormatter("%m-%Y")) # to set how dates are displayed
plt.legend()
Sign up to request clarification or add additional context in comments.

1 Comment

Great. Thank you. That's exactly what I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.