'How can I eliminate this disproportionate vertical gap between my x-axis and data in matplotlib? [duplicate]
I have been testing a regression model by graphing its output in matplotlib, but when I do I end up getting a really tall graph with a gap in the bottom.
The code that generates this graph:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import sys
!{sys.executable} -m pip install --upgrade MatrixModule
import matrixmodule as mm
%matplotlib inline
df = pd.read_csv('monthly_in_situ_co2_mlo.csv',names=['date','CO2 ppm'])
df.head()
x = np.linspace(1958,2023,800)
ax = fig.add_subplot(1, 1, 1)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
y1=eval(mm.regression(np.array(df),model="nth")[1])
y2=eval(mm.regression(np.array(df),model="exp")[1])
y3=eval(mm.regression(np.array(df),model="pwr",type="ls")[1])
y4=eval(mm.regression(np.array(df),model="lin",type="robust")[1])
plt.plot(x,y1, 'r')
plt.plot(x,y2, 'g')
plt.plot(x,y3, 'y')
plt.plot(x,y4, 'c')
data = np.array(df)
xd, yd = data.T
plt.scatter(xd,yd,s=3)
plt.show()
I'm in a Jupyter notebook, hence the odd import of my module. The issue is the same in other IDEs though.
I have tried messing with ax.spines['bottom']
, by changing its position or setting it invisible, but nothing changes. I'm fairly new to matplotlib so I'm not sure what I am doing wrong. How can I get rid of the large gap between the top of the graph (where the actual data is) and the bottom axis?
Solution 1:[1]
It looks like your adding the subplot correctly. This should work,
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import sys
!{sys.executable} -m pip install --upgrade MatrixModule
import matrixmodule as mm
%matplotlib inline
df = pd.read_csv('monthly_in_situ_co2_mlo.csv',names=['date','CO2 ppm'])
df.head()
x = np.linspace(1958,2023,800)
fig, ax = plt.subplots()
y1=eval(mm.regression(np.array(df),model="nth")[1])
y2=eval(mm.regression(np.array(df),model="exp")[1])
y3=eval(mm.regression(np.array(df),model="pwr",type="ls")[1])
y4=eval(mm.regression(np.array(df),model="lin",type="robust")[1])
ax.plot(x,y1, 'r')
ax.plot(x,y2, 'g')
ax.plot(x,y3, 'y')
ax.plot(x,y4, 'c')
data = np.array(df)
xd, yd = data.T
ax.scatter(xd,yd,s=3)
plt.show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ocean |