Gireesha Jeera
Gireesha Jeera

Reputation: 35

matplotlib in python - plot a graph using respective value in the x-axies and for the large numbers

Can any one help me with this, i am new to graph a plot.

basically i have list of DocId's which are of some numbers and not necessarily in the order and this needs to come in X-Axies and timelst should come in Y-Axies.

below code actually plotting it, but DocId's not taken as respective DocID, its considered as range. so this needs to know and other thing is document list is huge may be i have 3000 - 5000 DocId's, can this graph goes for long for each DocID ?

import matplotlib.pyplot as plt
Doclst=   [32409057,32409058,32409059,32409060,32409061,32409062,32409063,32409065,32409066,32409067]
timelst=[120,1,4,35,675,1240,500,889,99,10]

plt.plot(Doclst, timelst, marker='o', linestyle='--', color='r', label='time')
plt.xlabel('Document ID'+"'"+'s')
plt.ylabel('Time in Seconds')
plt.title('Performance')
plt.legend()
plt.savefig('graph.png')

Please help me as early as possible.

Upvotes: 2

Views: 785

Answers (1)

MBR
MBR

Reputation: 824

Update: I added two lines, one to access the axis properties, and the other one to force all the labels to show. To avoid the labels to overlap, I add also the argument rotate in plt.ticks to rotate the label by 30°.

If I get right what you want, you should simply use plt.xticks to customize tick labels. You can do something like that

import matplotlib as mpl
import matplotlib.pyplot as plt
Doclst=   [32409057,32409058,32409059,32409060,32409061,32409062,32409063,32409065,32409066,32409067]
timelst=[120,1,4,35,675,1240,500,889,99,10]
fig, ax = plt.subplots()
plt.plot(Doclst, timelst, marker='o', linestyle='--', color='r', label='time')
plt.xlabel('Document ID'+"'"+'s')
plt.ylabel('Time in Seconds')
plt.title('Performance')
plt.legend()
plt.axis([Doclst[0], Doclst[-1], min(timelst)-1, max(timelst)+1])
ax.xaxis.set_major_locator(mpl.ticker.MaxNLocator(len(Doclst)+1))
locs,label = plt.xticks()
plt.xticks(locs, map(lambda x: "%d" %x, locs), rotation=30)
plt.show()

which will give you something like that output

Upvotes: 1

Related Questions