Quantcast
Channel: The Stata Blog - Latest Comments
Viewing all articles
Browse latest Browse all 1126

Re: Stata/Python integration part 5: Three-dimensional surface plots of marginal predictions

$
0
0

As a Mac user, I had a problem with backend part, but I found that the following works just as well:

python
import pandas as pd
data = pd.read_stata("predictions.dta")
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.plot_trisurf(data['age'], data['weight'], data['pr_highbp'], cmap=plt.cm.Spectral)
ax.set_xticks(np.arange(20, 90, step=10))
ax.set_yticks(np.arange(40, 200, step=40))
ax.set_zticks(np.arange( 0, 1.2, step=0.2))
ax.set_title("Probability of Hypertension by Age and Weight")
ax.set_xlabel("Age (years)")
ax.set_ylabel("Weight (kg)")
ax.zaxis.set_rotate_label(False)
ax.set_zlabel("Probability of Hypertension", rotation=90)
ax.view_init(elev=30, azim=240)
plt.show()
end


Viewing all articles
Browse latest Browse all 1126

Trending Articles