获取工具栏中matplotllib的数据

2024-04-25 02:07:31 发布

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

目前正在尝试使用面向对象接口的matplotlib。我对这个工具还是新手

这是我想用matplotlib创建的图形(使用excel)的最终结果

enter image description here

我已经把这个表加载到dataframe中,看起来像这样。 enter image description here

下面是我写的代码

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

loaddf = pd.read_excel("C:\\SampleRevenue.xlsx")

#to get the row on number of tickets
count = loaddf.iloc[0]
#to get the total proceeds I get from sellling the ticket
vol = loaddf.iloc[1]
#The profit from tickets after deducting costs
profit = loaddf.iloc[2]

fig, ax = plt.subplots(figsize=(8, 4))
ax.barh(str(count), list(loaddf.columns.values))

不知怎么的,这就是我收到的图表。如何以条形图的形式显示每个月的票号?意向是Y轴上的票数和x轴上的月数 enter image description here

这是使用iloc提取行之后的count、vol和profit序列。 在用于绘图之前是否需要删除序列? enter image description here


Tags: thetofromimportgetmatplotlibascount
1条回答
网友
1楼 · 发布于 2024-04-25 02:07:31

当数据帧被转置时,read_excel会变得非常混乱。它期望第一行是列的标题,随后的每一行是下一个条目。(可选)第一列包含行标签。在这种情况下,必须将index_col=0添加到read_excel的参数中。如果在Excel中复制并粘贴转置所有内容,其工作方式可能如下:

import pandas as pd
import matplotlib.pyplot as plt

loaddf = pd.read_excel("C:\\SampleRevenue_transposed\.xlsx", index_col=0)
loaddf[["Vol '000"]].plot(kind='bar', title ="Bar plot of Vol '000")
plt.show()

如果不转置Excel,则标题行将获取部分数据,这将导致“no numeric data to plot”消息

相关问题 更多 >