Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib error frames plotted separately when RPY2 imports package

After I installed RPY2 (v. 2.7.8) and the accompanying dependencies on my computer (Windows 10, Python 2.7 x86), Matplotlib (v. 1.5.3) seems to have been damaged. With even the simplest plots, the frame and the window are depicted separately from each other:

series1  = pd.Series(np.arange(500))
series2 = series1*-1
plt.plot(series1,series2)
# OUT: [<matplotlib.lines.Line2D object at 0x185F4A50>]
plt.show()

enter image description here

This goes wrong with the following imports.

# general
import os
from os import path # necessary for check file-overwrite
import calendar
import datetime
import csv
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import imp

# cvxEDA
from scipy import signal
citi = imp.load_source('cvxeda', 'D:\\Python27\\Lib\\cvxEDA\\src\\cvxEDA.py')

#RPY and RHRV
import rpy2
import rpy2.robjects as robjects
import rpy2.robjects.packages as rpackages
from rpy2.robjects.packages import importr# This step is importing the R package in the embedded R,
# and is exposing all R objects in that package as Python objects
# import R's "base" package
base = importr('base')
# import R's "utils" package
utils = importr('utils')
# select a mirror for R packages
utils.chooseCRANmirror(ind=1) # select the first mirror in the list
rhrv = importr('RHRV')

Update:
I've traced the problem down to the following import rhrv = importr('RHRV'). RHRV is a heart rate analysis package of R, which includes its own plotting functions (for R). That might be the troubling issue.

Has anyone encountered this issue before? What is going wrong, and how can I solve it?

like image 964
Robin Kramer Avatar asked Apr 05 '26 19:04

Robin Kramer


1 Answers

I have contacted the developers of the R-package RHRV about the issue. They told me that

RHRV makes use of the tkrplot library, which makes use of Tk widgets. Maybe this is interferring with your python module.

Although I am unable to find that matplotlib indeed uses Tk widgets, I did find that matplotlib cán be combined with Tk (e.g. http://matplotlib.org/examples/user_interfaces/embedding_in_tk). This is likely the cause of the plotting issues.

The only solution I can think of is to not plot when you have imported RHRV. First calculate and save the data. Then, in another script you can make the plots. Alternatively, there are HRV analysis packages specifically for Python (gHRV or hrv). However, I could not get gHRV to work, and the hrv package is, as of yet, not complete.

like image 102
Robin Kramer Avatar answered Apr 08 '26 09:04

Robin Kramer