简体   繁体   English

pyplot:使用多个Y值和分类X值绘制散点图

[英]pyplot: Plotting scatter plot with multiple Y values and categorical X values

I'm trying to create a simple scatter plot with metrics data I collect from my experiments. 我正在尝试使用从实验中收集的指标数据创建一个简单的散点图。 Each day, I test multiple experimental samples and the number of samples varies. 每天,我都会测试多个实验样本,样本数量会有所不同。 I'm trying to create a scatter plot with the days as the x values, and all the experimental values collected on that day as the y values. 我正在尝试创建一个散点图,将天作为x值,并将当天收集的所有实验值作为y值。

I've tried several approaches so far. 到目前为止,我已经尝试了几种方法。

I'll spare the full code, but here is an example of what the data looks like: 我将保留完整的代码,但是下面是数据的示例:

XVals = ['10-Dec-18', '11-Dec-18']
YVals = [[0.88, 0.78, 0.92, 0.98, 0.91],[0.88, 0.78, 0.92, 0.98]]

Since pyplot wants x and y to be of the same dimension, I tried the following suggestion 由于pyplot希望x和y具有相同的维数,因此我尝试了以下建议

for xe, ye in zip(XVals, YVals):
   plt.scatter([xe] * len(ye), ye)

This gives me a value error since my xvals are strings. 这给了我一个值错误,因为我的xval是字符串。

ValueError: could not convert string to float: '10-Dec-18'

I have also tried generating the plot in the following fashion, but again I get an error message because x and y are of different dimensions: 我也尝试过以以下方式生成图,但是由于x和y的尺寸不同,我又收到一条错误消息:

fig, ax = plt.subplots()
ax.scatter(XVals, YVals)
plt.show()

This gives me the obvious error: 这给了我明显的错误:

ValueError: x and y must be the same size

I haven't been able to find any examples of a similar plot (multiple Y values with categorical X values). 我还没有找到任何类似图的示例(多个Y值和X类别的分类值)。 Any help would be appreciated! 任何帮助,将不胜感激!

One option is to create flattened lists for the data. 一种选择是为数据创建扁平化列表。 The first list, X , will contain the day of each data point. 第一个列表X包含每个数据点的日期。 Each day is repeated n times, where n is the number of data points for that day. 每天重复n次,其中n是该天的数据点数。 The second list Y is simply a flattened version of YVals . 第二个列表Y只是YVals的扁平化版本。

import matplotlib.pyplot as plt

XVals = ['10-Dec-18', '11-Dec-18']
YVals = [[0.88, 0.78, 0.92, 0.98, 0.91],[0.88, 0.78, 0.92, 0.98]]

X = [XVals[i] for i, data in enumerate(YVals) for j in range(len(data))]
Y = [val for data in YVals for val in data]

plt.scatter(X, Y)
plt.show()

在此处输入图片说明

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

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