首页 \ 问答 \ 用散点图叠加直方图列(Overlay columns of histograms with scatter plots)

用散点图叠加直方图列(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 than data2, so my idea is to display data1 with column-wise histograms and data2 with points like this: Example

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
更新时间:2023-12-08 15:12

最满意答案

你可以试试这个代码:

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.

相关问答

更多
  • 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这样的东西永远不会崩溃。 唉,我们生活在一个不完美的世界。 我想问一个更有用的问题是“如果Python崩溃,我应该怎么做?”。 如果你想帮助建立一个更完美的世界,首先要快速搜索Python问题跟踪器,以查看是否有类似的问题已经被报告,并且可能在更新或尚未发布的Python版本中修复。 如果没有,请参阅是否可以找到重现问题的方法,并指明所涉及步骤的明确方向,适用的操作系统平台和版本,Python和第三方库的版本。 然后用所有细节开启一个新问题。 请记住,像许多开源项目一样,Pytho ...
  • 如果进程如下所示,您可以将输出存储在文件中: 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.
  • 如果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
  • 有布局分辨率的问题,我多次面对这样的问题,更好地创建文件夹结构如下所示 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 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。