'How do I change the frequency of X axis numbers for a scatterplot using Matplotlib and Python?
I'm trying to make a graph of COVID-19 cases in march of 2020. The graph skips numbers by 5 (5, 10, 15 etc.) Here is the graph:

I tried this: plt.xticks(np.arange(0, len(x)+1, 1)) but it skipped to 2, and ended early at 29.
Here is my code:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
df = pd.read_csv("CasesByMonth.csv")
x = df['date']
y = df['newCases']
plt.xticks(rotation=60)
plt.scatter(x, y)
x = df['date'].astype(float)
y = df['newCases'].astype(float)
slope, intercept, r, p, std_err = stats.linregress(x, y)
def myfunc(x):
return slope * x + intercept
mymodel = list(map(myfunc, x))
plt.plot(x, mymodel)
plt.show()
Solution 1:[1]
I found the answer, you just need to use: plt.xticks(np.arange(min(x), max(x)+1, 1.0))
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 | FireFlower |
