Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib二维栏,散点未按预期对齐_Python_Matplotlib_Bar Chart - Fatal编程技术网

Python Matplotlib二维栏,散点未按预期对齐

Python Matplotlib二维栏,散点未按预期对齐,python,matplotlib,bar-chart,Python,Matplotlib,Bar Chart,我正在尝试创建一个类似于中所示的二维条形图 在下面的代码中,我希望散点位于每个条的底部,但它看起来像是沿着y=0绘制所有内容。我还预计酒吧的顶部会有所不同,而不是底部 (我还想让条形图与散点图的颜色相同,但我还没有开始解决这个问题。) 查看下面的代码: import numpy as np import matplotlib import matplotlib.pyplot as plt # Data generation # I changed y=-x to y=x. In the for

我正在尝试创建一个类似于中所示的二维条形图

在下面的代码中,我希望散点位于每个条的底部,但它看起来像是沿着y=0绘制所有内容。我还预计酒吧的顶部会有所不同,而不是底部

(我还想让条形图与散点图的颜色相同,但我还没有开始解决这个问题。)


查看下面的代码:

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

# Data generation
# I changed y=-x to y=x. In the former case,
# the bottoms of the bars varied. This is because
# you plot them with negative values! So, the bars
# are being drawn from the x,y-plane at zero to an
# x,y-plane BELOW the zero plane. If you use y=x 
# instead, the bars are drawn from the x,y-plan at
# zero to an x,y-plane ABOVE zero!
x = np.arange(25)
y = x
z = x/15

# Initiate figure and axes
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Generate the viridis color map with as many color
# values as the size of the x-array (or the y-array)
cmap = matplotlib.cm.get_cmap('viridis')
colors = np.zeros((x.size, 4))

for i in range(x.size):
    colors[i,:] = cmap(i / x.size)
    
# Loop over the different x- and y-values. Plot each
# combination separately with a zs value equal to the
# current y-value. Use the iterator 'i' to extract the
# proper color from the 'colors' matrix
for i, (xval, yval) in enumerate(zip(x, y)):
    # Even though we only have one x-value and one y-value,
    # these must be arrays (otherwise Axes.bar throws and
    # error)!
    ax.bar([xval], [yval], zs=yval, zdir='y', color=colors[i], alpha=0.8)

ax.scatter(x, y, s=45, c=z, cmap='viridis')

ax.set(ylabel='y')
ax.set(xlabel='x')

plt.show()
首先,由于
y
数组中的值为负数,因此条的底部每一步都不同!这意味着正在为z=0的x,y平面绘制钢筋,以负z值绘制x,y平面。因此,这种行为是意料之中的。如果希望条在顶部变化/步进,应将行
y=-x
更改为
y=x

通过将
y
数组更改为正值,也可以解决有关散射点的问题,因为当从z=0的x,y平面开始时,条形将指向“向上”

对于条的颜色,我们首先将viridis colormap(或您想要使用的任何其他colormap)存储在名为
cmap
的变量中。然后,我们分配一个
color
矩阵,其行数与数据点和四列(r、g、b、a)的行数相同。使用
for
循环,我们从前面提到的颜色映射中获得颜色。请注意,您需要向colormap提供一个规格化值(即范围[0,1]中的值),以便获得颜色。对于这个标准化值,我们只需使用迭代变量的值除以数据的总大小。我们在调用Axes.bar时使用颜色

如果我运行上面的代码,我会得到以下输出:

与侧节点一样,如果要在条形图顶部绘制散布点,则需要为散布点的z值提供一个值(在这种情况下,该值将仅等于
y
-数组中的值)。当前,在二维中绘制散射点(由于仅提供x和y值,因此所有散射点都位于z=0的x、y平面上)。假设我们将调用更改为
Axes.scatter
为:

# For a 3D situation, you need to give three dimensions to the Axes.scatter 
# function in order for it to plot in 3D (previously, you only had 'x, y').
# Since the z-value should equal the y-value in this case, we now have 
# 'x, y, y'.
ax.scatter(x, y, y, s=45, c=z, cmap='viridis')
更改后,输出如下所示:


感谢您提供清晰详细的解决方案!我要补充的唯一一点是y值实际上是负数,因此
y=-x
是正确的/必要的;但是添加
ax.invert_zaxis()
会保留负y并绘制条带,使其在顶部变化。@a11非常好的添加!我自动假设数据应该是正的,但你完全正确,如果你只是反转轴,也可能出现负值!
# For a 3D situation, you need to give three dimensions to the Axes.scatter 
# function in order for it to plot in 3D (previously, you only had 'x, y').
# Since the z-value should equal the y-value in this case, we now have 
# 'x, y, y'.
ax.scatter(x, y, y, s=45, c=z, cmap='viridis')