matplotlib:了解并更改散点图的轴标签,并逐步更新 [英] matplotlib: Understanding and changing axis labels for a scatter plot updated incrementally

查看:58
本文介绍了matplotlib:了解并更改散点图的轴标签,并逐步更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉 - 我承认我不知道如何以一种非常清晰的方式提出这个问题.

Sorry - I admit that I don't know how to ask this question in a really clear way.

文档和之前的 stackoverflow 问题都有不同的观点,似乎在某种程度上触及了这个问题,但它没有以任何方式组织或结构化,我可以理解我正在努力实现的目标,而且StackOverflow问题似乎是如此狭窄,以至于很难知道它们是否适用.

Both the documentation and previous stackoverflow questions have various points that seem in some way to touch on this problem but it's not organised or structured in any way that I can make sense of it with respect to what I'm trying to achieve, and the StackOverflow questions seem to be so narrow that it's hard to know if they even apply.

我有一个脚本,可以为硬件设备生成的数据生成散点图.标绘数据生成步骤在硬件上的不同数据源之间循环,以将它们标绘在散布图上.(原剧本是另一个人写的,不再是我们组的).据我了解,它正在将数据转储到一个匿名散点图对象中——这大概是同一个对象——因为它所做的(以及我想要它做的)是将所有来源的数据收集到同一个散点图上.

I have a script that generates scatter plots for data being generated by a hardware device. The plot data generation step cycles through different sources of data on the hardware to plot them on the scatter. (The original script was written by another person, no longer with our group). As I understand it, it's dumping the data into an anonymous scatter plot object - which presumably is the same one - because what it does (and what I want it to do) is collect the data from all the sources onto the same scatter plot.

但现在我想改变标签沿轴的大小——我能找到的所有在线信息似乎都表明你必须通过创建绘图的命名实例并通过修改它来做到这一点实例.但是,我不知道如何确保将每个连续来源中的所有数据放置在同一实例中,因此,当显示最终绘图时,我会在一个绘图中获得所有来源的散布图.我将如何执行此操作(然后修改关联的轴文本)?

But now I want to change the size of the labels along the axis - and all the on-line information I can find seems to suggest that you have to do this by creating a named instance of the plot and modifying it through the instance. However I don't know how to make sure that all the data from each successive source is placed into the same instance, so that when the final plot is displayed I get the scatter for all sources in one plot. How would I do this (and then modify the associated axis text)?

实际的情节线是:

id_accumulator=0
for i in range(len(pfc)):
    data = numpy.asarray(pfc[i].getSpikes())
    if len(data) > 0:
       pylab.scatter(data[:,0], data[:,1] + id_accumulator, color='green', s=4) # s=1
    id_accumulator = id_accumulator + pfc[i].size
pylab.show()

比这个具体的例子更一般地说,我将如何设置以便我可以拥有,对于 X 个数据源,Y 个单独的图,每个图都显示点 [S[x][p]] 的一些子集,其中 S 是一组数据源,x 是一个源,p 是一个数据点,然后我将每个轴的标签设置为任意大小(大概是字体)?

Speaking more generally than this specific example, how would I set things up so that I can have, for X data sources, Y separate plots each displaying some subset of points [S[x][p]] where S is the set of data sources, x is a source and p is a data point, where I then set the labels for each axis to any size (and presumably font)?

推荐答案

我已经用每行的内容注释了该代码段:

I have annotated that snippet of code with what each line is doing:

id_accumulator = 0   # set up counter
for i in range(len(pfc)):  # loop over input data array
    data = numpy.asarray(pfc[i].getSpikes())  # get data from hardware?
    if len(data) > 0:  # if we have some data
       # plot it via pylab.scatter -> pyplot.scatter -> pyplot.gca().scatter
       pylab.scatter(data[:,0], data[:,1] + id_accumulator, color='green', s=4) # s=1
    id_accumulator = id_accumulator + pfc[i].size   # increment 
pylab.show()  # after the loop, show the window using pylab.show -> pyplot.show

原始代码具有一些病理导入/命名方案.请参阅推荐的绘图方式: matplotlib or pylab? 解释 matplotlib vs pylab vs pyplot.简短的是,您不应该将 pylab 用作导入的模块(至少有计划摆脱它).

The original code has some pathological imports/naming schemes. See Which is the recommended way to plot: matplotlib or pylab? for an explaination of matplotlib vs pylab vs pyplot. The short of it is you should not be using pylab as an imported module (at a minimum, there are plans to get rid of it).

我将如何重写这段代码:

How I would re-write this code would be:

import matplotlib.pyplot as plt  # standard import
import numpy as np

fig, ax = plt.subplots(1, 1)     # make axes and figure objects
# do what ever axes level setup you want
ax.set_xlabel('text')

# iterate over input data
id_accumulator = 0
for _pfc in pfc:
    # get the data
    data = np.asarray(_pfc.getSpikes())
    if len(data) > 0:    # there should be a better check for this
        # plot the data
        ax.plot(data[:0], data[:1] + id_accumulator, 
                color='g', marker='o', linestyle='none')
    id_accumulator += _pcf.size   # update the offset count


# show the figure window

plt.show()

除非您打算改变数据集中标记的颜色或大小,否则最好使用不带线条的 plot.

Unless you plan to vary either the color or size of your markers within a data set, you are better off using plot with no line.

如果要使用许多 axes ,只需制作更多轴对象即可.如果您想要单独的图形窗口

If you want many axes to play with, just make more axes objects. If you want separate figure windows

fig1, ax1 = plt.subplots(1, 1)
fig2, ax2 = plt.subplots(1, 1)

或者如果每个图形需要多个轴:

or if you want many axes per figure:

fig, ax_lst = plt.subplots(2, 2)

这将为您提供2x2的网格.ax_lst 是轴对象的 2x2 ndarray.由此,您可以进行所需的数据源<->轴的任何排列.

which will give you a 2x2 grid. ax_lst is a 2x2 ndarray of the axes objects. From this you can do what ever permutation of data source <-> axes you want.

这篇关于matplotlib:了解并更改散点图的轴标签,并逐步更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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