Home > Software engineering >  How to plot multiple dataframe columns with options for markers, colors, etc
How to plot multiple dataframe columns with options for markers, colors, etc

Time:10-02

While the line plot comes out fine, I am looking for a more efficient way to write this code and to shorten it. What would be considered 'best practices? New fella working on the foundations. I feel like I should be using a loop to go through assigning all the y values, and perhaps the plotting as well.

Data description: Time series data 1990-2018, with continent electricity consumption (TWH).

fig, ax = plt.subplots(figsize=(16,9)) 

plt.rcParams['font.sans-serif'] = 'Calibri'
plt.rcParams['axes.edgecolor']='#333F4B'
plt.rcParams['axes.linewidth']=0.8


X1 = df_cont_cons.iloc[:,0]

y1 = df_cont_cons.iloc[:,1]
y2 = df_cont_cons.iloc[:,2]
y3 = df_cont_cons.iloc[:,3]
y4 = df_cont_cons.iloc[:,4]
y5 = df_cont_cons.iloc[:,5]
y6 = df_cont_cons.iloc[:,6]
y7 = df_cont_cons.iloc[:,7]
y8 = df_cont_cons.iloc[:,8]
y9 = df_cont_cons.iloc[:,9]
y10 = df_cont_cons.iloc[:,10]

ax.tick_params(axis='x', rotation=90)

plt.plot( X1, y2, marker='', color='#89FAB4', linewidth=3, label="OECD")
plt.plot( X1, y3, marker='', color='#FAE4A0', linewidth=2, label="BRICS")
plt.plot( X1, y4, marker=' ', color='#FA837D', linewidth=1, label="Europe")
plt.plot( X1, y5, marker='o', color='#B049E3', linewidth=2, label="North America")
plt.plot( X1, y6, marker='', color='#E3BA5F', linewidth=2, label="Latin America")
plt.plot( X1, y7, marker='', color='#E35E54', linewidth=2, linestyle='dashed' , label="Asia") 
plt.plot( X1, y8, marker='', color='#6591EA', linewidth=1, label="Pacific")
plt.plot( X1, y9, marker='', color='#EB83C6', linewidth=1, label="Africa")
plt.plot( X1, y9, marker='', color='#EB1551', linewidth=1, label="Middle East")
plt.plot( X1, y10, marker='', color='#1802F4', linewidth=1, label="CIS")

plt.legend()
fig.tight_layout(pad=3)
fig.suptitle('Power Generated by Renewable Energy sources (TWh)', fontsize=22, y=1.02, color='#A2BDF2')

df

