如何执行 Xticks 命令未更改

How to enforce Xticks order not changed

我正在绘制有关房地产建筑商数据的图表。有几个建筑商以不同的价格出售不同类型的公寓。我想绘制数据,因为公寓类型是 X 轴,公寓价格是 Y 轴。不能保证每个建筑商都建造相同类型的公寓,您可以在下面查看数据。当我绘制下面的数据时,它不是按 ApartmentType 来排序的,因为并非所有建筑商都提供所有类型的公寓。我正在寻找的预期输出是,即使 Builder 没有建造所有公寓,X 轴也应该完好无损。 以下是示例数据。

Builder,UnitType,Area,Price,AvgPriceInLakhs,UnitSizeInBhk
SJR Palazza City,Apartment,702 sq.ft,47 L,4700000.0,0.5
SJR Palazza City,Apartment,702 sq.ft,35 L -48.50 L,4175000.0,1
SJR Palazza City,Apartment,1242-1308 sq.ft,62.10 L -87.50 L,7480000.0,2
SJR Palazza City,Apartment,1550-1813 sq.ft,66.71 L -1.17 Cr,9185500.0,3
SJR Palazza City,Apartment,1657 sq.ft,84.50 L,8450000.0,3.5
SJR Palazza City,Apartment,1813 sq.ft,1.70 Cr,17000000.0,4
Century Ethos,Apartment,510 sq.ft,25.44 L,2544000.0,0.5
Century Ethos,Apartment,693 sq.ft,30 L -61.06 L,4553000.0,1
Century Ethos,Apartment,895 sq.ft,61.06 L,6106000.0,1.5
Century Ethos,Apartment,1040-1245 sq.ft,40 L -83.60 L,6180000.0,2
Century Ethos,Apartment,1437 sq.ft,76.03 L,7603000.0,2.5
Century Ethos,Apartment,1732-2157 sq.ft,66.86 L -1.25 Cr,9593000.0,3
Century Ethos,Apartment,2200 sq.ft,1.10 Cr,11000000.0,4
Prestige Lake Ridge,Apartment,661 sq.ft,36 L,3600000.0,0.5
Prestige Lake Ridge,Apartment,661 sq.ft,38 L -38.90 L,3845000.0,1
Prestige Lake Ridge,Apartment,1137-1159 sq.ft,56.85 L -73 L,6492500.0,2
Prestige Lake Ridge,Apartment,1345-1367 sq.ft,75 L -76.22 L,7561000.0,2.5
Prestige Lake Ridge,Apartment,1571 sq.ft,63.70 L -1.09 Cr,8635000.0,3
Prestige Lake Ridge,Apartment,1745-1750 sq.ft,96.07 L -96.35 L,9621000.0,3.5
Salarpuria Sattva Aspire,Apartment,613 sq.ft,60 L,6000000.0,1
Salarpuria Sattva Aspire,Apartment,1388-1799 sq.ft,84.28 L -1.20 Cr,10214000.0,2
Salarpuria Sattva Aspire,Apartment,1459-1622 sq.ft,98 L -1.10 Cr,10400000.0,2.5
Salarpuria Sattva Aspire,Apartment,1933-2762 sq.ft,98 L -1.93 Cr,14550000.0,3
Salarpuria Sattva Aspire,Apartment,2949-2956 sq.ft,2 Cr -2.20 Cr,21000000.0,4
Salarpuria Sattva Aspire,Apartment,6290 sq.ft,4.08 Cr -4.35 Cr,42150000.0,4+
Salarpuria Sattva Aspire,Apartment,6290-6314 sq.ft,4.08 Cr -4.35 Cr,42150000.0,5
Global Golden Pearl,Apartment,250 sq.ft,25.44 L,2544000.0,0.5
Global Golden Pearl,Apartment,620-636 sq.ft,30.42 L -48 L,3921000.0,1
Global Golden Pearl,Apartment,895 sq.ft,40 L -85 L,6250000.0,2
Global Golden Pearl,Apartment,1180-1257 sq.ft,59 L -62.85 L,6092500.0,2.5
Global Golden Pearl,Apartment,1509-1629 sq.ft,72.35 L -1.75 Cr,12367500.0,3
Global Golden Pearl,Apartment,1655 sq.ft,82.75 L,8275000.0,3.5
Global Golden Pearl,Apartment,1917 sq.ft,1.27 Cr -2.60 Cr,19350000.0,4
plt.subplots(figsize=(20,8))
g = sns.lineplot(x='UnitSizeInBhk',y='AvgPriceInLakhs',data=top5buildersdatadf,hue='Builder') 
g.set_title('Top 5 Builders Data')
g.set(xlabel='Car Model Year',ylabel='Average MPG') 
plt.show()

仅供参考,这是原始图表

当每个 builderapartment size 始终只有一个数据点时,您需要做的就是根据 apartment size.

排序
import numpy as np
import matplotlib.pyplot as plt

# Unsorted dummy data
n = 10
x1 = np.arange(n)
np.random.shuffle(x1)
y1 = np.random.random(n)

# Sort the x values from small to large
i = np.argsort(x1)
x2 = x1[i]
y2 = y1[i]

fig, ax = plt.subplots(1, 2)
ax[0].plot(x1, y1)
ax[1].plot(x2, y2)


#                Unsorted                                     Sorted


我会删除大小为 4+ 的数据点(在您的示例中,该值与 5 的值相同),因为这会阻止您使用数值。对于数值,排序是明确的,应该给出预期的结果:

# df = top5buildersdatadf
df.drop(df[df['UnitSizeInBhk'] == '4+'].index, axis=0, inplace=True)
df['UnitSizeInBhk'] = pd.to_numeric(df['UnitSizeInBhk'])

# do plotting