matplotlib:将颜色分配给半径 [英] matplotlib: assign color to a radius

查看:93
本文介绍了matplotlib:将颜色分配给半径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张磁盘的3D图。
现在,我想根据存储在数组中的值在颜色上绘制表面。
例如圆盘半径为300mm。数组可以是:

I have a 3D plot of a disk. Now I would like to plot the surface in color depending on values which are stored in an array. E.g. the radius of the disk is 300mm. The array could like:

arr = np.array([[ 114.28, 14],
                [ 128.57, 16],
                [ 142.85,19],
                [ 157.13,20],
                [ 171.41,21],
                [ 185.69,22],
                [ 199.97,24],
                [ 214.25,27],
                [ 228.53,29],
                [ 242.81,30],
                [ 257.09,31],
                [ 271.37,34],
                [ 288.65,35],
                [ 299.93,36],
                [ 300,38]])

对于半径= 114.28来说,我的值为14。我想用一种颜色(例如蓝色)在半径114.28处绘制一个圆。第二半径是128.57。对于半径128.57,将分配值16。这意味着我想用其他颜色绘制一个圆,例如

It means for radius = 114.28 I have the value 14. I would like to plot a circle at radius 114.28 in a color (e.g. blue). Second radius is 128.57. For the radius 128.57 the value 16 is assigned. That means I would like to plot a circle in another color e.g. orange on the surface of the plot and so on.

我试图用Contourplot解决这个问题(感谢bazingaa)。看起来完全符合我的要求。不幸的是,我只是意识到这并不是解决我的问题的方法。也许我应该解释我要达到的目标。我想展示某些参数(例如速度)如何沿光盘分布。在这种情况下,轮廓线也将随着速度的增加而校正。但是也可能发生这样的情况:必须将参数可视化,而这些参数并不总是朝着外部连续增加。我尝试了这种情况,只是简单地使数组中间的值小于之前的值(我将arr的值从27更改为14),并且轮廓图没有任何变化,只是图例发生了变化。毕竟,contourplot不是正确的方法吗?也许我必须绘制单个圆并为其分配颜色,然后在圆之间进行插值以填补空白。

I tried to solve this problem with contourplot (thanks to bazingaa). It looks exactly as I want it to. Unfortunately I just realized that this is not really the solution to my problem. Maybe I should explain what I'm trying to achieve. I want to show how certain parameters, such as speed, are distributed along the disc. In this case the contourplot would also correct, as the speed increases towards the outside. But it can also happen that parameters have to be visualized which do not always increase towards the outside continuously. I tried this scenario and simply made a value in the middle of the array smaller than the value before it (I changed the value 27 to 14 from arr) and nothing happens in the contourplot except that the legend changes. Maybe contourplot is not the right approach after all? Maybe I have to draw individual circles and assign colors to them and interpolate between the circles to fill the gaps.

import numpy as np
import matplotlib as mlp
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as axes3d

ri = 100
ra = 300
h=20

# input xy coordinates
xy = np.array([[ri,0],[ra,0],[ra,h],[ri,h],[ri,0]])
# radial component is x values of input
r = xy[:,0]
# angular component is one revolution of 30 steps
phi = np.linspace(0, 2*np.pi, 2000)
# create grid
R,Phi = np.meshgrid(r,phi)
# transform to cartesian coordinates
X = R*np.cos(Phi)
Y = R*np.sin(Phi)
# Z values are y values, repeated 30 times
Z = np.tile(xy[:,1],len(Y)).reshape(Y.shape)


fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')

ax.set_zlim(0,200)
ax.plot_surface(X, Y, Z, alpha=0.5, color='lightgrey', rstride=1, cstride=1)

plt.contourf(X, Y, np.sqrt(X ** 2 + Y ** 2), zdir='z', offset=20, 
             cmap=plt.cm.rainbow)
cbar = plt.colorbar() 
arr = np.array([[ 114.28, 14],
                [ 128.57, 16],
                [ 142.85,19],
                [ 157.13,20],
                [ 171.41,21],
                [ 185.69,22],
                [ 199.97,24],
                [ 214.25,27],
                [ 228.53,29],
                [ 242.81,30],
                [ 257.09,31],
                [ 271.37,34],
                [ 288.65,35],
                [ 299.93,36],
                [ 300,38]])
cbar.ax.set_yticklabels(arr[:,1])

plt.show()

我希望有人能够提供帮助,这对我来说非常重要。
最好的问候!

I hope someone can help, it is very important to me. Best regards !

推荐答案

这里是一种解决方案。我刚刚在您的第一个代码之后添加了以下几行,其最后一行是 ax.plot_surface(X,Y,Z,alpha = 0.5,color ='lightgrey',rstride = 1,cstride = 1)。我还没有使用 plt.show()。 PS:通过使用 phi = np.linspace(0,2 * np.pi,2000) phi 的密度c>最初您只有 20 个数据点。

Here is one solution. I just added the following lines after your first code whose last line is ax.plot_surface(X, Y, Z, alpha=0.5, color='lightgrey', rstride=1, cstride=1). I didn't use plt.show() yet. P.S.: I increased the density of phi by using phi = np.linspace(0, 2*np.pi, 2000) as initially you had just 20 data points. This is to plot relatively smoother circles.

plt.contourf(X, Y, np.sqrt(X ** 2 + Y ** 2), zdir='z', offset=20, 
             cmap=plt.cm.bwr)
plt.colorbar()
plt.show()

我选择了 z = 20 作为等高线图的高度。您可以根据自己的选择进行修改。

I have chosen z=20 as the height for the contour plot. You can modify it as per your choice.

以下是结果图。希望对您有帮助。

Following is the resulting plot. Hope it helps you. Viel spass.

这篇关于matplotlib:将颜色分配给半径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