Year,World,OECD,BRICS,Europe,North America,Latin America,Asia,Pacific,Africa,Middle-East,CIS
1990,101855.54000000001,52602.490000000005,26621.070000000003,20654.88,24667.230000000003,5373.06,24574.190000000002,1197.89,4407.77,2581.86,16049.400000000001
1991,102483.56000000001,53207.25,26434.99,20631.620000000003,24841.68,5500.990000000001,24783.530000000002,1186.26,4535.700000000001,2744.6800000000003,15898.210000000001
1992,102588.23000000001,53788.75,25993.050000000003,20189.68,25341.77,5628.92,25690.670000000002,1209.52,4582.22,3081.9500000000003,14339.79
1993,103646.56000000001,54614.48,26283.800000000003,20189.68,25830.230000000003,5675.4400000000005,26876.93,1267.67,4721.780000000001,3349.44,13246.570000000002
1994,104449.03000000001,55579.770000000004,25993.050000000003,20085.010000000002,26365.210000000003,5989.450000000001,28098.08,1279.3000000000002,4803.1900000000005,3640.19,11606.740000000002
1995,107112.3,56754.4,26946.710000000003,20713.030000000002,26714.11,6024.34,29761.170000000002,1290.93,5000.900000000001,3744.86,11188.060000000001
1996,109763.94,58417.490000000005,27481.690000000002,21445.72,27295.61,6303.46,30772.980000000003,1372.3400000000001,5152.09,3814.6400000000003,10850.79
1997,110903.68000000001,59022.25000000001,27446.800000000003,21341.050000000003,27574.730000000003,6570.950000000001,31435.890000000003,1407.23,5280.02,4058.8700000000003,10373.960000000001
1998,111450.29000000001,59219.96000000001,27528.210000000003,21503.870000000003,27772.440000000002,6803.55,31331.22,1442.1200000000001,5431.21,4082.13,10152.99
1999,113974.0,60301.55,28319.050000000003,21306.16,28528.390000000003,6989.63,32412.81,1477.01,5559.14,4244.95,10373.960000000001
2000,116590.75,61685.520000000004,28923.81,21538.760000000002,29342.49,7024.52,33564.18,1500.27,5617.29,4314.7300000000005,10501.890000000001
2001,117521.15000000001,61452.920000000006,29528.570000000003,21934.18,28795.88,7036.150000000001,34227.090000000004,1477.01,5756.85,4663.63,10571.67
2002,120207.68000000001,61987.9,30552.010000000002,21969.07,29156.410000000003,7280.38,35680.840000000004,1523.5300000000002,5849.89,4884.6,10653.08
2003,124464.26000000001,62871.780000000006,33157.130000000005,22515.68,29377.38,7501.35,38181.29,1535.16,6140.64,4954.38,11036.87
2004,129953.62000000001,64058.04,35994.850000000006,22748.280000000002,29993.77,7780.47,41437.69,1558.42,6466.280000000001,5245.13,11188.060000000001
2005,133582.18000000002,64453.46000000001,38169.66,22864.58,30168.22,8059.59,43693.91,1570.0500000000002,6652.360000000001,5675.4400000000005,11222.95
2006,137396.82,64616.280000000006,40670.11,23108.81,29958.88,8303.82,46124.58,1616.5700000000002,6861.700000000001,5989.450000000001,11548.59
2007,141211.46000000002,64883.770000000004,43112.41,22829.690000000002,30424.08,8571.310000000001,48555.25,1663.0900000000001,7129.1900000000005,6315.09,11664.890000000001
2008,142874.55000000002,64034.780000000006,44635.94,22829.690000000002,29644.870000000003,8780.650000000001,49636.840000000004,1721.24,7443.200000000001,6850.070000000001,11874.230000000001
2009,141490.58000000002,61197.060000000005,46426.96000000001,21608.54,28214.38,8559.68,51858.170000000006,1721.24,7606.02,7059.410000000001,10967.09

CodePudding user response:

  • Plot directly with enter image description here


    • Alternatively, combine the values for each plot using zip, and iterate through each combination of values.
    markers = ['', '', ' ', 'o', '', '', '', '', '', '']
    colors = ['#89FAB4', '#FAE4A0', '#FA837D', '#B049E3', '#E3BA5F', '#E35E54', '#6591EA', '#EB83C6', '#EB1551', '#1802F4']
    lws = [3, 2, 1, 2, 2, 2, 1, 1, 1, 1]
    columns = df.columns[2:]  # select all the columns except Year and World
    
    fig, ax = plt.subplots(figsize=(16, 9)) 
    for marker, color, lw, col in zip(markers, colors, lws, columns):
        ax.plot('Year', col, marker=marker, color=color, lw=lw, label=col, data=df)
        
    ax.set_xticks(df.Year)
    ax.legend(bbox_to_anchor=(1, 1.01), loc='upper left')
    fig.tight_layout(pad=3)
    fig.suptitle('Power Generated by Renewable Energy sources (TWh)', fontsize=22, y=1.02, color='#A2BDF2')
    plt.show()
    

    enter image description here

    CodePudding user response:

    You can minimalize implementation of that variables:

    y1 = df_cont_cons.iloc[:,1]
    y2 = df_cont_cons.iloc[:,2]
    y3 = df_cont_cons.iloc[:,3]
    y4 = df_cont_cons.iloc[:,4]
    y5 = df_cont_cons.iloc[:,5]
    y6 = df_cont_cons.iloc[:,6]
    y7 = df_cont_cons.iloc[:,7]
    y8 = df_cont_cons.iloc[:,8]
    y9 = df_cont_cons.iloc[:,9]
    y10 = df_cont_cons.iloc[:,10]
    

    You can crate a matrix with y's data:

    y_data=[]
    for y in range(1,11):  #number of ur variables  1 
     y_data.append = df_cont_cons.iloc[:,i]
    

    now u can use that matrix to induce ur data, i show example of first two rows :

    plt.plot( X1, y_data[0], marker='', color='#89FAB4', linewidth=3, label="OECD")
    plt.plot( X1, y_data[1], marker='', color='#FAE4A0', linewidth=2, label="BRICS")
    
  • Related