我不太了解如何绘制直方图,我在分配作业中遇到了问题。我正在尝试绘制2个直方图,以显示每种模式使用的不同燃料类型的频率计数。另一个直方图将是每种特定模式(例如出租车)使用的不同燃料类型的频率计数。
这是我正在使用的数据集:https://data.gov.sg/dataset/monthly-motor-vehicle-population-by-type-of-fuel-used
任何帮助将不胜感激!
import numpy as np
import matplotlib.pyplot as plt
title = "motor-vehicle-population-statistics-by-type-of-fuel-used."
titlelen = len(title)
print("{:*^{titlelen}}".format(title, titlelen=titlelen+6))
print()
data = np.genfromtxt("data/motor-vehicle-population-statistics-by-type-of-fuel-used.csv",
dtype=("datetime64[Y]","U100","U110",int),
delimiter=",",
names=True)
years = np.unique(data["month"])
category = np.unique(data['category'])
type = np.unique(data['type'])
number = data["number"]
cars = data[data["category"]=="Cars"]
carspetrol = cars[cars["type"]=="Petrol"]["number"]
carsCNG = cars[cars["type"]=="CNG"]["number"]
carsDiesel_Electric = cars[cars["type"]=="Diesel-Electric"]["number"]
carsElectric = cars[cars["type"]=="Electric"]["number"]
carsPetrol_CNG = cars[cars["type"]=="Petrol-CNG"]["number"]
carsPetrol_Electric = cars[cars["type"]=="Petrol-Electric"]["number"]
carsPetrolElectricPlug_In = cars[cars["type"]=="Petrol-Electric (Plug-In)"]["number"]
taxis = data[data["category"]=="Taxis"]
Taxispetrol = taxis[taxis["type"]=="Petrol"]["number"]
TaxisCNG = taxis[taxis["type"]=="CNG"]["number"]
TaxisDiesel_Electric = taxis[taxis["type"]=="Diesel-Electric"]["number"]
TaxisElectric = taxis[taxis["type"]=="Electric"]["number"]
TaxisPetrol_CNG = taxis[taxis["type"]=="Petrol-CNG"]["number"]
TaxisPetrol_Electric = taxis[taxis["type"]=="Petrol-Electric"]["number"]
TaxisPetrolElectricPlug_In = taxis[taxis["type"]=="Petrol-Electric (Plug-In)"]["number"]
#motor cyles only have electric and petrol
motor_cyles = data[data["category"]=="Motor-cycles"]
motor_cyles_petrol = motor_cyles[motor_cyles["type"]=="Petrol"]["number"]
motor_cyles_electric = motor_cyles[motor_cyles["type"]=="Electric"]["number"]
bus = data[data["category"]=="Buses"]
buspetrol = bus[bus["type"]=="Petrol"]["number"]
busCNG = bus[bus["type"]=="CNG"]["number"]
busDiesel_Electric = bus[bus["type"]=="Diesel-Electric"]["number"]
busElectric = bus[bus["type"]=="Electric"]["number"]
busPetrol_CNG = bus[bus["type"]=="Petrol-CNG"]["number"]
busPetrol_Electric = bus[bus["type"]=="Petrol-Electric"]["number"]
busPetrolElectricPlug_In = bus[bus["type"]=="Petrol-Electric (Plug-In)"]["number"]
goodsor = data[data["category"]=="Goods & Other Vehicles"]
goodsorpetrol = goodsor[goodsor["type"]=="Petrol"]["number"]
goodsorCNG = goodsor[goodsor["type"]=="CNG"]["number"]
goodsorDiesel_Electric = goodsor[goodsor["type"]=="Diesel-Electric"]["number"]
goodsorElectric = goodsor[goodsor["type"]=="Electric"]["number"]
goodsorPetrol_CNG = goodsor[goodsor["type"]=="Petrol-CNG"]["number"]
goodsorPetrol_Electric = goodsor[goodsor["type"]=="Petrol-Electric"]["number"]
goodsorPetrolElectricPlug_In = goodsor[goodsor["type"]=="Petrol-Electric (Plug-In)"]["number"]
plt.figure(1) # second figure
plt.hist(buspetrol,
alpha=0.5,
color=['red'])
plt.title("bus petrol",fontsize=20)
plt.ylabel('number of users',fontsize=10)
plt.figure(2) # second figure
plt.hist(busCNG,
alpha=0.5,
color=['red'])
plt.title("bus CNG",fontsize=20)
plt.ylabel('number of users',fontsize=10)
plt.figure(3) # second figure
plt.hist(busDiesel_Electric,
alpha=0.5,
color=['cyan'])
plt.title("busDiesel_Electric",fontsize=20)
plt.ylabel('number of users',fontsize=10)
plt.xticks(rotation='vertical')
plt.figure(4) # second figure
plt.hist(busCNG,
alpha=0.5,
color=['red'])
plt.title("bus CNG",fontsize=20)
plt.ylabel('number of users',fontsize=10)
plt.show()
答案 0 :(得分:-1)
我不知道我是否理解你的问题,但是我可以通过以下方式解决它:
_, bins, _ = plt.hist(busCNG, bins=50, range=[0,80], alpha=0.5, color='r', density=True)
_ = plt.hist(buspetrol, bins=bins, alpha=0.5, color='b', density=True)
_ = plt.hist(busDiesel_Electric, bins=bins, alpha=0.5, color='g', density=True)
_ = plt.hist(busElectric, bins=bins, alpha=0.5, color='#d8dcd6', density=True)
plt.ylabel('number of users',fontsize=10)
plt.show()
直方图不好,因为每种特定模式x燃料类型的记录很少。
答案 1 :(得分:-1)
我可以使用您的代码本身绘制所有4个直方图。您唯一错过的一件事是“以title变量结尾
Blockquote
title = "motor-vehicle-population-statistics-by-type-of-fuel-used. "
答案 2 :(得分:-1)
如果我理解正确,那么您真正想要的是条形图。玩转公交数据:
fig=plt.figure(figsize=(16, 8))
bus_data = [buspetrol, busCNG, busDiesel_Electric, busElectric, busPetrol_Electric, busPetrolElectricPlug_In]
bus_data_count = []
for bd in bus_data:
print
if len(bd): # Doing this because some of the bus arrays are empty
b = bd[0] # Data are multidimensional, for different months. I'm only taking the first month here, you can iterate over these as well.
bus_data_count.append(b)
else:
bus_data_count.append(0) # To count the empty ones
bus_data_labels = ['buspetrol', 'busCNG', 'busDieselElectric', 'busElectric', 'busPetrolElectric', 'busPetrolElectricPlugIn']
x = np.arange(len(bus_data))
plt.bar(x, bus_data_count)
plt.xticks(x, bus_data_labels, rotation='45')
plt.tight_layout()
plt.show()
结果:
您可以为要绘制的每个数据集运行类似的功能。