2

How can I make this plot's x-axis looks better?

Is there any way to show only 3 values in x-axis while plotting all values in my dataframe?

image

I tried using plt.xticks(rotation=70) but it still cuts my numbers..

As you can see, it shows the date in the format MMM DD YYYY.

I wanna something like MMM DD or DD MMM. Or even writing in 2 lines DD MM \n YYYY.

How can I achieve that?

This is the code used for generating the chart:

plt.plot(df_21d["Timestamp"], df_21d["Close"], label="Price", color="b")    

plt.title("BTC - 21 Dias")

plt.xlabel("Data")
plt.ylabel("Preco (em USD)")

plt.grid(True)

plt.show()

Here's my data:

df_21d["Timestamp"]

667   2016-05-27 08:00:00
668   2016-05-27 08:30:00
669   2016-05-27 09:00:00
670   2016-05-27 09:30:00
671   2016-05-27 10:00:00
Name: Timestamp, dtype: datetime64[ns]

df_21d["Close"]

667    480.00
668    471.36
669    477.35
670    476.55
671    479.92
Name: Close, dtype: float64

I dont know if I have to format df_21d.Timestamp or if it's a matplotlib configuration. Maybe formatting the timestamp and passing it as a vector using plt.xticks()... but I'm not sure about how do that...

Or maybe even inserting only the first, the last and the middle value for the x-axis, with something like:

plt.xaxis_labels = (df_21d["Timestamp"][1], 
             df_21d["Timestamp"][len(df_21d["Timestamp"])/2], 
             df_21d["Timestamp"][len(df_21d["Timestamp"])])
2
  • I know this is not a solution to your actual problem, but I would recommend you to think about using less ticks. 5-7 should be enough to give the reader a good impression of the scale and it makes your plot less cluttered. Commented Jun 17, 2016 at 11:03
  • @ian_itor I've updated my question. Is there any way to show - as you said - only 3 values in x-axis? I don't wanna use only 3 ticks, I wanna plot all ticks (don't wanna interpolate my data) but show only the first, last and the middle tick timestamp value in x-axis, is this possible? Commented Apr 14, 2017 at 14:52

1 Answer 1

2

This answer might help you: Not write out all dates on an axis, Matplotlib

Essentially, you can set the format of dates by setting the format:

plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d %m'))
Sign up to request clarification or add additional context in comments.

Comments

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.