简体   繁体   English

在 Python 中创建一个表示变量如何沿 x 方向变化的颜色编码图形。 (类似于彩色地图)

[英]Creating a colour coded figure which represents how a variable changes along the x-direction in Python. (similar to a colour map)

I have to create a colour coded figure of how the thickness of a beam varies along the beam.我必须创建一个颜色编码图,说明梁的厚度如何沿梁变化。 For example a figure like this:例如这样的图:

[Beam schematic with coloured blocks representing the different sections of the beam][1] [1]: https://i.stack.imgur.com/3GZCp.png The coloured blocks in the figure have to be colour coded like in a contour plot; [光束示意图,彩色块代表光束的不同部分][1] [1]:https://i.stack.imgur.com/3GZCp.png 图中的彩色块必须像在轮廓 plot; so for example, a darker shade would indicate a thicker part of a beam and vice versa.例如,较暗的阴影表示光束的较厚部分,反之亦然。 The key to the colour code should also be provided with the resulting figure.颜色代码的关键也应与结果图一起提供。 The 'colour blocks' essentially represent sections of the beam, which is stored in an array. “色块”基本上代表了光束的各个部分,这些部分存储在一个阵列中。 The corresponding thicknesses of the sections are also stored in an array.相应的截面厚度也存储在一个数组中。

import numpy as np

beamsectionsx = np.array([ 0.   ,  2.65 ,  6.25 , 10.45 , 14.05 , 17.25 , 20.05 , 23.05 ,
       26.05 , 29.05 , 32.12 , 34.72 , 37.32 , 40.92 , 44.72 , 48.52 ,
       52.32 , 55.42 , 58.512, 61.612, 63.702, 63.825, 64.02, 66.62 ]) # points of the sections along the x-axis

thicknessy = np.array([0.4, 0.099, 0.099,0.099,0.1,0.099, 0.079,0.079, 0.079, 0.081, 0.084, 0.088, 0.091, 0.092, 0.095, 0.094, 0.093, 0.09, 0.082, 0.068, 0.068, 0.075, 0.095]) 
# thicknesses of the respective sections. For eg: thickness between 0m and 2.65m (from beamsectionsx), would correspond to 0.4 and so on. 



I tried creating colourbars to represent this, but instead of representing the span of the beam in the x-direction (0-66.62m), the colourmap represents 0-24 instead - this corresponds to the length of the array respectively.我尝试创建颜色条来表示这一点,但不是表示光束在 x 方向 (0-66.62m) 上的跨度,而是颜色图表示 0-24 - 这分别对应于数组的长度。

Any input on creating the figure in a similar manner to the example figure shown above would be helpful.以与上面显示的示例图类似的方式创建图的任何输入都会有所帮助。

Thank you.谢谢你。

I was able to create the plot with a colorbar as follows: https://i.stack.imgur.com/DxtVr.png我能够使用如下颜色条创建 plot: https://i.stack.imgur.com/DxtVr.png

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.colors import Normalize

beamsectionsx = np.array([ 0.   ,  2.65 ,  6.25 , 10.45 , 14.05 , 17.25 , 20.05 , 23.05 ,
       26.05 , 29.05 , 32.12 , 34.72 , 37.32 , 40.92 , 44.72 , 48.52 ,
       52.32 , 55.42 , 58.512, 61.612, 63.702, 63.825, 64.02, 66.62 ]) # points of the sections along the x-axis

thicknessy = np.array([0.4, 0.099, 0.099,0.099,0.1,0.099, 0.079,0.079, 0.079, 0.081, 0.084, 0.088, 0.091, 0.092, 0.095, 0.094, 0.093, 0.09, 0.082, 0.068, 0.068, 0.075, 0.095])
# thicknesses of the respective sections. For eg: thickness between 0m and 2.65m (from beamsectionsx), would correspond to 0.4 and so on.

# Colormap of choice
cmap = plt.cm.plasma

# Plot bars
fig, ax = plt.subplots(1, 1)
for x0, x1, y in zip(beamsectionsx[:-1], beamsectionsx[1:], thicknessy):
    ax.fill_between([x0, x1], [y, y], color=cmap(y / max(thicknessy)))

# Set colorbar with label
cbar = plt.colorbar(mappable=plt.cm.ScalarMappable(norm=Normalize(0, max(thicknessy)), cmap=cmap))
cbar.set_label('Beam thickness')

fig.savefig('result.png')

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

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