在图上具有原始分箱= 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)
希望以范围和价格格式(例如 $ 0- $ 2,500 , $ 2,500- $ 50,000 等)显示x刻度标签。如下所示,但在每个数字
之前带有$号谢谢!
答案 0 :(得分:1)
我看到了与骗子的区别。由于您未提供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()
答案 1 :(得分:1)
您可以使用pandas的cut
(doc)和numpy的linspace(或任何等效的东西),应该可以同时生成间隔bin(n个bin)和相应的bin标签(n-1)。您可以使用$符号来丰富这些垃圾箱标签。
另请参见Label histogram by bins matplotlib
使用seaborn的自适应示例:
# 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()
输出:
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