1

I have this following code in order to generate scatterplots

            import matplotlib.pyplot as plt

              line = plt.figure()    
              plt.plot(xvalue, yvalue)
              plt.grid(True)
              plt.savefig("test.png")
              plt.show()

and here is the screenshot of the plot: enter image description here

I am just wondering if i could change the x-axis labels into strings. I have stored all the labels in

    xlabel = ['2015/4/1', '2015/4/11', '2015/4/12', '2015/4/18', '2015/4/19'...]

Is there any function for matplotlib so that i could set x-axis labels to the values in "xlabel"?

many thx!

ALso my labels are overlapped, anything i could do to fix this problem? thx!

enter image description here

0

2 Answers 2

6

Here is my answer. You target was to plot the datetime as xticklabel.
I always do something like this. Code like this:

## For example, I have 9 daily value during 2013-04-01 to 2014-04-10
start = datetime.datetime.strptime("01-04-2013", "%d-%m-%Y")
end = datetime.datetime.strptime("10-04-2013", "%d-%m-%Y")
date = [start + datetime.timedelta(days=x) for x in range(0, (end-start).days)]

plt.figure(figsize=(12,4))
## y is the data I want to plot
ind = np.arange(len(y))
ax=plt.subplot()
plt.plot(ind,y,lw = 3)
k = []
for i in range(0,len(ind),1):
    k.append(str(date[i])[0:10])

plt.xticks(ind,k,rotation=65)

enter image description here

Update

To solve the overlap problem, I recommend the code below:

for label in ax.xaxis.get_ticklabels()[::2]:
    label.set_visible(False)       

For daily value in a month, you can get a figure like this:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

thx for the thorough answer! exactly waht i want for the overlapping issue!
0

Do:

plt.xticks(xs, labels)

Where xs is a list of the positions for the ticks, and labels is the list of labels.

3 Comments

sorry about pressing the enter button by accident. my labels are overlapped, is there anyway to fix this? thx!
i did try plt.xticks(xvalue, dates,rotation=45) and plt.subplots_adjust(bottom=0.05). but they are not working so well. thx for the help!
i have solved the problem of overlapping with other's answer. Thx for the xticks thing!

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.