I want to create 3 subplots below with the subplot with the coordinates stated in the for loop parameters as add_plot
. The format of add_plot
is nrows, ncols ,cells
. But I get an error when I try to implement it. How can I modify the contents of the for loop within Graphing()
to achieve this?
Error:
ValueError: Single argument to subplot must be a three-digit integer, not (2, 2, 1)
Code:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
data = pd.DataFrame({'col1': [4, 5, 2, 2, 3, 5, 1, 1, 6], 'col2': [6, 2, 1, 7, 3, 5, 3, 3, 9],
'label':['Old','Old','Old','Old','Old','Old','Old','Old','Old'],
'date': ['2022-01-24 10:07:02', '2022-01-27 01:55:03', '2022-01-30 19:09:03', '2022-02-02 14:34:06',
'2022-02-08 12:37:03', '2022-02-10 03:07:02', '2022-02-10 14:02:03', '2022-02-11 00:32:25',
'2022-02-12 21:42:03']})
def Graphing():
#Size of the figure
fig = plt.figure(figsize=(12, 7))
#Creating the dataframe
df = pd.DataFrame({
'date' : datetime,
'col1': data['col1']
})
for subplot_,add_plot in (zip(
['sub1','sub2','sub3'],
[(2,2,1), (2,2,1), (2,1,2)])):
subplot_ = fig.add_subplot(add_plot)
# Show Graph
plt.show()
Graphing()
CodePudding user response:
This still raises a NameError: name 'datetime' is not defined
as you didn't define datetime
in your code, but shouldn't raise any error for the subplots.
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
data = pd.DataFrame({'col1': [4, 5, 2, 2, 3, 5, 1, 1, 6], 'col2': [6, 2, 1, 7, 3, 5, 3, 3, 9],
'label':['Old','Old','Old','Old','Old','Old','Old','Old','Old'],
'date': ['2022-01-24 10:07:02', '2022-01-27 01:55:03', '2022-01-30 19:09:03', '2022-02-02 14:34:06',
'2022-02-08 12:37:03', '2022-02-10 03:07:02', '2022-02-10 14:02:03', '2022-02-11 00:32:25',
'2022-02-12 21:42:03']})
def Graphing():
#Size of the figure
fig = plt.figure(figsize=(12, 7))
#Creating the dataframe
df = pd.DataFrame({
'date' : datetime,
'col1': data['col1']
})
for subplot_,add_plot in (zip(
['sub1','sub2','sub3'],
[(2,2,1), (2,2,1), (2,1,2)])):
subplot_ = fig.add_subplot(*add_plot)
# Show Graph
plt.show()
Graphing()