我开发了一个perl脚本来处理数据并给我一个最终的csv文件。不幸的是,我的系统不支持perl中的图形和图表包,由于工作限制,我无法安装它们。所以我想尝试使用csv文件,在Python中组合一些东西来生成一个混合图。我希望第一列是x轴上的标签。接下来的三列是条形图。第四列是横穿x轴的一条线。在
以下是示例数据:
Name PreviousWeekProg CurrentWeekProg ExpectedProg Target
Dan 94 92 95 94
Jarrod 34 56 60 94
Chris 45 43 50 94
Sam 89 90 90 94
Aaron 12 10 40 94
Jenna 56 79 80 94
Eric 90 45 90 94
我做了一些研究,但是由于我在python中的无知,我想征求一些关于在python中混合使用图表和图形的好模块的指导。对不起,如果我的帖子模糊不清的话。除了在网上看其他的参考资料,我对如何做这件事一无所知。另外,我的python版本是3.8,而且我确实安装了matplotlib(这是之前建议我使用的)。在
由于@ShaunLowis的答案没有包含完整的示例,我想我应该添加一个。就读取} ,正如另一个答案所指出的那样。在本例中,我将文件命名为
.csv
文件而言,在这种情况下,最好的方法可能是使用^{test.csv
,并将其放在运行脚本的同一目录中解释
从
pandas
文档中,read_csv()
(排除了与示例无关的参数)在本例中,我指定的是文件的路径,而不是文件对象。在
^{pr2}$这将提取} 对象。为了绘制一个名称的多个条形图,我引用了this answer。但是,为了使用这个方法,我们需要一个与
'Name'
列中的值并将其转换为^{names
数组长度相同的浮点数组,因此然后设置条的宽度并相应地偏移第一条和第三条,如referenced answer中的轮廓
从^{} 页(排除未使用的非位置参数)
在本例中,}是值序列(每个条不同),而
x
和{width
是一个标量(每个条都是相同的)。在接下来是
target
的行,它非常简单,只需根据'Target'
列中的值绘制前面创建的x
值其中
lw
指定线宽。免责声明:如果.csv
的每一行的目标值都不相同,则此操作仍然有效,但可能与您希望的不完全相同。在接下来的两行
只需在适当的
x
位置添加这些名称,然后设置y
限制来跨越[0, 100]
。在最后一点是在情节下方添加图例
请参阅this answer,了解如何根据需要调整此值。在
您可以使用包
seaborn
中的参数hue
。首先,需要使用函数melt
重塑数据集:输出:
^{pr2}$现在您可以使用列
'variable'
作为函数barplot
中的hue
参数:我建议您使用Pandas库的'read_csv()'实用程序读取.csv文件,如下所示:
对象存储在这个信息框中。然后可以通过以下方式访问列:
^{pr2}$之后您可以拨打:
在你想画的任何一列上。 配置子批次是另一种方法,我建议使用matplotlib的pyplot。在
我建议从这个figure和axis对象声明开始,然后从那里开始:
在这里您可以阅读更多关于添加轴数据here。在
让我知道如果这有帮助!在
相关问题 更多 >
编程相关推荐