I am trying to create a Control Variability Grid Analysis in Python. This analysis requires plotting min and max blood glucose values (in this case, my own) per day on a scatterplot, with a background image that helps classify the overall glucose control. See here.
I have found other threads that address this question, but I cannot seem to get it to work for my application. I need the axes to be a specific range, and I think that is interfering with the image. The code I have tried so far, based on this page and this page, is as follows:
JavaScript
x
8
1
img = plt.imread('CVGA.png')
2
fig, ax = plt.subplots(figsize = (12,6))
3
sns.scatterplot(data=min_max_day,x=min_max_day['Glucose Value (mg/dL)']['amin'],
4
y=min_max_day['Glucose Value (mg/dL)']['amax'], zorder=1)
5
plt.xlim(110,40)
6
plt.ylim(110,400)
7
ax.imshow(img);
8
And the output looks like this:
With the line “ax.imshow(img)” removed, the output looks like this:
Additional details:
- the image I’m using is 1200×1200:
- Maybe there is a problem with the fact that the scale I’m using isn’t a perfect square? Not sure if that makes sense, but clearly 110 to 400 on the Y axis and 100 to 40 on the X axis would not make a perfect square? I am unsure what to do about that.
Any advice or help would be greatly appreciated. Thanks a lot.
Advertisement
Answer
- Use the
extent
parameter as shown in Change values on matplotlib imshow() graph axis, but you must also useaspect='auto'
and setfigsize = (12, 12)
(or(6, 6)
, etc.).- See all parameters at
matplotlib.pyplot.imshow
- See all parameters at
sns.scatterplot
from the OP is commented out because no data was provided.- Tested in
python 3.8
,matplotlib 3.4.2
, andseaborn 0.11.1
JavaScript
1
14
14
1
import matplotlib.pyplot as plt
2
import seaborn as sns
3
import numpy as np # sample data
4
5
img = plt.imread('CVGA.png')
6
fig, ax = plt.subplots(figsize=(6, 6))
7
# sns.scatterplot(data=min_max_day, x=min_max_day['Glucose Value (mg/dL)']['amin'], y=min_max_day['Glucose Value (mg/dL)']['amax'], zorder=1)
8
9
sns.scatterplot(x=np.linspace(110, 41, 10), y=np.linspace(110, 401, 10), ax=ax)
10
plt.xlim(110, 40)
11
plt.ylim(110, 400)
12
13
ax.imshow(img, extent=[110, 40, 110, 400], aspect='auto')
14