简体   繁体   English

绘制四个象限 plot 一个散点 plot 使用 matplotlib

[英]Drawing four quadrants plot a scatter plot using matplotlib

I wish to plot a 2D scatter plot with values in the range [-0.5, 0.5] and [-0.5,0.5] for x and y coordinates respectively我希望 plot 二维散布 plot 的值分别在 [-0.5, 0.5] 和 [-0.5,0.5] 范围内的 x 和 y 坐标

fig1 = plt.figure(figsize=[7,7])
axes1 = fig1.add_axes([1,1,1,1])
axes1.set_xlabel("xlabel")
axes1.set_ylabel("ylabel")
axes1.set_title("Emotions Quadrants")
axes1.spines['left'].set_position(('axes', 0.5))
axes1.spines['bottom'].set_position(('axes', 0.5))
axes1.scatter(x_vals, y_vals, label="test")

above code scales and x and y coordinates according to to the data.上面的代码根据数据缩放和 x 和 y 坐标。 I wish to show x and y coordinates from -0.5 to 0.5 equally spaced for both the axes and then plot the scatter graph我希望显示 x 和 y 坐标从 -0.5 到 0.5 等间距的两个轴,然后 plot 散点图

I wish to keep the axes intersecting at 0,0 and not scale according to the data points我希望保持轴在 0,0 处相交并且不根据数据点进行缩放


Edit by taking x and y values as xvals = [0.237, 0.415, -0.264, 0.142, -0.037, -0.088, -0.143, -0.332, 0.166, -0.079] y vals= [0.265, 0.325, -0.069, 0.086, -0.136, 0.124, -0.051, -0.052, 0.280, 0.121]通过将 x 和 y 值设为 xvals = [0.237, 0.415, -0.264, 0.142, -0.037, -0.088, -0.143, -0.332, 0.166, -0.079] y vals= [0.265, 0.325, -0.069, 0.086, -0.136、0.124、-0.051、-0.052、0.280、0.121]

I am getting following plot with @r-beginners answer [enter image description here][2]我正在关注 plot 和@r-beginners 回答 [在此处输入图像描述][2]

I want the origin to be in the center and points not scaled and not shift downward [2]: https://i.stack.imgur.com/lxGIs.png我希望原点位于中心并且点不缩放也不向下移动[2]: https://i.stack.imgur.com/lxGIs.png

If you set the axis to the desired range and the frame to the center, you will get the graph you want.如果将轴设置为所需范围并将框架设置为中心,您将获得所需的图形。

import matplotlib.pyplot as plt
import numpy as np

x_vals = np.linspace(-0.5,0.5,10)
y_vals = np.linspace(-0.5,0.5,10)

fig1 = plt.figure(figsize=[7,7])
axes1 = fig1.add_axes([-0.5,-0.5,0.5,0.5])
axes1.set_xlabel("xlabel", labelpad=110)
axes1.set_ylabel("ylabel", labelpad=110)
axes1.set_title("Emotions Quadrants")
axes1.spines[['top', 'right']].set_visible(False)
axes1.spines[['left','bottom']].set_position('center')

# axes1.spines['left'].set_position(('axes', 0.5))
# axes1.spines['bottom'].set_position(('axes', 0.5))

axes1.xaxis.set_ticks_position('bottom')
axes1.yaxis.set_ticks_position('left')

axes1.scatter(x_vals, y_vals, label="test")

plt.show()

在此处输入图像描述

IIUC,国际大学联合会,

x_vals = [0.237, 0.415, -0.264, 0.142, -0.037, -0.088, -0.143, -0.332, 0.166, -0.079] 
y_vals= [0.265, 0.325, -0.069, 0.086, -0.136, 0.124, -0.051, -0.052, 0.280, 0.121] 

fig1 = plt.figure(figsize=[7,7])
axes1 = fig1.add_axes([-0.5,-0.5,0.5,0.5])
axes1.set_xlabel("xlabel", labelpad=110)
axes1.set_ylabel("ylabel", labelpad=110)
axes1.set_title("Emotions Quadrants")
axes1.spines['right'].set_visible(False)
axes1.spines['top'].set_visible(False)

axes1.spines['left'].set_position(('axes', 0.5))
axes1.spines['bottom'].set_position(('axes', 0.5))

axes1.xaxis.set_ticks_position('bottom')
axes1.yaxis.set_ticks_position('left')

axes1.scatter(x_vals, y_vals, label="test")
axes1.set_ylabel('Arousal')
axes1.set_xlabel('Valence')

axes1.set_ylim(-.5,.5)
axes1.set_xlim(-.5,.5)

plt.show()

Output: Output: 在此处输入图像描述

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

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