Python 将y轴点匹配在一起
我已经成功地从一个目录中读取了数据,并且我已经绘制了我需要的图形。然而,我还需要一件事。我想通过“半光半径”将不同的“标准偏差”值与“数字”对应起来。在所示的图表中,“数字”不是图表上的轴,但是,在这种情况下,将有十个“数字9”。我想找到一种方法,将这些编号相同的点与某种类型的线进行匹配,如下图所示(我只是随机画了几条线,让您了解我想要什么) 在本例中,假设每一个点(其中一条绘制的线)具有相同的“数字”。一个“数字”点将具有10个不同的“标准偏差”值(1到10)和10个不同的“半光半径”值,这些值是我希望匹配的值。我已将读取/打印代码粘贴到下面。我该怎么做Python 将y轴点匹配在一起,python,graph,matplotlib,Python,Graph,Matplotlib,我已经成功地从一个目录中读取了数据,并且我已经绘制了我需要的图形。然而,我还需要一件事。我想通过“半光半径”将不同的“标准偏差”值与“数字”对应起来。在所示的图表中,“数字”不是图表上的轴,但是,在这种情况下,将有十个“数字9”。我想找到一种方法,将这些编号相同的点与某种类型的线进行匹配,如下图所示(我只是随机画了几条线,让您了解我想要什么) 在本例中,假设每一个点(其中一条绘制的线)具有相同的“数字”。一个“数字”点将具有10个不同的“标准偏差”值(1到10)和10个不同的“半光半径”值,这些
newvid = asciitable.read('user4.cat')
n_new = newvid['n']
re_new = newvid['re']
number = newvid['number']
standard_deviation = newvid['standard_deviation']
plt.title('sersic parameter vs. standard deviation distribution of noise')
plt.xlabel('standard deviation')
plt.ylabel('sersic parameter')
plt.xlim(0,12)
plt.ylim(0,5)
plt.scatter(standard_deviation, n_new)
plt.show()
plt.title('half-light radius vs. standard deviation distribution of noise')
plt.xlabel('standard deviation')
plt.ylabel('half-light radius')
plt.xlim(0,12)
plt.ylim(-2,15)
plt.scatter(standard_deviation,re_new)
plt.show()
要执行我认为您需要的操作,您必须使用
绘图
功能,而不是分散
,以便连接线路。根据数据的排列方式,可能需要对数据进行拆分或排序,以便一次绘制每个数字的所有点,并按标准偏差排序
试试这个:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
newvid = asciitable.read('user4.cat')
n_new = newvid['n']
re_new = newvid['re']
number = newvid['number']
std_dev = newvid['standard_deviation']
n_max = float(number.max()) # for coloring later
plt.figure()
plt.title('sersic parameter vs. standard deviation distribution of noise')
plt.xlabel('standard deviation')
plt.ylabel('sersic parameter')
plt.xlim(0,12)
plt.ylim(0,5)
for n in np.unique(number):
n_mask = number == n # pick out just where n_new is the current n
order = np.argsort(std_dev[n_mask]) # sort by std_dev, so line doesn't zig zag
plt.plot(std_dev[n_mask][order], n_new[n_mask][order],
label=str(n), color=cm.jet(n/n_max)) # label and color by n
plt.legend()
plt.show()
plt.figure()
plt.title('half-light radius vs. standard deviation distribution of noise')
plt.xlabel('standard deviation')
plt.ylabel('half-light radius')
plt.xlim(0,12)
plt.ylim(-2,15)
# do one plot per number
for n in np.unique(number):
n_mask = number == n # pick out just where n_new is the current n
order = np.argsort(std_dev[n_mask]) # sort by std_dev, so line doesn't zig zag
plt.plot(std_dev[n_mask][order], re_new[n_mask][order],
label=str(n), color=cm.jet(n/n_max)) # label and color by n
plt.legend()
plt.show()
随机数据:要使用颜色栏而不是图例,请执行以下操作:
m = cm.ScalarMappable(cmap=cm.jet)
m.set_array(number)
plt.colorbar(m)
这太棒了,谢谢!你知道如何在绘图中增加一条随机的“线”吗?是的,使用线宽选项:在
plot
命令中使用lw=2
或lw=3
。你真的是指随机的,还是一个特定的?如果您愿意,可以说n=3
special,例如lw=3 If n==3 else 1
。如果你真的想随机做一个,只需事先随机挑选n
:nbold=np.random.choice(number)
,然后lw=3如果n==nbold else 1
谢谢你,效果很好!还有一个简单的问题:我的钥匙里有42件东西。有没有办法使按键可读,因为目前屏幕本身在28左右的位置切断按键。@vdogsandman您可以在图例选项中更改字体大小和间距,请参阅plt.legend()
@vdogsandman的文档,使用图例可能不是最好的解决方案。colorbar
可以工作,但如果您还没有使用图像打印,则实现起来有点棘手。