我有两个数据集来绘制:
LWM:
Client name,CTR,mean_diff
customeronewithverylongname,0.08355714285714286,-0.02935714285714286
customertwowithverylongname,0.028471428571428568,-0.001942857142857142
customerthree,0.014371428571428571,0.000700000000000001
customerfourwithverylongname,0.09971428571428573,0.0014285714285714457
customerfive,0.006799999999999999,0.0014999999999999987
customersixQuickSale,0.0396,0.005075000000000003
customerseven,0.16254285714285713,0.0052428571428571324
PWM:
Client name,CTR,mean_diff
customeronewithverylongname,0.11291428571428572,-0.02935714285714286
customertwowithverylongname,0.03041428571428571,-0.001942857142857142
customerthree,0.01367142857142857,0.000700000000000001
customerfourwithverylongname,0.09828571428571428,0.0014285714285714457
customerfive,0.0053,0.0014999999999999987
customersixQuickSale,0.034525,0.005075000000000003
customerseven,0.1573,0.0052428571428571324
我想绘制一系列直方图,其中x轴上的客户名称和y上的点击率,没有x标签被截断。
我绘制并注意到xlabels被切断了。所以I read this question并以这种方式解决了:
plt.subplots_adjust(left=None, bottom=0.15, right=None, top=None, wspace=None, hspace=None)
我尝试使用bottom
的不同值:
0.35
每次xlabels改变位置时,我都没有相同的xlabels订单。
相反,直方图始终处于相同的位置。
底= 0.15
这是我的代码片段
#defing the labels of the histograms
#pwm and lwm are the last & penultimate week dataframes
# with the weekly mean CTR for each customer
#defing the labels of the histograms
customer_list=set(lwm['Client name'])
x_pos=list(range(len(customer_list)))
x_lab=customer_list
width=0.4
#defining the y max heigh
max_y=max(zip(lwm['CTR'],pwm['CTR']))
#defining the histograms
fig,ax =plt.subplots(figsize=(8,6))
plt.bar(x_pos, pwm['CTR'], width, alpha=0.5, color='b',label=x_lab)
plt.bar([p + width for p in x_pos], lwm['CTR'], width, alpha=0.5, color='r', label=x_lab)
#defining the y max height
plt.ylim([0,max(max_y[0],max_y[1])*1.1])
plt.xticks(x_pos,x_lab,rotation=45, rotation_mode="anchor", ha="right")
plt.title('CTR Bar plot of the last week')
# Adding the legend and showing the plot
plt.legend(['Penultimate Week CTR','Last Week CTR', ], loc='best')
plt.subplots_adjust(left=None, bottom=0.15, right=None, top=None, wspace=None, hspace=None)
plt.show()
I don't knwow if i have to insert more information about the dataset or if it is fine
我很难自拔,我读了the documentation here和this question以及this to。但我仍然没有提出解决方案。
答案 0 :(得分:0)
问题
xlabels因set
而改变位置。
set
是一个不同的可清除对象的无序集合,但这并不意味着它是随机排序的。 (详情请见https://stackoverflow.com/questions/2860339/can-pythons-set-absence-of-ordering-be-considered-random-order)。
所以你的输出是正确的。
解决方案
您需要的是根据您的规格提取标签,然后进行绘制。
例如使用:
customer_list=lwm['Client name']
而不是
customer_list=set(lwm['Client name'])
这样您就可以按照x和y值的相同顺序定义标签。
0.15
0.25
注意如果您需要特定订单,则必须先对数据集进行排序,然后提取绘图标签,例如:
test1=lwm.sort_values(by=['CTR'], ascending=False)
test2=pwm.sort_values(by=['CTR'], ascending=False)
customer_list2=test1['Client name']