简体   繁体   English

带有多个 Y 值的散点图,每个类别 X 标签带有线

[英]Scatter plot with multiple Y values with line for each category X label

I am trying to create a scatter plot, where each x-axis category label contains two data points (from different conditions), and displayed in a way that shows the trend between the two.我正在尝试创建一个散点图,其中每个 x 轴类别标签包含两个数据点(来自不同条件),并以显示两者之间趋势的方式显示。 With the following code, I have managed to match each x-axis category labels with its designated data points.使用以下代码,我设法将每个 x 轴类别标签与其指定的数据点匹配。

import numpy as np
import matplotlib.pyplot as plt
import csv

# x-axis labels    
IDs = ['a', 'b', 'c', 'd', 'e', 'f']

# y-axis data
lowCont = [-0.31, 0.71, 0.37, 0.05, 0.15, 1.33]
highCont = [-0.38, -0.16, 0.02, -0.55, -0.02, -0.51]

# Standard Errors for each data point
lowContErr = [0.03,0.13,0.02,0.10,0.09,0.04]
highContErr = [0.07, 0.09, 0.03, 0.09, 0.06, 0.03]

# plotting
plt.scatter(range(len(lowCont)), lowCont, color = 'r', label = 'label1')
plt.scatter(range(len(highCont)), highCont, color = 'k', label = 'label2')

plt.xticks(range(len(lowCont)), IDs, size='small')

plt.errorbar(range(len(lowCont)), lowCont,yerr=lowContErr, linestyle="None", color = 'r')
plt.errorbar(range(len(highCont)), highCont,yerr = highContErr,linestyle = "None", color = 'k')

plt.xlabel('x')
plt.ylabel('y')
plt.title('graph title')
plt.legend()
plt.show()

However, what I am trying to establish here is to highlight the trend between the two data points for each x-axis label (increasing or decreasing).但是,我在这里试图建立的是突出显示每个 x 轴标签(增加或减少)的两个数据点之间的趋势。 For this, I need the coupling of data points to be displayed side by side (rather than on top of each on a single vertical axis).为此,我需要并排显示数据点的耦合(而不是在单个垂直轴上的每个点的顶部)。 Here is a sample of the desired plot for one x-axis label:以下是一个 x 轴标签所需图的示例: 在此处输入图片说明

I guess my thought patterns direct me to create dummy sub x-axis categories for x-axis category (eg, 0, 1), and assign the data points to them, but my skills in python and matplotlib are not enough for what I am trying to make.我想我的思维模式指导我为 x 轴类别(例如 0、1)创建虚拟x 轴类别,并将数据点分配给它们,但是我在 python 和 matplotlib 方面的技能不足以满足我的需求试图使。

You can achieve this by shifting the highCont along the x axis by a certain amount, then using plt.plot() to draw lines between them.您可以通过将highCont沿 x 轴移动一定量,然后使用plt.plot()在它们之间绘制线条来实现这一点。

I have used the variable shift to move the highCont values along the x axis by 0.2 in the example below.在下面的示例中,我使用变量shifthighCont值沿 x 轴移动了 0.2。

You can add caps to your error bars (which you included in the desired image) by using the capsize argument of plt.errorbar() , which defaults to None if not provided.您可以通过添加帽到您的误差条(其中包括你中所需的影像) capsize的参数plt.errorbar()默认为None ,如果不提供。

import matplotlib.pyplot as plt
import numpy as np

IDs = ['a', 'b', 'c', 'd', 'e', 'f']

lowCont = [-0.31, 0.71, 0.37, 0.05, 0.15, 1.33]
highCont = [-0.38, -0.16, 0.02, -0.55, -0.02, -0.51]

lowContErr = [0.03,0.13,0.02,0.10,0.09,0.04]
highContErr = [0.07, 0.09, 0.03, 0.09, 0.06, 0.03]

shift = 0.2   # Change this to increase distance between pairs of points
x_vals = np.arange(0,len(lowCont),1)
shifted_x_vals = np.arange(0+shift,len(highCont)+shift,1)

# loop through the data and plot the pairs of points to join them by a line
for x,x1,y,y1 in zip(x_vals,shifted_x_vals,lowCont,highCont):
    plt.plot([x,x1], [y,y1], color="k")

plt.scatter(x_vals, lowCont, color = 'r', label = 'label1')
plt.scatter(shifted_x_vals, highCont, color = 'k', label = 'label2')

# set ticks to between the two points
plt.xticks(x_vals + (shift/2), IDs, size='small')

plt.errorbar(x_vals, lowCont,yerr=lowContErr, linestyle="None", color = 'r', capsize=3)
plt.errorbar(shifted_x_vals, highCont,yerr = highContErr,linestyle = "None", color = 'k', capsize=3)

plt.xlabel('x')
plt.ylabel('y')
plt.title('graph title')
plt.legend()

plt.show()

Which gives这给

在此处输入图片说明

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

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