无法使用创建饼图或条形图matplotlib.pyp文件

2024-10-08 18:24:35 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我的代码,我做的条形图是不对的。对于饼图,我不断得到这个错误:(ValueError:cannot copy sequence with size 9994 to array axis with dimension 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]

Tags: ofthebylabelswithprofitstraxis
1条回答
网友
1楼 · 发布于 2024-10-08 18:24:35

最近我在大学里遇到了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

我希望这个答案是有帮助的。你知道吗

相关问题 更多 >

    热门问题