I am creating a Python script that takes stock data and generates a graph for that but the problem I am facing is stock data is not available for every Saturday and Sunday so there is some gap in dates. So the graphs looks like this:
As you can see here there is a lot of gap between this. What I want is to remove this gap which means removing unnecessary date which is automatically generated by matplotlib and doesn't have any data making it look like this. Does anybody know how do to do that I trying everything from 3 days but didn't get any solution for this.
My code:
import pandas as pd
import pytz
import matplotlib.pyplot as plt
import json
data = json.loads(open("data.json", "r").read())
local_timezone = pytz.timezone("Asia/Kolkata")
data = data[:501]
# Extracting relevant data and creating a pandas DataFrame
timestamps = [
pd.to_datetime(row["Data"][2]["ScalarValue"])
for row in data
]
values = [float(row["Data"][3]["ScalarValue"]) for row in data]
df = pd.DataFrame({"Timestamps": timestamps, "Values": values})
df.set_index("Timestamps", inplace=True)
plt.figure(figsize=(12, 6))
plt.plot(df.index, df["Values"], label="Original Data")
plt.xlabel("Timestamps")
plt.ylabel("Price")
plt.title(
"Stock Price"
)
plt.legend()
plt.grid(True)
plt.show()
data.json
file:
[
{
"Data": [
{
"ScalarValue": "Nifty Bank"
},
{
"ScalarValue": "price"
},
{
"ScalarValue": "2023-01-20 06:58:00.000000000"
},
{
"ScalarValue": "42644.2"
}
]
},
{
"Data": [
{
"ScalarValue": "Nifty Bank"
},
{
"ScalarValue": "price"
},
{
"ScalarValue": "2023-01-20 06:59:00.000000000"
},
{
"ScalarValue": "42642.25"
}
]
},
...
]