简体   繁体   English

如何使用Matplotlib在python中定义和绘制10 * 10数组?

[英]how can I define and plot an 10*10 array in python with Matplotlib?

I am quite new to Python Matplot. 我对Python Matplot很陌生。 I am trying to plot a 3d bar plot in which the hight of the bar is z and the position of the bars are x,y of my array. 我正在尝试绘制3d条形图,其中条形的最高点是z,条形的位置是我数组的x,y。

This is an example of my data, I have an 10*10 array: 这是我的数据的示例,我有一个10 * 10的数组:

data = np.array([[0.1729,0.8658,0.9283,0.9381,0.9535,1.0000,1.0000,0.9999,0.9999,1.0000],
[-0.0430,0.3109,0.2788,0.5846,0.5369,0.5717,0.6357,0.5619,0.5866,0.6587],
[-0.2228,0.3427,0.1842,0.2106,0.4398,0.3896,0.4687,0.5749,0.4684,0.5484],
[0.0011,0.2899,0.1854,0.1040,0.3513,0.3863,0.3971,0.4485,0.2514,0.4946],
[-0.0096,0.0630,0.1564,-0.0077,0.0825,0.1637,0.2627,0.4800,0.3931,0.3244],
[0.0134,0.1198,0.3339,0.1243,-0.0864,0.4048,0.1996,0.3988,0.1970,0.5248],
[-0.0153,0.2806,0.1926,0.1918,0.0860,0.1054,0.3202,0.3063,0.2644,0.3791],
[-0.1118,0.3065,0.1396,0.0949,0.3459,0.1168,0.3730,0.0741,0.3518,0.2150],
[0.1022,0.3144,-0.1165,0.0509,0.1462,0.3265,0.2087,0.2549,0.3914,0.2683],
[-0.0083,-0.0228,0.0721,0.1086,0.2400,0.1899,0.0922,0.1006,0.2430,0.2768]])

I am using the below code to generate the 3d bar plot : 我正在使用以下代码生成3d条形图:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax1 = Axes3D(fig)

xpos = np.arange(data.shape[0])
ypos = np.arange(data.shape[1])
zpos = data

xposM, yposM = np.meshgrid(xpos, ypos, copy=False)

dx=0.5
dy=0.5
dz=zpos

ax1.bar3d(xposM.ravel(), yposM.ravel(), dz, dx, dy, dz)
plt.show()

but I am getting this error: 但我收到此错误:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Does any body know what is the problem of my code? 有谁知道我的代码有什么问题吗?

There are two issues with your code. 您的代码有两个问题。

1: zpos should not be =data ; 1: zpos不应为=data this is the main hangup you are having. 这是您遇到的主要难题。

2: You should (probably) be calling bar3d as ax1.bar3d(xposM.ravel(), yposM.ravel(), zpos, dx, dy, dz) . 2:您应该(可能)将bar3d称为ax1.bar3d(xposM.ravel(), yposM.ravel(), zpos, dx, dy, dz) As it stands, you are positioning your bars to have their base at where they should go. 就目前而言,您正在将钢筋定位在其应该放置的位置。 That is, a bar of height 3 is going to start at z=3 and go to z=6, and a bar of height -2.1 is going to start at z=-2.1. 也就是说,高度为3的小节将从z = 3开始并达到z = 6,高度为-2.1的小节将从z = -2.1开始。

As for the first issue, xpos and ypos have shape (100,), while data has shape (10,10). 对于第一个问题,xpos和ypos的形状为(100,),而数据的形状为(10,10)。 You need them to have the same dimensions, so I would recommend including zpos = np.zeros(len(data.ravel())) . 您需要它们具有相同的尺寸,因此我建议您包括zpos = np.zeros(len(data.ravel())) This also means you will need to set dz = data.ravel() . 这也意味着您将需要设置dz = data.ravel()

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

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