How do I render Python plots in RMarkdown? - python

For the life of me, I can't get to render plots in RMarkdown using Python. The plots display in my Python Editor, but not when I switch to RStudio trying to build a PDF/HTML report. I've been all over the internet, everyone has an answer, but none has the solution to my issue. Here's the error message I get the attached error below:
Below I made up some data to show what I'm trying to achieve. Once the application hits the seaborn line it produces the attached error message in RStudio
knitr::opts_chunk$set(echo = TRUE)
library(reticulate)
Importing Required Packages
import pandas as pd
import numpy as np
from scipy.stats import uniform # for training-and-test split
import statsmodels.api as sm # statistical models (including regression)
import statsmodels.formula.api as smf # R-like model specification
import matplotlib.pyplot as plt # 2D plotting
import seaborn as sns
x = np.random.normal(0, 1, 20)
y = np.random.normal(0, 2, 20)
sns.scatterplot(x, y)

I found the answer here
All I needed was to add these two lines in the R setup chunk below the library(reticulate) call
matplotlib <- import("matplotlib")
matplotlib$use("Agg", force = TRUE)

Related

Result image not showing in VSCode

Im trying to visualize the data using python in VSCode, the code is down below
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
train_df = pd.read_csv('D:/GBM project/train_labels.csv')
plt.figure(figsize = (5, 5))
sns.countplot(data = train_df, x = 'MGMT_value')
This code run successfully but no image is shown, could someone tells me where's the problem?
Thanks in advance!
Maybe it's closing after running successfully. If you're not getting any exceptions after code execution, try following things
add temp=input() at the end of the program
add plt.show() at the end of the program
add sns.set_theme() before last line

Seaborn Plot is not displaying

I have been trying to plot a simple bar chart using Seaborn. Oddly enough the previous plots worked, but these ones do not appear. No error is being thrown and as far as I can tell the code is okay. Perhaps a more experienced eye will be able to find an error.
import pandas as pd
import numpy as np
import pyodbc
import matplotlib.pyplot as plt
import seaborn as sns
region_pct_25 = region_totals.iloc[0:6, :]
region_pct_25['Opp_Lives_pct'] = ((region_pct_25.Lives / region_pct_25.Lives.sum())*100).round(2)
region_pct_25.reset_index(level=['RegionName'], inplace=True)
region_25_plt = sns.barplot(x="RegionName", y="Opp_Lives_pct", data=region_pct_25, color = 'g').set_title("Client Usage by Region: ADD ")
plt.show()
No plot is showing. Please help wherever you can!
adding %matplotlib inline to the preamble resolved the issue!

Why can't I create a 3d scatter plot in Python on my desktop?

I am trying to use the 3D scatter plot object in python. And I have successfully done this on my laptop. However, I can not copy and paste code onto my desktop. When I do this I get an error. I will attach my the section of my code below that is giving me trouble. I am using Anaconda to run my code. I will note that my laptop uses python 3.6 and my desktop uses 3.7, but I do not think that is causing it. The error I is get is as follows. "ValueError: Unknown projection '3d'"
import numpy as np
from scipy import optimize
import time
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import preprocessing
from sklearn.svm import SVR
import multiprocessing as mp
from obj_class import objective_class
import pdb
import scipy.integrate as integrate
def create3d():
grid_matrix = np.array([[1,1,1,1],[2,2,2,2],[3,3,3,3]])
fig = plt.figure()
ax = plt.axes(projection='3d')
p = ax.scatter3D(grid_matrix[:,0],grid_matrix[:,1] ,grid_matrix[:,2] , c=grid_matrix[:,3], cmap='viridis')
cb = fig.colorbar(p)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title(' Scatter Plot')
In order to use a 3d projection in matplotlib <= 3.1 you need to import axes3d, i.e.
from mpl_toolkits.mplot3d import Axes3D
From matplotlib >= 3.2, no extra import is necessary. So possibly you are running different matplotlib versions on both computers.
If you are running your code within an iPython kernel, Jupyter notebook for example,
then you only need to perform each import once and you will be able to run any code which relies on said import until the kernel is shutdown. However, in order to run the script in a self contained fashion you will need that import included in your script.

Transform some kind of exponential distribution into normal distribution

I have the following exponential distribution, generated with the following code:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import random
np.random.seed(1)
tags_ratio = np.random.exponential(1/25, 1000)
plt.hist(tags_ratio, range=(0, 1), bins=100)
plt.show()
I'm trying to transform my data, resides in tags_ratio into normal distribution, but with no success.
Tried with the log function and square functions. it given decent results. But I'm interesting in more ideas. Maybe more sophisticated.
You can try to see if this helps:
from scipy.stats import boxcox
tags_ratio = boxcox(tags_ratio, 0.3)
plt.hist(tags_ratio)
plt.show()
result:
for more explanations and theory about Box-Cox click here.

Unable to use seaborn plotting features in Visual Studio

I've written code to read in certain data from a CSV file, perform a PCA analysis on the data with the sklearn library, and then plot the resulting data as a heatmap. The code doesn't show any errors when run, but it also outputs no graph and just a line saying AxesSubplot(0.125,0.11;0.62x0.77).
I'm wondering if Visual Studio is unable to display plots like this and if so what would be a better IDE for me to use for this project. If not can anyone see a problem that would prevent this code from displaying a heatmap? Copying the relevant code below
import os
import matplotlib as mpl
if os.environ.get('DISPLAY','') == '':
print('no display found. Using non-interactive Agg backend')
mpl.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
sns.set()
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
import pandas as pd
# Scaling the data for PCA
scaler = StandardScaler()
x = StandardScaler().fit_transform(data)
pca = PCA(n_components = 2)
pca.fit(x)
finSet = pca.transform(x)
hm = sns.heatmap(finSet)
plt.show()
You may delete the lines
if os.environ.get('DISPLAY','') == '':
print('no display found. Using non-interactive Agg backend')
mpl.use('Agg')
Those lines are intendet to query if the code is run in an environment which has a display. If no display is there, it should not try to create a plotting window on screen.
However, even if you have a display, but os.environ does not have a "DISPLAY" key, the code will falsely assume that no plotting window shall be created. This seems to be the case on Windows at least.
You might also want to inform the source of this code about their error.

Categories

Resources