user977828
user977828

Reputation: 7679

xtick max value setup with panda and matplotlib

How is it possible that the last xtick would be the last value of the data?

import pandas as pd
import matplotlib.pyplot as plt
a = [583, 1379, 1404, 5442, 5512, 5976, 5992, 6111, 6239, 6375, 6793, 6994, 7109, 7149, 7210, 7225, 7459, 8574, 9154, 9417, 9894, 10119, 10355, 10527, 11933, 12170, 12172, 12273, 12870, 13215, 13317, 13618, 13632, 13790, 14260, 14282, 14726, 15156, 19262, 21501, 21544, 21547, 21818, 21939, 22386, 22622, 22830, 23898, 25796, 26126, 26340, 28585, 28645, 28797, 28808, 28878, 29167, 29168, 31161, 31225, 32284, 32332, 32641, 33227, 34175, 34349, 34675, 34772, 34935, 35086]

d = pd.DataFrame(a)
d.hist(bins=50)
f = plt.figure()
plt.xlabel("xTEST")
plt.ylabel("yTEST")
plt.subplots_adjust(bottom=.25, left=.25)

plt.ticklabel_format(style = 'plain')

plt.title('Title here!', color='black')
plt.xlim(xmin=1, xmax=35086)
plt.show()

Upvotes: 2

Views: 2892

Answers (2)

Saullo G. P. Castro
Saullo G. P. Castro

Reputation: 59005

If you want to use the same figure created by pandas you cannot create a new one (see below). You can set the ticks positions manually adding plt.xticks() after the plt.xlim()

plt.xticks(seq)

where the sequence seq can be pd.np.linspace(1, 35086, 5) for example, or given manually like:

seq = [1, 10000, 20000, 35086]

The final code would be:

import pandas as pd
import matplotlib.pyplot as plt
a = [583, 1379, 1404, 5442, 5512, 5976, 5992, 6111, 6239, 6375, 6793, 6994, 7109, 7149, 7210, 7225, 7459, 8574, 9154, 9417, 9894, 10119, 10355, 10527, 11933, 12170, 12172, 12273, 12870, 13215, 13317, 13618, 13632, 13790, 14260, 14282, 14726, 15156, 19262, 21501, 21544, 21547, 21818, 21939, 22386, 22622, 22830, 23898, 25796, 26126, 26340, 28585, 28645, 28797, 28808, 28878, 29167, 29168, 31161, 31225, 32284, 32332, 32641, 33227, 34175, 34349, 34675, 34772, 34935, 35086]

d = pd.DataFrame(a)
d.hist(bins=50)
ax = plt.gca()
ax.set_xlabel("xTEST")
ax.set_ylabel("yTEST")
plt.subplots_adjust(bottom=.25, left=.25)
plt.ticklabel_format(style = 'plain')

ax.set_title('Title here!', color='black')
ax.set_xlim(xmin=1, xmax=35086)
ax.set_xticks(pd.np.linspace(1, 35086, 5))
plt.show()

Giving:

enter image description here

Upvotes: 2

Happy001
Happy001

Reputation: 6383

Not sure what exactly what you need, but you'll need to delete the line

f = plt.figure()

if you want you histogram of d and the title/label/xlim etc. to be in the same chart.

Upvotes: 1

Related Questions