用散点图叠加直方图列(Overlay columns of histograms with scatter plots)
我正在尝试将数据集可视化,并且我很早就陷入了困境,因为我不理解matplotlib的逻辑。
数据保存在两个格式相同的2d表中:
>>> data1 array([[ 1. , 1.384 ], [ 2. , 3.65218531], [ 3. , 3.17718531], [ 1. , 2.34513543], [ 2. , 3.43223443], [ 3. , 1.23333255], ... >>> data2 ...
我想在同一个图中可视化这些表格。
data1
表比data2
有更多的行,所以我的想法是用列式直方图显示data1
,用这样的点显示data2
:我知道如何制作数据的一维直方图,并且我知道如何制作散点图,但是如何获得彩色分区并用正确位置的散点图覆盖它们?
编辑:示例代码:
import pylab import numpy import matplotlib.pyplot as plt fig = plt.figure() data1 = numpy.array([[1,1.5],[2,1.7],[3,1.8],[1,1.2],[2,1.8],[3,2.2]]) data2 = numpy.array([[1,2.1],[2,1.3],[3,1.4],[1,1.1],[2,1.9],[3,2.25]]) plt.scatter(data1[:,0], data1[:,1]) plt.scatter(data2[:,0], data2[:,1], color="red") plt.show()
在实际代码中,
data1
数组包含许多条目,使其变得混乱。 因此希望'直方图'。I'm trying to visualize a data set and I'm stuck pretty early in the process because I don't understand the logic of matplotlib.
The data is kept in two 2d tables of the same format:
>>> data1 array([[ 1. , 1.384 ], [ 2. , 3.65218531], [ 3. , 3.17718531], [ 1. , 2.34513543], [ 2. , 3.43223443], [ 3. , 1.23333255], ... >>> data2 ...
I want to visualize these tables in the same plot. The
data1
table has many more rows thandata2
, so my idea is to displaydata1
with column-wise histograms anddata2
with points like this:I know how to make 1D histograms of data, and I know how to make scatterplots, but how do I get colored bins and overlay them with the scatterplots in the right positions?
Edit: Example code:
import pylab import numpy import matplotlib.pyplot as plt fig = plt.figure() data1 = numpy.array([[1,1.5],[2,1.7],[3,1.8],[1,1.2],[2,1.8],[3,2.2]]) data2 = numpy.array([[1,2.1],[2,1.3],[3,1.4],[1,1.1],[2,1.9],[3,2.25]]) plt.scatter(data1[:,0], data1[:,1]) plt.scatter(data2[:,0], data2[:,1], color="red") plt.show()
In the real code the
data1
array contains so many entries that it becomes a mess. Hence the wish for a 'histogram'.
原文:https://stackoverflow.com/questions/20621190
最满意答案
你可以试试这个代码:
file_input = open('mhyfile.txt','r') for line in file_input: #Your code here
for line in file_input:
loop中的for line in file_input:
读取文件。 但我在我的linux系统中测试并发现fileinput.input()不再使用内存。 我认为你应该提供更多关于你的问题的信息。一个可能的问题是您将太多文件写入磁盘并导致系统崩溃。 您可以尝试将选定的行写入单个文件并标记行号
j
。You can try this code:
file_input = open('mhyfile.txt','r') for line in file_input: #Your code here
The
for line in file_input:
loop will read the file line by line. But I test in my linux system and find fileinput.input() use no more memory. I think you should give more information about your problem.One possible problem is that you write too many files into your disk and cause the system crash. You can try to write the selected lines into one single file and mark the line number
j
.
相关问答
更多-
Python For loop 循环[2023-04-19]
python3 简化成: for x in range(6): print('*'*(x+1)) 一样的输出 -
你可以试试这个代码: file_input = open('mhyfile.txt','r') for line in file_input: #Your code here for line in file_input: loop中的for line in file_input:读取文件。 但我在我的linux系统中测试并发现fileinput.input()不再使用内存。 我认为你应该提供更多关于你的问题的信息。 一个可能的问题是您将太多文件写入磁盘并导致系统崩溃。 您可以尝试将选定的行写入单 ...
-
Tkinter不是线程安全的; 除了主线程外,您无法从任何地方访问Tkinter窗口小部件。 您需要重构代码,以便QueueHandler在主线程中运行。 Tkinter is not thread safe; you cannot access Tkinter widgets from anywhere but the main thread. You will need to refactor your code so that QueueHandler runs in the main thread. ...
-
我没有看到任何错误处理。 所以,任何出错都会导致脚本退出。 例如,如果Internet连接或Twitter发生故障,则urlopen调用将失败,整个脚本将停止工作。 这是添加错误处理最明显的地方,但是如果断开串口的连接,会发生什么? 添加错误处理来处理潜在的错误,否则Python将处理错误,并且您不会喜欢这种方式。 I don't see any error handling. So, anything that goes wrong will cause the script to exit. For e ...
-
第一个错误,“致命的Python错误:GC对象已经跟踪”于2013年关闭,状态为“CLOSED WONTFIX”请参阅Bugzilla的错误报告 在2015年[与dask相关]再次提出的临时解决方案似乎是使用此代码使用单个线程 : import dask dask.set_options(get=dask.async.get_sync) 但问题实际上存在于dataframe.read_csv问题。 这个问题最终在后来的熊猫版本中得到了解决 。 如果您升级matplotlib的版本,问题很可能也 ...
-
Python是否应该崩溃?(Is Python ever supposed to crash?)[2022-08-03]
当然,目标是像Python这样的东西永远不会崩溃。 唉,我们生活在一个不完美的世界。 我想问一个更有用的问题是“如果Python崩溃,我应该怎么做?”。 如果你想帮助建立一个更完美的世界,首先要快速搜索Python问题跟踪器,以查看是否有类似的问题已经被报告,并且可能在更新或尚未发布的Python版本中修复。 如果没有,请参阅是否可以找到重现问题的方法,并指明所涉及步骤的明确方向,适用的操作系统平台和版本,Python和第三方库的版本。 然后用所有细节开启一个新问题。 请记住,像许多开源项目一样,Pytho ... -
如何记录Python崩溃?(How to log a Python crash?)[2023-09-04]
如果进程如下所示,您可以将输出存储在文件中: python script.py >> /logdir/script.py.log 2>&1 You can store the output in a file, if the process is started like this: python script.py >> /logdir/script.py.log 2>&1 -
OpenCV 2.0安装程序附带的绑定有问题; 我有类似的问题,一些非常简单的操作触发崩溃。 从源代码编译应该修复它。 The bindings that come with the OpenCV 2.0 installer are buggy; I had similar problem where some very simple operations triggered crashes. Compiling from the source should fix it.
-
for循环崩溃python(for-loop crashes python)[2023-07-16]
如果time实际上是您要打印的值列表,那么这应该可以解决问题: for v in time: print v If time is in fact the list of values you want to print, then this should do the trick: for v in time: print v -
在setContentView上崩溃(Crash on setContentView)[2022-03-18]
有布局分辨率的问题,我多次面对这样的问题,更好地创建文件夹结构如下所示 layout-large layout-small layout-medium I found the issue: I just created different folders for landscape and portrait e.g -> layout-land layout-port But in Androidmanifest file i set the orientation as landscape. On ...