totallyoreo.blogg.se

Python subplot
Python subplot











  • Method 2: plt.subplot_tool() for matplotlib subplot spacing:.
  • Method 1: tight_layout for matplotlib subplot spacing:.
  • Different methods to add matplotlib subplot spacing:.
  • python subplot

    The output is captured in the figure below.

    #PYTHON SUBPLOT CODE#

    The code simply runs a loop by picking each state (initially sorted based on their weights) and plots them in canvas laid with the purpose of aggregating these subplots. Hence, an interested reader will not find any difficulty in understanding it. The code is embedded with comments for making it self-explanatory. #laying out the subplots canvas fig, axs =plt.subplots(4,3, figsize =(25,25), sharex = True) axs = axs.flatten() i=0 #index for iterating for state in states: df2 = df=state] #filtering the original DF for the specific state df2.reset_index(drop=True, inplace=True) #reseting the row index of the new DF df2 = df2.replace("-",0, regex=True) #replacing chr with zero df2 = df2.replace("-",0, regex=True) #replacing chr with zero ax2 = axs.twinx() #creating a twin axis in the plot canvas #preparing axis for the first plot = "Rural CPI" ax1 = df2.plot(ax = axs, kind='line',x="Date",y="Rural Index",lw=4) #preparing axis for the first plot = "Urban CPI" ax2 = df2.plot(ax = ax2, kind='line',x="Date",y="Urban Index",lw=4,color = color_pal) # ax3 = df1.plot(ax = axs, kind='line',x="Date",y="Combined Index",lw=3.5,color = color_pal) inflation = round(df1.values*100,2) #extrating the values of inflation for display weight = round(df1.values,2) #extracting values of weights for display legend = state+" ("+str(inflation)+"%)"+" " #proparing the legend ax1.set_title(legend, fontsize =24) #printing the title ax1.legend().remove() #preventing the legend from display ax2.legend().remove() #preventing the legend from display ax2.grid(False) #removing the grid for the 2nd plot # ax3.legend().remove() # ax3.grid(False) ax1.set_facecolor("white") #setting up the background as white i += 1 if i=12: #only picking the top 12 states for display break plt.tight_layout() plt.show() The following is the code that I have used for creating the subplots. The output DataFrame is embedded in the following picture.įigure 2 - Intemediate DataFrame Creating Subplots

    python subplot python subplot

    The purpose of the above code is to create another DataFrame that will hold the names of states, their weights, and the latest inflation number for the month of Jan 2023. #importing libraries import pandas as pd import os import numpy as np import matplotlib.pyplot as plt %matplotlib inline #setting ploting style ('fivethirtyeight') color_pal=_key() #changing dir os.chdir("path of the working dir") #working dir print(os.getcwd()) #naming file file ="2022_05_18_State_CPI_Index.xlsx" #loading file from disc df= pd.read_excel(file) #extracting names of all states form the dataframe states = df.drop_duplicates() #extracting all the weights for combined index from the dataframe weights = df.drop_duplicates() #extracting values of latest combined inflation from dataframe comb_infla = df.query("Date = ''") #combining all the three values in one single dataframe df1 = pd.concat(,axis=1) #sorting the dataframe by "Comined Weights" df1 = df1.sort_values(by=, axis=0, ascending=False) #resets the values of row index of df1 df1.reset_index(drop=True, inplace = True) #droping the first value which is "Pan India" df1 = df1 #storing the values of states in a list for iterating states = df1 The following is the code that we can use to process this data for the purpose of feeding into the plotting module.











    Python subplot