I have been trying to plot a smooth graph, and here is my code
import matplotlib.pyplot as plt
#fig,axes= plt.subplots(nrows=6, ncols=1, squeeze=False)
x = df["DOY"]
y = df["By"]
z = df["Bz"]
a = df["Vsw"]
b = df["Nsw"]
c = df["magnetopause_distance"]
d = df["reconnection_rate"]
And after that, I used the following logic to plot the same
#create a figure
fig=plt.figure()
#define subplots and define their position
plt1=fig.add_subplot(611)
plt2=fig.add_subplot(612)
plt3=fig.add_subplot(613)
plt4=fig.add_subplot(614)
plt5=fig.add_subplot(615)
plt6=fig.add_subplot(616)
plt1.plot(x,y,'black',linewidth=0.5,marker=None)
plt1.set_ylabel("By")
plt1.set_title("3-6 July 2003")
plt2.plot(x,z,'black',linewidth=0.5)
plt2.set_ylabel("Bz")
plt3.plot(x,a,'black',linewidth=0.5)
plt3.set_ylabel("Vsw")
plt4.plot(x,b,'black',linewidth=0.5)
plt4.set_ylabel("Nsw")
plt5.plot(x,c,'black',linewidth=0.5)
plt5.set_ylabel("MD")
plt6.plot(x,d,'black',linewidth=0.5)
plt6.set_ylabel("MRR")
plt.subplots_adjust(hspace = 2,wspace = 2)
#saving plot in .jpg format
plt.savefig('myplot01.jpg', format='jpeg',dpi=500, bbox_inches='tight')
Finally, I am getting a plot like this:
What I want is something like this:
Sorry for the typos. Thanks for your time :)
Use:
from scipy.interpolate import UnivariateSpline
import numpy as np
list_x_new = np.linspace(min(x), max(x), 1000)
list_y_smooth = UnivariateSpline(x, y, list_x_new)
plt.plot(list_x_new, list_y_smooth)
plt.show()
This is for one of the graphs, you can substitute the values in list_y_smooth in place of y according to the values you want to plot.
Related
I have a df containing x,y coordinates of a mouse's snout that I want to use for an animated scatterplot. Currently, I have the code for a static scatterplot.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import os
from pathlib import Path
from IPython.display import HTML
#import video pose estimation data
video='topNoF.mp4'
DLCscorer='C:/Users/Bri-Guy/Desktop/DLC/h5/topFDLC_resnet50_topAnalysisJan20shuffle1_100000'
dataname = DLCscorer+'.h5' #can change to .csv instead; make sure to change pd.read_hdf() to pd.read_csv()
df=pd.read_hdf(os.path.join(dataname))
#get X & Y coordinates of snout bdp
scorer=df.columns.get_level_values(0)[0]
x=df[scorer]['snout']['x'].values
y=df[scorer]['snout']['y'].values
#produce a static scatterplot trace of snout movement
length=len(x)
n = len(x)
color = []
for i in range (1,n+1):
color.append(i/n)
scatter=plt.scatter(x,y,c=color, cmap='inferno')
ax = scatter.axes
which yields
I want to use a variation of the code from this Stack Exchange answer: Matplotlib Plot Points Over Time Where Old Points Fade to animate the scatterplot in a manner that looks like this:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from IPython.display import HTML
from matplotlib.colors import LinearSegmentedColormap
plt.rcParams['animation.ffmpeg_path'] = r'C:/Users/Bri-Guy/anaconda3/envs/pWGA/Library/bin/ffmpeg.exe'
x=df[scorer]['snout']['x'].values
y=df[scorer]['snout']['y'].values
fig, ax = plt.subplots()
plt.xlim(0, max(x)+100)
plt.ylim(0, max(y)+100)
graph, = plt.plot([], [], 'o')
def animate(i):
graph.set_data(x[:i+1], y[:i+1])
return graph
ani = FuncAnimation(fig, animate, frames=len(y), interval=20)
html = ani.to_html5_video()
HTML(html)
Ideally, I'd like for the points in this animation to fade over time (smooth fade like the example linked). Also, I'm not sure how to set a colormap such as inferno to the animated scatterplot. Fade & the ability to set a colormap are my most important priorities.
The main issue I'm running into with the code from the linked example is the following area:
def get_new_vals():
n = np.random.randint(1,5)
x = np.random.rand(n)
y = np.random.rand(n)
return list(x), list(y)
def update(t):
global x_vals, y_vals, intensity
# Get intermediate points
new_xvals, new_yvals = get_new_vals()
x_vals.extend(new_xvals)
y_vals.extend(new_yvals)
# Put new values in your plot
scatter.set_offsets(np.c_[x_vals,y_vals])
#calculate new color values
intensity = np.concatenate((np.array(intensity)*0.96, np.ones(len(new_xvals))))
scatter.set_array(intensity)
# Set title
ax.set_title('Time: %0.3f' %t)
I need to get one new value from x=df[scorer]['snout']['x'].values & y=df[scorer]['snout']['y'].values. These values have to be called in order from x[0] to x[len(x)-1] so that the plot will update chronologically. However, I get errors when trying to add a parameter in get_new_vals(i) because I think the t variable for animation isn't an integer. I'm not sure if there is also an issue with the element types inside my arrays x & y since they are floating points.
I appreciate your help in advance! Please let me know if I can clarify anything for you. Below I have posted some of the data inside my x,y variables:
print(x[:200])
[276.89370728 280.57974243 285.25439453 285.55096436 284.71258545
283.52386475 284.31976318 285.08609009 285.56118774 285.38183594
289.21246338 295.28497314 303.41043091 315.51828003 324.87826538
333.36367798 338.73730469 341.11685181 349.20300293 357.63671875
366.72702026 395.68356323 385.37298584 387.86871338 387.58526611
382.20205688 378.13674927 373.97241211 368.39953613 591.94116211
347.27310181 616.52069092 608.12902832 605.11340332 602.10974121
598.72052002 598.48504639 599.19256592 601.30432129 603.32104492
604.9621582 605.21533203 621.36779785 627.51617432 626.20269775
621.00164795 618.92498779 617.44885254 615.73883057 598.8916626
594.17883301 593.38647461 589.3248291 592.67895508 593.67053223
589.05767822 589.08850098 303.0085144 568.39239502 555.08520508
550.79425049 547.77197266 547.21954346 313.01544189 333.96121216
348.59899902 353.26141357 358.76705933 360.81588745 363.94262695
527.38165283 522.80316162 518.20489502 521.84442139 525.30664062
526.43286133 532.38995361 536.35961914 536.51574707 540.41906738
545.77844238 545.22381592 545.34112549 540.22357178 537.93457031
534.03442383 532.6651001 522.52618408 505.38290405 489.37664795
469.75460815 448.28039551 424.54315186 403.87719727 383.25265503
359.93307495 335.06869507 318.53125 296.43450928 288.86499023
442.87780762 443.70950317 440.31652832 439.50854492 439.11328125
434.4161377 216.55622864 216.7456665 215.32554626 213.63644409
213.8143158 213.96568298 213.87882996 214.10801697 214.05957031
212.54373169 216.25740051 214.80444336 216.47532654 218.31072998
215.78303528 213.40249634 292.92352295 290.80630493 287.03222656
283.45129395 390.10848999 274.83648682 269.53741455 215.76341248
218.75086975 220.38156128 219.87997437 219.83804321 218.52023315
216.93737793 218.18110657 218.31959534 224.79884338 224.69064331
221.88998413 218.67016602 216.9510498 216.63031006 215.88612366
217.3243103 217.01783752 214.08659363 213.87808228 211.14770508
206.47595215 214.88208008 214.39358521 212.50665283 212.39123535
213.95169067 217.72639465 313.20504761 288.23443604 283.30273438
283.1756897 281.46990967 276.20397949 273.39535522 274.38088989
267.42678833 269.19915771 271.11810303 331.80795288 330.61746216
329.03930664 227.14578247 226.81338501 227.80999756 229.65690613
229.97644043 207.91325378 219.31289673 225.3374939 230.50515747
313.22525024 310.83474731 306.02667236 299.73217773 288.91854858
278.45489502 266.55349731 264.91229248 256.15029907 249.70783997
245.47244263 246.11851501 245.35572815 246.03157043 246.50708008
246.63691711 245.77215576 245.09873962 241.44792175 243.87677002]
print(y[:200])
[1321.18652344 1316.84301758 1316.04064941 1315.66455078 1315.38586426
1315.74560547 1317.04711914 1318.11218262 1320.09631348 1321.45703125
1328.39794922 1339.04956055 1353.28076172 1364.76757812 1371.4901123
1376.57568359 1381.58361816 1383.17236328 1390.66870117 1401.93652344
1412.72961426 1393.1583252 1437.81616211 1440.15380859 1440.01843262
1438.75891113 1437.50012207 1436.55932617 1429.39416504 1620.10900879
1400.08654785 1506.79125977 1497.49243164 1491.12316895 1488.84606934
1487.79296875 1489.73840332 1488.69665527 1490.09631348 1490.75927734
1490.99291992 1487.58154297 1610.09851074 1612.06115723 1613.1282959
1615.44006348 1619.22009277 1620.50915527 1618.51501465 1495.30175781
1496.44470215 1497.99816895 1492.10546875 1474.48339844 1481.59130859
1475.67248535 1477.48083496 1375.00964355 1480.91625977 1486.29675293
1493.73193359 1501.6204834 1507.31176758 1366.4050293 1358.88977051
1346.67797852 1339.04455566 1333.7755127 1328.54821777 1322.38439941
1502.17102051 1497.28198242 1493.86022949 1479.5690918 1469.38928223
1458.9095459 1448.56921387 1448.53613281 1443.38757324 1444.77124023
1443.64233398 1440.03857422 1431.31848145 1427.53820801 1427.53393555
1428.34887695 1429.39526367 1431.30249023 1440.09594727 1457.33044434
1474.85864258 1492.55554199 1498.40905762 1497.65649414 1503.4185791
1507.8626709 1507.43164062 1506.86315918 1507.16052246 1506.81115723
1313.21789551 1317.2467041 1322.02380371 1320.96960449 1314.60107422
1314.20336914 1640.76843262 1656.12866211 1667.98669434 1675.00683594
1675.64379883 1678.5567627 1678.24157715 1673.58984375 1667.44628906
1651.3112793 1635.78564453 1626.48413086 1616.28967285 1594.74316406
1580.41320801 1579.52124023 1506.03894043 1504.37646484 1501.6105957
1500.49902344 1316.76806641 1491.15759277 1471.89294434 1575.55322266
1585.22619629 1594.93151855 1598.60961914 1597.25134277 1598.54443359
1599.59936523 1597.15466309 1594.58544922 1585.51281738 1582.63903809
1584.89025879 1585.41784668 1589.9654541 1592.00085449 1596.66369629
1598.4128418 1598.8269043 1600.46069336 1596.1394043 1597.69421387
1598.36975098 1582.69897461 1580.45617676 1581.58618164 1580.9498291
1578.98144531 1575.28918457 1494.18151855 1490.47180176 1490.87109375
1485.41467285 1480.73291016 1481.30554199 1485.26867676 1489.50756836
1497.07263184 1501.99645996 1505.94543457 1267.27172852 1270.28515625
1281.31738281 1568.67248535 1576.60119629 1578.63500977 1577.04748535
1577.89709473 1681.06103516 1681.95227051 1684.63745117 1686.96484375
1325.62084961 1331.21569824 1331.70007324 1331.41809082 1336.49401855
1345.08703613 1351.00073242 1356.37145996 1360.50805664 1367.5177002
1371.69470215 1376.6151123 1377.27844238 1376.92382812 1376.37487793
1377.03894043 1377.87390137 1378.53796387 1373.02990723 1365.00915527]
In case anyone's curious. I managed to solve the issue of adapting the linked code to my dataset by converting x,y numpy arrays to lists using the list() function, then chronologically iterate over each list in get_new_vals() without introducing a parameter by using .pop(). The last piece of troubleshooting was with extend() and len() in update() function. Since extend() doesn't work on numpy.float64, I switched to append(). The variable new_xvals has no real length due to how the data is returned from get_new_vals(), so I switched np.ones(len()) to np.ones(1) because I'm moving through one data point at a time anyway. Final code:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation
import os
from matplotlib.colors import LinearSegmentedColormap
from matplotlib.animation import PillowWriter
from IPython.display import HTML
#set path to ffmpeg for animation processing
plt.rcParams['animation.ffmpeg_path'] = r'C:/Users/Bri-Guy/anaconda3/envs/pWGA/Library/bin/ffmpeg.exe'
#import video pose estimation data
video='topNoF.mp4'
DLCscorer='C:/Users/Bri-Guy/Desktop/DLC/h5/topFDLC_resnet50_topAnalysisJan20shuffle1_100000'
dataname = DLCscorer+'.h5' #can change to .csv instead; make sure to change pd.read_hdf() to pd.read_csv()
df=pd.read_hdf(os.path.join(dataname))
#get X & Y coordinates of snout bdp
scorer=df.columns.get_level_values(0)[0]
x=df[scorer]['snout']['x'].values
y=df[scorer]['snout']['y'].values
x=list(x)
y=list(y)
#set plt & axes elements (empty canvas to be iterated over)
fig, ax = plt.subplots()
ax.set_xlabel('X Axis', size = 12)
ax.set_ylabel('Y Axis', size = 12)
ax.axis([0,max(x),0,max(y)])
x_vals = []
y_vals = []
intensity = []
iterations = 1000 #set number of frames for video
t_vals = np.linspace(0,1, iterations)
#define colormap and scatterplot
colors = [[0,0,1,0],[0,0,1,0.5],[0,0.2,0.4,1], [1,0.2,0.4,1]]
cmap = LinearSegmentedColormap.from_list("", colors)
scatter = ax.scatter(x_vals,y_vals, c=[], cmap=cmap, vmin=0,vmax=1)
def get_new_vals():
xp = x.pop(0)
yp = y.pop(0)
return xp, yp
def update(t):
global x_vals, y_vals, intensity
# Get intermediate points
new_xvals, new_yvals = get_new_vals()
x_vals.append(new_xvals)
y_vals.append(new_yvals)
# Put new values in your plot
scatter.set_offsets(np.c_[x_vals,y_vals])
#calculate new color values
intensity = np.concatenate((np.array(intensity)*0.98, np.ones(1)))
scatter.set_array(intensity)
# Set title
ax.set_title('title')
ani = matplotlib.animation.FuncAnimation(fig, update, frames=t_vals,interval=50)
html = ani.to_html5_video() #necessary to view anim in Jup.NoteBook
HTML(html)
data = {'tenor': ['1w','1m','3m','6m','12m','1y','2y','3y','4y','5y','6y','7y','10y','15y','20y','25y','30y','40y','50y'],'rate_s': [0.02514, 0.026285, 0.0273, 0.0279, 0.029616, 0.026526, 0.026028, 0.024, 0.025958,0.0261375, 0.026355, 0.026, 0.026898, 0.0271745, 0.02741, 0.027, 0.0275, 0.0289,0.0284],'rate_t':[ 0.02314, 0.024285, 0.0253,0.0279, 0.028616, 0.026526,0.027028, 0.024, 0.025958,0.0271375, 0.02355, 0.026, 0.024898, 0.0271745, 0.02641,0.027, 0.0255, 0.0289,0.0284]}
I want to produce the chart in blue with the same format like below. I tried this piece of code but results are not satisfactory (chart in white). It also not showing all x-axis labels. Please suggest.
ax = plt.gca()
df.plot(kind='line',x='tenor',y='rate_s',marker='o',color='green',ax=ax)
df.plot(kind='line',x='tenor',y='rate_y',marker='o', color='red', ax=ax)
ax.minorticks_on()
ax.grid(which='major',linestyle='-', linewidth='0.5', color='blue')
ax.grid(which='minor', linestyle=':', linewidth='0.5', color='black')
plt.show()
This is following the discussions in the comments.
There are a couple parts, the full example is at the bottom.
Style
One of your questions was how to change the style of the plot. This can be done with the following code:
import matplotlib.pyplot as plt
plt.style.use('seaborn-darkgrid')
there are many possible styles, and you can even create your own style if you wish. To see all possible styles see: the documentation. To list all styles use plt.style.available
Custom Ticker
For the custom tickers: you can use FixedLocator or if you know it is log or symlog, then matplotlib has a built-in locator. See the matplotlib doc for scales
You can use FixedLocator to set up the axis, to be separated. i.e. the following code will give you what you want.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
X = np.arange(0, 2000)
Y = np.arange(0, 2000)
def convert(date):
if 'w' in date:
return 7*int(date[:-1])
if 'm' in date:
return 30*int(date[:-1])
if 'y' in date:
return 30*int(date[:-1]) + 360
ticks = [convertdate(d) for d in tenor]
plt.style.use('seaborn-darkgrid')
ax = plt.axes()
t = ticker.FixedLocator(locs=ticks)
ax.xaxis.set_ticklabels(tenor)
ax.xaxis.set_major_locator(t)
# ax.xaxis.set_minor_locator(ticker.MultipleLocator(3))
plt.plot(X, Y, c = 'k')
plt.show()
Which produces:
Specific Case
For your specific case, you probably want the custom tickers to be on a specific interval (i.e. smallest of rate_t, biggest of rate_t).
Thus you would need to change the convert function to be as following:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
x = data['rate_t']
y = data['rate_s']
def get_indices(date):
if 'w' in date:
return 7*int(date[:-1])
if 'm' in date:
return 30*int(date[:-1])
if 'y' in date:
return 30*int(date[:-1]) + 360
def convert(indices):
x = np.linspace(min(data['rate_t']), max(data['rate_t']), indices[-1] + 1)
return x[indices]
indices = [get_indices(d) for d in tenor]
ticks = convert(indices)
plt.style.use('seaborn-darkgrid')
ax = plt.axes()
t = ticker.FixedLocator(locs=ticks)
ax.xaxis.set_ticklabels(tenor)
ax.xaxis.set_major_locator(t)
# ax.xaxis.set_minor_locator(ticker.MultipleLocator(3))
plt.plot(x, y, c = 'k')
plt.show()
(assuming the data['rate_s'] and data['rate_t'] are as is and without processing)
Which would produce this:
Let me know if you have any questions.
I have a code:
import math
import numpy as np
import pylab as plt1
from matplotlib import pyplot as plt
uH2 = 1.90866638
uHe = 3.60187307
eH2 = 213.38
eHe = 31.96
R = float(uH2*eH2)/(uHe*eHe)
C_Values = []
Delta = []
kHeST = []
J_f21 = []
data = np.genfromtxt("Lamda_HeHCL.txt", unpack=True);
J_i1=data[1];
J_f1=data[2];
kHe=data[7]
data = np.genfromtxt("Basecol_Basic_New_1.txt", unpack=True);
J_i2=data[0];
J_f2=data[1];
kH2=data[5]
print kHe
print kH2
kHe = map(float, kHe)
kH2 = map(float, kH2)
kHe = np.array(kHe)
kH2= np.array(kH2)
g = len(kH2)
for n in range(0,g):
if J_f2[n] == 1:
Jf21 = J_f2[n]
J_f21.append(Jf21)
ratio = kHe[n]/kH2[n]
C = (((math.log(float(kH2[n]),10)))-(math.log(float(kHe[n]),10)))/math.log(R,10)
C_Values.append(C)
St = abs(J_f1[n] - J_i1[n])
Delta.append(St)
print C_Values
print Delta
print J_f21
fig, ax = plt.subplots()
ax.scatter(Delta,C_Values)
for i, txt in enumerate(J_f21):
ax.annotate(txt, (Delta[i],C_Values[i]))
plt.plot(np.unique(Delta), np.poly1d(np.polyfit(Delta, C_Values, 1))(np.unique(Delta)))
plt.plot(Delta, C_Values)
fit = np.polyfit(Delta,C_Values,1)
fit_fn = np.poly1d(fit)
# fit_fn is now a function which takes in x and returns an estimate for y
plt.scatter(Delta,C_Values, Delta, fit_fn(Delta))
plt.xlim(0, 12)
plt.ylim(-3, 3)
In this code, I am trying to plot a linear regression that extends past the data and touches the x-axis. I am also trying to add a legend to the plot that shows the slope of the plot. Using the code, I was able to plot this graph.
Here is some trash data I have been using to try and extend the line and add a legend to my code.
x =[5,7,9,15,20]
y =[10,9,8,7,6]
I would also like it to be a scatter except for the linear regression line.
Given that you don't provide the data you're loading from files I was unable to test this, but off the top of my head:
To extend the line past the plot, you could turn this line
plt.plot(np.unique(Delta), np.poly1d(np.polyfit(Delta, C_Values, 1))(np.unique(Delta)))
Into something like
x = np.linspace(0, 12, 50) # both 0 and 12 are from visually inspecting the plot
plt.plot(x, np.poly1d(np.polyfit(Delta, C_Values, 1))(x))
But if you want the line extended to the x-axis,
polynomial = np.polyfit(Delta, C_Values, 1)
x = np.linspace(0, *np.roots(polynomial))
plt.plot(x, np.poly1d(polynomial)(x))
As for the scatter plot thing, it seems to me you could just remove this line:
plt.plot(Delta, C_Values)
Oh right, as for the legend, add a label to the plots you make, like this:
plt.plot(x, np.poly1d(polynomial)(x), label='Linear regression')
and add a call to plt.legend() just before plt.show().
I'm using a library which produces 3 plots given an object k.
I need to figure the data points (x,y,z) that produced these plot, but the problem is that the plots comes from a function from k.
The library I'm using is pyKriging and this is their github repository.
A simplified version of their example code is:
import pyKriging
from pyKriging.krige import kriging
from pyKriging.samplingplan import samplingplan
sp = samplingplan(2)
X = sp.optimallhc(20)
testfun = pyKriging.testfunctions().branin
y = testfun(X)
k = kriging(X, y, testfunction=testfun, name='simple')
k.train()
k.plot()
The full code, comments and output can be found here.
In summary, I'm trying to get the numpy array that produced these plots so I can create plots that follows my formatting styles.
I'm not knowledgeable about going into library codes in Python and I appreciate any help!
There is no single data array that produces the plot. Instead many arrays used for plotting are generated inside the kriging plot function.
Changing the filled contours to line contours is of course not a style option. One therefore needs to use the code from the original plotting function.
An option is to subclass kriging and implement a custom plot function (let's call it myplot). In this function, one can use contour instead of contourf. Naturally, it's also possible to change it completely to one's needs.
import pyKriging
from pyKriging.krige import kriging
from pyKriging.samplingplan import samplingplan
import numpy as np
import matplotlib.pyplot as plt
class MyKriging(kriging):
def __init__(self,*args,**kwargs):
kriging.__init__(self,*args,**kwargs)
def myplot(self,labels=False, show=True, **kwargs):
fig = plt.figure(figsize=(8,6))
# Create a set of data to plot
plotgrid = 61
x = np.linspace(self.normRange[0][0], self.normRange[0][1], num=plotgrid)
y = np.linspace(self.normRange[1][0], self.normRange[1][1], num=plotgrid)
X, Y = np.meshgrid(x, y)
# Predict based on the optimized results
zs = np.array([self.predict([xi,yi]) for xi,yi in zip(np.ravel(X), np.ravel(Y))])
Z = zs.reshape(X.shape)
#Calculate errors
zse = np.array([self.predict_var([xi,yi]) for xi,yi in zip(np.ravel(X), np.ravel(Y))])
Ze = zse.reshape(X.shape)
spx = (self.X[:,0] * (self.normRange[0][1] - self.normRange[0][0])) + self.normRange[0][0]
spy = (self.X[:,1] * (self.normRange[1][1] - self.normRange[1][0])) + self.normRange[1][0]
contour_levels = kwargs.get("levels", 25)
ax = fig.add_subplot(222)
CS = plt.contour(X,Y,Ze, contour_levels)
plt.colorbar()
plt.plot(spx, spy,'or')
ax = fig.add_subplot(221)
if self.testfunction:
# Setup the truth function
zt = self.testfunction( np.array(zip(np.ravel(X), np.ravel(Y))) )
ZT = zt.reshape(X.shape)
CS = plt.contour(X,Y,ZT,contour_levels ,colors='k',zorder=2, alpha=0)
if self.testfunction:
contour_levels = CS.levels
delta = np.abs(contour_levels[0]-contour_levels[1])
contour_levels = np.insert(contour_levels, 0, contour_levels[0]-delta)
contour_levels = np.append(contour_levels, contour_levels[-1]+delta)
CS = plt.contour(X,Y,Z,contour_levels,zorder=1)
plt.plot(spx, spy,'or', zorder=3)
plt.colorbar()
ax = fig.add_subplot(212, projection='3d')
ax.plot_surface(X, Y, Z, rstride=3, cstride=3, alpha=0.4)
if self.testfunction:
ax.plot_wireframe(X, Y, ZT, rstride=3, cstride=3)
if show:
plt.show()
sp = samplingplan(2)
X = sp.optimallhc(20)
testfun = pyKriging.testfunctions().branin
y = testfun(X)
k = MyKriging(X, y, testfunction=testfun, name='simple')
k.train()
k.myplot()
I have made an animation from a set of images like this (10 snapshots):
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
import time
infile = open ('out.txt')
frame_counter = 0
N_p = 100
N_step = 10
N_line = N_p*N_step
for s in xrange(N_step):
x, y = [], []
for i in xrange(N_p):
data = infile.readline()
raw = data.split()
x.append(float(raw[0]))
y.append(float(raw[1]))
xnp = np.array(x)
ynp = np.array(y)
fig = plt.figure(0)
ax = fig.add_subplot(111, aspect='equal')
for x, y in zip(xnp, ynp):
cir = Circle(xy = (x, y), radius = 1)
cir.set_facecolor('red')
ax.add_artist(cir)
cir.set_clip_box(ax.bbox)
ax.set_xlim(-10, 150)
ax.set_ylim(-10, 150)
fig.savefig("step.%04d.png" % frame_counter)
ax.remove()
frame_counter +=1
Now I want to add a legend to each image showing the time step.
For doing this I must set legends to each of these 10 images. The problem is that I have tested different things like ax.set_label , cir.set_label, ...
and I get errors like this:
UserWarning: No labelled objects found. Use label='...' kwarg on individual plots
According to this error I must add label to my individual plots, but since this is a plot of Artists, I don't know how I can do this.
If for whatever reason you need a legend, you can show your Circle as the handle and use some text as the label.
ax.legend(handles=[cir], labels=["{}".format(frame_counter)])
If you don't really need a legend, you can just use some text to place inside the axes.
ax.text(.8,.8, "{}".format(frame_counter), transform=ax.transAxes)