i have the following code:
import pandas as pd
from pandas import datetime
from pandas import DataFrame as df
import matplotlib
from pandas_datareader import data as web
import matplotlib.pyplot as plt
import datetime
import numpy as np
stocks = 'GE','F' #<-- In this case there are just 2 symbols but this could be more
start = datetime.date(2000,1,1)
end = datetime.date.today()
data = web.DataReader(stock, 'yahoo',start, end)
for stock in stocks:
data.plot(y='Close')
plt.subplot(1,1,2) #<-- Trouble here
Here is my question. How can i plot all the symbols side by side inside the forloop?
To create multiple plots we use the subplot function of pyplot module in Matplotlib. Parameters: nrows is for number of rows means if the row is 1 then the plots lie horizontally. ncolumns stands for column means if the column is 1 then the plot lie vertically.
By using plt. subplot() method we create two subplots side by side. plt. bar() method is used to create multiple bar chart graphs.
To create two graphs, we can use nrows=1, ncols=2 with figure size (7, 7). Create a data frame with keys, col1 and col2, using Pandas. Use countplot() to show the counts of observations in each categorical bin using bars. Adjust the padding between and around the subplots.
How can i plot all the symbols side by side inside the forloop? You can take the code above and specify figsize parameter. If you want all of your plots on the same row, just modify the first parameter of figsize which is the length of the horizontal axis of the box that contains the subplots.
An alternative approach is to create an axis object on the fly insidethe loop, although you still need to specify the grid size (rows x cols) ahead of time. This means that you only create an axis if there is data to fill it and you do not get unnecessary empty plots.
Is it possible to plot them side by side but each plot will take the space of 1 only instead of space being split ? You can use the figsize argument: plt.figure (figsize= [width, height]) to specify the size of the whole figure. You were almost correct. You should place subplot inside the for loop.
In Matplotlib we can create multiple plots by calling them once. To create multiple plots we use the subplot function of pyplot module in Matplotlib. nrows is for number of rows means if the row is 1 then the plots lie horizontally. ncolumns stands for column means if the column is 1 then the plot lie vertically.
You can take the code above and specify figsize
parameter.
fig, ax = plt.subplots(1, len(stocks), figsize=(20, 10))
If you want all of your plots on the same row, just modify the first parameter of figsize
which is the length of the horizontal axis of the box that contains the subplots.
For example, if you want every plot to be 10x10 :
fig, ax = plt.subplots(1, len(stocks), figsize=(len(stocks)*10, 10))
Here i have used the plot function of dataframe, Also i like plotting in different colors so i do this small trick.
#CODE
import pandas as pd
from pandas import datetime
from pandas import DataFrame as df
import matplotlib
from pandas_datareader import data as web
import matplotlib.pyplot as plt
import datetime
import numpy as np
colors = ["b",'g','r','c','m','y','k']
stocks = 'GE','F' #<-- In this case there are just 2 symbols but this could be more
start = datetime.date(2000,1,1)
end = datetime.date.today()
data = web.DataReader(stocks, 'yahoo',start, end)
# THIS MAKES A GRID OF 1 ROW and len(stocks) COLUMN and figure size as (width, height) in inches.
fig, axs = plt.subplots(1, len(stocks), figsize=(30, 5))
i = 0
#iterate for each stock
for stock in stocks:
# i'th close stock will plot on i'th axs (Note: Whatever be your grid)
data["Close"][stock].plot(ax=axs[i],color=colors[i%len(colors)])
i += 1
#show plot
plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With