0
G =  [500, 600]    
V_cel =  [[0.5061622703106294, 0.018464827637910804], [0.5109806104259279, 0.020619347874867924]]    
I_cel =  [[0.         0.35938438]     [0.         0.4335135 ]]    
P_cel =  [[0.0, 0.006635970540228099], [0.0, 0.008938765665079252]]
plt.figure(1,figsize=(10,3))
plt.subplot(121)
for i in G:
    for t in list(zip(V_cel, I_cel)):  
        plt.plot(*t, label='%s W/m$^{2}$' % i)        
    plt.legend()
plt.axis([0, 0.7, 0, 0.9])    
plt.subplot(122)
for i in G:
    for t in list(zip(V_cel, P_cel)):  
        plt.plot(*t, label='%s W/m$^{2}$' % i)        
    plt.legend()
plt.axis([0, 0.7, 0, 0.4])
plt.suptitle("I-V & P-V curves of PV cell for given STC data", fontsize=20)
plt.rc('font',family='Times New Roman')
plt.savefig('STC_I_V_curve')
plt.show()

** output is: I have plotted two subplots and it is fine. Problem is with legends. Both should show a list of ten legends only. Because, V_cel and I_celboth have [10 lists, each list with 1000 values]. Instead, both plots are showing 100 legends. Please, see the attached plot. I want it show ten legends [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000] only.Some where the error is. enter image description here

** Output after including the changes as per SpghttCd code, which is accepted as answer enter image description here

Msquare
  • 353
  • 1
  • 7
  • 17
  • [This answer](https://stackoverflow.com/a/4700762/4681316) worked for me: `legend(loc="upper left", bbox_to_anchor=(1,1))` – Ersel Er Jul 20 '18 at 06:23
  • 1
    What is ```G```? Why are you looping over its elements ? – Kefeng91 Jul 20 '18 at 06:25
  • Please add samples of the structure of your data (G, V_cel, I_cel, P_cel) – SpghttCd Jul 20 '18 at 06:40
  • I have added a sample `G, V_cel, I_cel, P_cel` values in above question. – Msquare Jul 20 '18 at 06:58
  • For each value of `G` (solar irradiance), I have computed `V_cel, I_cel, P_cel`. Hence, `G` gives the irraidance values (400 W/m2). – Msquare Jul 20 '18 at 07:00
  • I have tried `legend(loc="upper left", bbox_to_anchor=(1,1))` as suggested by ` Ersel Er `. List is repeating again. The only change is the appearance of legend at end of box. – Msquare Jul 20 '18 at 07:06
  • I have put `plt.legend()` outside the loop as suggested by `Kefeng91`. Still the problem is same. – Msquare Jul 20 '18 at 07:18

1 Answers1

2

You iterate both over your list of G-elements and your array of data characteristics - which is one loop to much.

Assumed that G is your list of incoming energy and with some self designed data arrays, your plot would possibly be createable like:

import numpy as np
import matplotlib.pyplot as plt

fig, axs = plt.subplots(1, 2, figsize=(10, 3))
for g, v, i, p in zip(G, V_cel, I_cel, P_cel):
    axs[0].plot(v, i, label='%s W/m$^{2}$' % g)
    axs[1].plot(v, p)

fig.legend()
axs[0].axis([0, 0.7, 0, 0.9])
axs[1].axis([0, 0.7, 0, 0.9])

enter image description here

plt.suptitle("I-V & P-V curves of PV cell for given STC data", fontsize=20)
plt.tight_layout(rect=[0, 0, .85, .9])
SpghttCd
  • 10,510
  • 2
  • 20
  • 25
  • Thanks for a simple code. I just tried it. But, error is coming like this `ValueError: x and y must have same first dimension, but have shapes (10, 10000) and (10000,)`. I guess, is it because the way I am generating `I_cel` as `I_cel = (np.linspace(0, 1, 10000).reshape(10000,1) @ [Isc_cal]).T`, where `Isc_cal` is a single list of values. – Msquare Jul 20 '18 at 07:23
  • I think `V_cel` is in your case also a 2-D array, so you should add it to the iteration list in `for g, v, i, p in zip(G, V_cel, I_cel, P_cel):` and then plot(v, i, ...) For my sample data, V_cel was exactly the same array for all ten plots, as I created it per `V_cel = np.linspace(0, 0.7, 100)` – SpghttCd Jul 20 '18 at 07:48
  • Excellent. It worked. In my above question statement, I have updated the latest figure, which I obtained through your code. My only problem is, how to eliminate the unwanted white space between the second subplot and legend list. – Msquare Jul 20 '18 at 09:59
  • One more, how the code outputs the `plot` though we did not mention `plt.show()` in the code. – Msquare Jul 20 '18 at 10:02
  • 1
    The space which is used for the plots is defined by the `rect` kwarg of `tight_layout()`. The four numbers are the corners of the rectangle which is used for that. Increase x2 (i.e. 0.85) and the gap between the plots and the legend will shrink, decrease it and it will grow. The same with the value y2 (0.9) with regards to the title. – SpghttCd Jul 20 '18 at 10:12
  • 1
    plt.show() is only needed when matplotlib session is in non-interactive mode. You see it used quite frequently, because it doesn't do anything if in interactive mode. So people don't notice that they could leave it away... – SpghttCd Jul 20 '18 at 10:14
  • Great. I got some lessons here. I am beginner to Python (5 days). Thanks a lot @https://stackoverflow.com/users/8300135/spghttcd – Msquare Jul 20 '18 at 10:16
  • 1
    Your welcome. Using `plt.subplots` for creating a multiple plot figure you could consider setting additional kwargs `sharex=True` and `sharey=True`, so that all subplots automatically scale their axes equally. (Consequently you could leave one line of the axes setting away, eg `axs[1].].axis([0, 0.7, 0, 0.9])`) – SpghttCd Jul 20 '18 at 10:20
  • Yes. Previously I used it. Now, I came to know many lessons about plot from you @ https://stackoverflow.com/users/8300135/spghttcd. Your conversation helped me a lot to understand the Python and plots. – Msquare Jul 20 '18 at 10:23