下面是我的代码,而我制作的条形图并不正确。对于饼图,我一直收到此错误:(ValueError:无法将大小为9994的序列复制到尺寸为2的数组轴上)
The bars instead of showing the number of profit, it somehow make it the month
def totalProfit_month():
#total profit by month
ProfitDataMonth = OrdersOnlyData
ProfitDataMonth["Profit"] = ProfitDataMonth["Profit"].sum()
ProfitDataMonth["Month"] = ProfitDataMonth["Order Date"].dt.month
MonthlyProfit = ProfitDataMonth[["Month", "Profit"]]
MonthlyProfitSum = MonthlyProfit.groupby(by="Month").sum()
MonthlyProfitSum['Profit'] = MonthlyProfitSum['Profit'].map("{:.2f}".format)
MonthlyProfitSum['Profit'] = pd.np.where(MonthlyProfitSum['Profit'].astype(float)
< 0, '-$' + MonthlyProfitSum['Profit'].astype(str).str[1:],
'$' + MonthlyProfitSum['Profit'].astype(str))
print(MonthlyProfitSum)
MonthlyProfitSum = MonthlyProfitSum.reset_index()
#barchart
# barchart_mostProfitable_month = sns.barplot(x="Month", y="Profit", data=MonthlyProfitSum)
# barchart_mostProfitable_month.set_title("Sales by Profit")
#piechart
labels = ProfitDataMonth
sizes = [ProfitDataMonth[["Month", "Profit"]]]
fig1, ax1 = plt.subplots()
ax1.pie(sizes, labels=labels,
shadow=True, startangle=90)
ax1.axis('equal')
plt.show()[enter image description here][1]
答案 0 :(得分:0)
最近,我在大学期间遇到了Matplotlib及其应用程序,并基于它编写了许多程序。我将尝试通过基础知识来解决您的问题,并希望评论中的相关讨论将为您提供指导:)。
饼图
import matplotlib.pyplot as plt
import numpy as np
label=['1st year','2nd year','3rd year','4th year'] #sector labelling
value=[103,97,77,60] #values given to each sector
clrs=['b','r','y','g'] #colouring of different sectors
explode = (0,0,0,0) #each value 0 to get a normal pie chart
plt.pie(value,explode=explode,labels=label,colors=clrs,autopct='%1.1f%%',shadow=False)
plt.title("Pie Chart") #gives title to the pie chart
plt.show() #generates the piechart
对于条形图
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
years=['1st year','2nd year','3rd year','4th year']
stu=[103,97,77,60]
y_pos=np.arange(len(years)) #arange is numpymethod that generates
#an array of sequential numbers.
#We need data for X-axis and we have
#labels that can’t be used for plotting purpose.
plt.bar(y_pos,stu,align='center',alpha=0.5)
plt.xticks(y_pos, years, rotation=30) #added rotation of 30
plt.xlabel('Years') #label on x axis
plt.ylabel('No. of students') #label on y axis
plt.title('Bar Chart') #give your bar graph a title
plt.show() #generate your bar graph
我希望这个答案会有所帮助。