0

have original bins on a plot = array([ 0, 2500, 5000, 7500, 10000, 12500, 15000, 17500, 20000, 22500, 25000, 27500, 30000, 32500, 35000, 37500, 40000, 42500, 45000, 47500, 50000, 52500, 55000, 57500, 60000, 62500, 65000, 67500, 70000, 72500, 75000, 77500, 80000, 82500, 85000, 87500, 90000, 92500, 95000, 97500, 100000, 102500], dtype=int64)

which looks like enter image description here

would like to have x tick label displayed in a range and in price format(such as $0-$2,500, $2,500-$50,000, etc..). Something like below but with $ sign before each number enter image description here

Thank you!

Chiefscreation
  • 181
  • 3
  • 12
  • Possible duplicate of [MatPlotLib Dollar Sign with Thousands Comma Tick Labels](https://stackoverflow.com/questions/38152356/matplotlib-dollar-sign-with-thousands-comma-tick-labels) – Sheldore Mar 15 '19 at 21:00
  • @Bazingaa no I need a range of the price displayed in each label/position, the link you sent only has one price per position. – Chiefscreation Mar 15 '19 at 21:02

2 Answers2

1

I see the difference from the dupe. I would present a workaround solution using a sample data since you did not provide a MCVE

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.hist(np.random.normal(50000, 10000, 100000), bins=bins)
ax.set_xlim(0, 100000)

fig.canvas.draw()
labels = [i.get_text().strip('$') for i in ax.get_xticklabels()]
new_labels = [('\${:,}' + 'to' +  '\${:,}').format(int(i), int(j)) 
              for i, j in zip(labels[0:-1],labels[1:])]

ax.set_xticklabels(new_labels, rotation=45)
plt.show()

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71
1

You can use pandas's cut (doc) and numpy’s linspace (or anything equivalent), it should be possible to generate both the intervals bins ( n bins) and corresponding bins labels (n-1). You can use the $ sign to enrich those bins labels. See also Label histogram by bins matplotlib

Adapted example using seaborn:

enter image description here

# coding=utf-8
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

data = np.random.normal(5000, 1000, 1000)

dataframe = pd.DataFrame(data)
dataframe = dataframe.rename({0 : 'values'}, axis='columns')
print(dataframe.head(3))
print('---------------')
cut_vals = np.linspace(start=dataframe['values'].min(),stop=dataframe['values'].max(),num=8)
bin_headers = list()

for index in range(len(cut_vals) - 1):
    header = '$ {}-{}'.format(int(cut_vals[index]), int(cut_vals[index+1]))
    bin_headers.append(header)

print(bin_headers)

print('---------------')
dataframe['categories'] = pd.cut(dataframe['values'], cut_vals, labels=bin_headers)

print('---------------')
print(dataframe.head(2))

ind = np.array(cut_vals[:-1])
width = np.array([cut_vals[i+1]-cut_vals[i] for i in range(len(cut_vals)-1)])

g = sns.distplot(dataframe['values'], bins=cut_vals, label='foo')
g.set_xticks(ind + width/2)
g.set_xticklabels(bin_headers, rotation=45)
plt.show()

Output:

        values
0  4442.338053
1  5253.443608
2  6552.700087
---------------
['$ 2080-3034', '$ 3034-3988', '$ 3988-4942', '$ 4942-5897', '$ 5897-6851', '$ 6851-7805', '$ 7805-8759']
---------------
---------------
        values   categories
0  4442.338053  $ 3988-4942
1  5253.443608  $ 4942-5897
LoneWanderer
  • 3,058
  • 1
  • 23
  • 41