Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot charts side by side with a forloop

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?

like image 763
Slartibartfast Avatar asked Jan 20 '20 19:01

Slartibartfast


People also ask

How do you plot 3 graphs side by side in Python?

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.

How do I plot two bar graphs side by side in Python?

By using plt. subplot() method we create two subplots side by side. plt. bar() method is used to create multiple bar chart graphs.

How do you plot Seaborn plots side by side?

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 to plot all the symbols side by side inside forloop?

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.

How do you create an axis in a plot loop?

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?

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.

How to create multiple plots in Matplotlib?

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.


Video Answer


2 Answers

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))
like image 165
Jinter Avatar answered Oct 27 '22 18:10

Jinter


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()
like image 34
Vivasvan Patel Avatar answered Oct 27 '22 16:10

Vivasvan Patel