简体   繁体   English

使用文件数据中的条件通过Matplotlib Python进行绘图

[英]Using a condition from file data to plot with matplotlib python

I have data in a file. 我的文件中有数据。 It looks like this: 看起来像这样:

08:00,user1,1   
08:10,user3,2   
08:15,empty,0  
....

How could I plot binary data with hours on x-axis and users on y-axis . 我如何在小时数上绘制二进制数据,在x-axis绘制用户,在y-axis绘制用户。 Users will be denoted with different markers according to user. 用户将根据用户使用不同的标记来表示。 For example, user1 to be denoted as * and user3 to be denoted as o . 例如,将user1表示为* ,将user3表示为o And y-axis is 1 for user and 0 for empty. y-axis用户为1 ,空y-axis0 The numbers (in text file) after the username are meant to decide in condition statement which marker it will be. 用户名后的数字(在文本文件中)用于在条件语句中确定它将是哪个标记。

Here is a pic of what I want to do. 这是我想做的照片。

在此处输入图片说明

You can load the file with np.recfromcsv . 您可以使用np.recfromcsv加载文件。 We then convert the time column into datetime objects, for which we define a convtime function. 然后,我们将时间列转换为日期时间对象,为此我们定义了convtime函数。 Then we use this function to read in your CSV file. 然后,我们使用此功能读取您的CSV文件。

import numpy as np
import matplotlib.pyplot as plt
convtime = lambda x: datetime.datetime.strptime(x, "%H:%M")
all_records = np.recfromcsv("myfilename.csv", names=["time", "user", "val"], converters={0:convtime}) # This says parse column 0 using the convtime function

Note that since we have given just the time part to datetime , it will assume the date as 1 January 1900. You can add a relevant date to it if you care. 请注意,由于我们只给datetime指定了时间部分,因此它将假定日期为1900年1月1日。如果需要,可以在其中添加一个相关的日期。

Now, to plot the data. 现在,绘制数据。 This brings us to a curious problem where matplotlib can use only one symbol for all points being plotted. 这给我们带来了一个奇怪的问题,其中matplotlib只能对所有要绘制的点使用一个符号。 Unfortunately, this means we have to use a for loop. 不幸的是,这意味着我们必须使用for循环。 First, let us define dict s for the symbol and colour for each user: 首先,让我们为每个用户的符号和颜色定义dict

symbols = {'user1':'*', 'user3':'o', 'empty':'x'}
colours = {'user1':'blue', 'user3':'red', 'empty':'orange'}
for rec in all_records:
    plt.scatter(rec['time'], rec['val'], c=colours[rec['user']], marker=symbols[rec['user']])

That almost does it. 差不多了。 We are still missing the legend. 我们仍然缺少传说。 A drawback of this for loop is that every row in your file will make one entry in the legend. 这种for循环的缺点是文件中的每一行都会在图例中成为一个条目。 We beat this by creating a custom legend. 我们通过创建自定义图例来解决这个问题。

import matplotlib.lines as mlines
legend_list = []
for user in symbols.keys():
    legend_list.append(mlines.Line2D([], [], color=colours[user], marker=symbols[user], ls='none', label=user))
plt.legend(loc='upper right', handles=legend_list)
plt.show()

That does it! 做到了! If your plot appears squished, then use plt.xlim() to adjust limits to your taste. 如果您的地块看上去被plt.xlim() ,请使用plt.xlim()调整您的品味极限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM