简体   繁体   English

添加颜色条以散布图或更改图类型

[英]Add colorbar to scatter plot or change the plot type

I am plotting some data that includes spatial (x, y) components as well as az component, which is the value of the measurement at that point in space. 我正在绘制一些数据,其中包括空间(x,y)分量以及z分量,这是空间中该点的测量值。 I was looking at the gallery, and I'm just not getting it. 我一直在寻找在画廊,和我只是没有得到它。 I think that what I want is a pcolormesh, but I don't understand what I need to put in for arguments. 我认为我想要的是pcolormesh,但是我不明白我需要在参数中添加什么。 I finally had success getting a scatter plot to do basically what I want, but it's less pretty than I want. 我终于成功地获得了一个散布图,基本上可以完成我想要的事情,但是它比我想要的要漂亮。 If I could figure out a way to make the points in the scatter plot bigger, I would be a lot happier with my plot. 如果我想出一种方法可以使散点图中的点变大,那么我对自己的图会更满意。 Furthermore, I am stuck on trying to add a legend - I only need the colorbar portion, since the end user doesn't really care about the X and Y dimensions. 此外,我坚持尝试添加图例-我只需要颜色栏部分,因为最终用户并不真正在乎X和Y尺寸。 Looking at the colorbar example, it seems that I need to add an axis, but I don't understand how I'm telling it that the axis I need is the Z axis. 看一下颜色条示例,似乎我需要添加一个轴,但是我不明白如何告诉我我需要的轴是Z轴。

x_vals = list(first_array[data_loc_dictionary['x_coord_index']][:])
y_vals = list(first_array[data_loc_dictionary['y_coord_index']][:])
y_vals = [-i for i in y_vals]
z_vals = list(first_array[data_loc_dictionary['value_index']][:])

plt.scatter(x_vals, y_vals, s = len(x_vals)^2, c = z_vals, cmap = 'rainbow')
plt.show()

Here is an example of what I am trying to duplicate: 这是我要复制的示例: 在此处输入图片说明 And here is what the code above produces: 这是上面的代码产生的内容: 在此处输入图片说明

  • I would like the second to look a little more like the first, ie, if there were a way to adjust the markers to be large enough to approximate that look, that would be ideal 我希望第二个看起来像第一个,也就是说,如果有一种方法可以将标记调整到足够大以逼近该外观,那将是理想的
  • I am struggling with creating a legend. 我正在努力创造一个传奇。 Colorbar seems to be the way to go, but I am not comprehending how to specify that it needs to be based on the Z values. Colorbar似乎是要走的路,但是我不理解如何指定它必须基于Z值。

Good catch with the ^2 - ^ 2的优势- 在此处输入图片说明

What about this basic example: 这个基本示例呢?

# generate random data
In [63]: x = np.random.rand(20)
In [64]: y = np.random.rand(20)
In [65]: z = np.random.rand(20)

# plot it with square markers: marker='s'
In [66]: plt.scatter(x, y, s=len(x)**2, c=z, cmap='rainbow', marker='s')
Out[66]: <matplotlib.collections.PathCollection at 0x39e6c90>

# colorbar
In [67]: c = plt.colorbar(orientation='horizontal')
In [68]: c.set_label('This is a colorbar')
In [69]: plt.show()

在此处输入图片说明

The Size of the points is given by 点的大小由下式给出

s : scalar or array_like, shape (n, ), optional, default: 20

    size in points^2.

I see no reason why s=len(x)**2 is a good choice by default. 我看不出为什么默认情况下s=len(x)**2是一个不错的选择。 I would play around with it according to your preference. 我会根据您的喜好处理它。

In case you want to know how to replicate your initial example image with pcolormesh, I would do: 如果您想知道如何使用pcolormesh复制初始示例图像,我可以这样做:

import numpy as np
import matplotlib.pyplot as plt

f, ax = plt.subplots(figsize=(6, 5))
grid = np.arange(-5, 6)
x, y = np.meshgrid(grid, grid)

z = np.random.randn(len(x), len(y)) 
mask = (np.abs(x) + np.abs(y)) > 4
z = np.ma.masked_array(z, mask)

mesh = ax.pcolormesh(x - .5, y - .5, z, cmap="coolwarm", vmin=-3, vmax=3)
plt.colorbar(mesh)

To produce: 生产:

在此处输入图片说明

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

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