简体   繁体   English

如何制作一维图,其中值由颜色表示,而不是在 y 轴上?

[英]How to make 1-dimensional plot, where values are represented by colors, not on y-axis?

I'm trying to generate a plot in matplotlib, which would show what kind of mixing type is dominant in different section of a star (1-dimensional section).我正在尝试在 matplotlib 中生成一个图,这将显示在恒星的不同部分(一维部分)中哪种混合类型占主导地位。 Data is loaded from file, and mixing types are represented by integers (from 1 to 6).数据从文件中加载,混合类型用整数表示(从 1 到 6)。 I would like to show it, not on standard 2-dimensional plot, but as a colorbar, where values of parameter are shown not as y value, but as colors on x-axis.我想显示它,不是在标准的二维图上,而是作为颜色条显示,其中参数值不显示为 y 值,而是显示为 x 轴上的颜色。

There are many ways to creates such plots.有很多方法可以创建这样的图。 Depending on how many plots and how many x values you want to show you might prefer a different approach.根据您想要显示的图的数量和 x 值的数量,您可能更喜欢不同的方法。 If there is a clear hierarchy in the values, this can be reflected in the colors chosen.如果值中有清晰的层次结构,这可以反映在选择的颜色中。

Here is an example using imshow() to get you started.这是一个使用imshow()帮助您入门的示例。

import numpy as np
import matplotlib.pyplot as plt

x = np.random.randint(1, 7, 25)
cmap = plt.cm.get_cmap('inferno', 6)
plt.imshow(x.reshape(1, -1), cmap=cmap, vmin=1, vmax=6)
for i in range(len(x)):
    plt.text(i, 0, x[i], ha='center', va='center', color='navy' if x[i] >= 5 else 'white')
plt.xticks(range(len(x)))
plt.gca().yaxis.set_visible(False)
plt.show()

示例图

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

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