Matplotlib axes have Major and Minor ticks. How do I add a third level of tick below Minor?
For example
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker
t = np.arange(0.0, 100.0, 0.1)
s = np.sin(0.1*np.pi*t)*np.exp(-t*0.01)
fig, ax = plt.subplots()
plt.plot(t, s)
ax1 = ax.twiny()
ax1.plot(t, s)
ax1.xaxis.set_ticks_position('bottom')
majors = np.linspace(0, 100, 6)
minors = np.linspace(0, 100, 11)
thirds = np.linspace(0, 100, 101)
ax.xaxis.set_major_locator(matplotlib.ticker.FixedLocator(majors))
ax.xaxis.set_minor_locator(matplotlib.ticker.FixedLocator(minors))
ax1.xaxis.set_major_locator(matplotlib.ticker.FixedLocator([]))
ax1.xaxis.set_minor_locator(matplotlib.ticker.FixedLocator(thirds))
ax1.tick_params(which='minor', length=2)
ax.tick_params(which='minor', length=4)
ax.tick_params(which='major', length=6)
ax.grid(which='both',axis='x',linestyle='--')
plt.axhline(color='gray')
plt.show()
produces the effect I want using twinned x-axes.
Is there a better way?
As I stated that you can achieve what you want by deriving from some key classes, I decided to do so (but as I said, it's probably not worth the effort). Anyway, here is what I've got:
from matplotlib import pyplot as plt
from matplotlib import axes as maxes
from matplotlib import axis as maxis
import matplotlib.ticker as mticker
import matplotlib.cbook as cbook
from matplotlib.projections import register_projection
from matplotlib import ticker
import numpy as np
class SubMinorXAxis(maxis.XAxis):
def __init__(self,*args,**kwargs):
self.subminor = maxis.Ticker()
self.subminorTicks = []
self._subminor_tick_kw = dict()
super(SubMinorXAxis,self).__init__(*args,**kwargs)
def reset_ticks(self):
cbook.popall(self.subminorTicks)
##self.subminorTicks.extend([self._get_tick(major=False)])
self.subminorTicks.extend([maxis.XTick(self.axes, 0, '', major=False, **self._subminor_tick_kw)])
self._lastNumSubminorTicks = 1
super(SubMinorXAxis,self).reset_ticks()
def set_subminor_locator(self, locator):
"""
Set the locator of the subminor ticker
ACCEPTS: a :class:`~matplotlib.ticker.Locator` instance
"""
self.isDefault_minloc = False
self.subminor.locator = locator
locator.set_axis(self)
self.stale = True
def set_subminor_formatter(self, formatter):
"""
Set the formatter of the subminor ticker
ACCEPTS: A :class:`~matplotlib.ticker.Formatter` instance
"""
self.isDefault_minfmt = False
self.subminor.formatter = formatter
formatter.set_axis(self)
self.stale = True
def get_subminor_ticks(self, numticks=None):
'get the subminor tick instances; grow as necessary'
if numticks is None:
numticks = len(self.get_subminor_locator()())
if len(self.subminorTicks) < numticks:
# update the new tick label properties from the old
for i in range(numticks - len(self.subminorTicks)):
##tick = self._get_tick(major=False)
tick = maxis.XTick(self.axes, 0, '', major=False, **self._subminor_tick_kw)
self.subminorTicks.append(tick)
if self._lastNumSubminorTicks < numticks:
protoTick = self.subminorTicks[0]
for i in range(self._lastNumSubminorTicks, len(self.subminorTicks)):
tick = self.subminorTicks[i]
tick.gridOn = False
self._copy_tick_props(protoTick, tick)
self._lastNumSubminorTicks = numticks
ticks = self.subminorTicks[:numticks]
return ticks
def set_tick_params(self, which='major', reset=False, **kwargs):
if which == 'subminor':
kwtrans = self._translate_tick_kw(kwargs, to_init_kw=True)
if reset:
self.reset_ticks()
self._subminor_tick_kw.clear()
self._subminor_tick_kw.update(kwtrans)
for tick in self.subminorTicks:
tick._apply_params(**self._subminor_tick_kw)
else:
super(SubMinorXAxis, self).set_tick_params(which=which, reset=reset, **kwargs)
def cla(self):
'clear the current axis'
self.set_subminor_locator(mticker.NullLocator())
self.set_subminor_formatter(mticker.NullFormatter())
super(SubMinorXAxis,self).cla()
def iter_ticks(self):
"""
Iterate through all of the major and minor ticks.
...and through the subminors
"""
majorLocs = self.major.locator()
majorTicks = self.get_major_ticks(len(majorLocs))
self.major.formatter.set_locs(majorLocs)
majorLabels = [self.major.formatter(val, i)
for i, val in enumerate(majorLocs)]
minorLocs = self.minor.locator()
minorTicks = self.get_minor_ticks(len(minorLocs))
self.minor.formatter.set_locs(minorLocs)
minorLabels = [self.minor.formatter(val, i)
for i, val in enumerate(minorLocs)]
subminorLocs = self.subminor.locator()
subminorTicks = self.get_subminor_ticks(len(subminorLocs))
self.subminor.formatter.set_locs(subminorLocs)
subminorLabels = [self.subminor.formatter(val, i)
for i, val in enumerate(subminorLocs)]
major_minor = [
(majorTicks, majorLocs, majorLabels),
(minorTicks, minorLocs, minorLabels),
(subminorTicks, subminorLocs, subminorLabels),
]
for group in major_minor:
for tick in zip(*group):
yield tick
class SubMinorAxes(maxes.Axes):
name = 'subminor'
def _init_axis(self):
self.xaxis = SubMinorXAxis(self)
self.spines['top'].register_axis(self.xaxis)
self.spines['bottom'].register_axis(self.xaxis)
self.yaxis = maxis.YAxis(self)
self.spines['left'].register_axis(self.yaxis)
self.spines['right'].register_axis(self.yaxis)
register_projection(SubMinorAxes)
if __name__ == '__main__':
fig = plt.figure()
ax = fig.add_subplot(111,projection = 'subminor')
t = np.arange(0.0, 100.0, 0.1)
s = np.sin(0.1*np.pi*t)*np.exp(-t*0.01)
majors = np.linspace(0, 100, 6)
minors = np.linspace(0, 100, 11)
thirds = np.linspace(0, 100, 101)
ax.plot(t, s)
ax.xaxis.set_ticks_position('bottom')
ax.xaxis.set_major_locator(ticker.FixedLocator(majors))
ax.xaxis.set_minor_locator(ticker.FixedLocator(minors))
ax.xaxis.set_subminor_locator(ticker.FixedLocator(thirds))
##some things in set_tick_params are not being set correctly
##by default. For instance 'top=False' must be stated
##explicitly
ax.tick_params(which='subminor', length=2, top=False)
ax.tick_params(which='minor', length=4)
ax.tick_params(which='major', length=6)
ax.grid(which='both',axis='x',linestyle='--')
plt.show()
It's not perfect, but for the use case you provided it's working fine. I drew some ideas from this matplotlib example and by going through the source codes directly. The result looks like this:
I tested the code on both Python 2.7 and Python 3.5.
EDIT:
I noticed that the subminor gridlines would always be drawn if the grid is turned on (while I had intended for it not to be drawn at all). I rectified this in the code above, i.e. the subminor ticks should never produce grid lines. If gridlines should be implemented properly, some more work will be needed.
Related
I am trying to make a virtual flight tracker with Python and API data, and I have managed so far to draw the points, update their location every 10 seconds and annotate them. Unfortunately, they are being annotated for each position they are drawn in when in fact I want them to only be shown in the spot where the point is at that time, or else there is a constant trail of labels.
The code is below:
import requests
import json
import time
import cartopy.crs as ccrs
import cartopy
import matplotlib.pyplot as plt
from cartopy.io.img_tiles import GoogleTiles
from matplotlib import animation
#SET AXES
fig, ax = plt.subplots()
ax=plt.axes(projection=ccrs.PlateCarree())
'''
ax.set_ylim(48,61.5)
ax.set_xlim(-12.5, 3.3)
'''
#ADD OSM BASEMAP
osm_tiles=GoogleTiles(style='satellite')
ax.add_image(osm_tiles,8)
ax.stock_img()
ax.set_extent([-12.5, 3.3, 48, 61.55])
ax.add_feature(cartopy.feature.BORDERS)
#mplcursors.cursor(hover=True)
#PLOT TRACK
track, = ax.plot([], [], 'wo')
# RELOAD API EVERY 15"
def update (self):
vapi = requests.get("https://data.vatsim.net/v3/vatsim-data.json")
# LOAD DATA AS JSON
data = vapi.text
parse_json = json.loads(data)
# LOAD PILOTS
pilots = parse_json['pilots']
no_pilots = len(pilots)
# GET INFO FOR EACH AIRCRAFT
x = 0
callsigns, lat, lon, alt, head, gspeed = [], [], [], [], [], []
while x < no_pilots:
xcall = pilots[x]['callsign']
xlat = pilots[x]['latitude']
xlon = pilots[x]['longitude']
xgspeed = pilots[x]['groundspeed']
xalt = pilots[x]['altitude']
xhead = pilots[x]['heading']
callsigns.append(xcall)
lat.append(xlat)
lon.append(xlon)
alt.append(xalt)
head.append(xhead)
gspeed.append(xgspeed)
x += 1
for i, txt in enumerate(callsigns):
ax.annotate(txt, (lon[i], lat[i]))
track.set_data(lon,lat)
return track, callsigns,
anim = animation.FuncAnimation(fig, update,interval=10000, blit=False)
plt.show()
It leaves a trail of annotations like this
I'm making a real-time application that graphs
sensor data in a real-time fashion.
The data is saved to a .csv file and graphed using matplotlib in the following fashion:
class Scope:
def __init__(self, ax, ax2, f, maxt=2000, dt=0.02):
self.ax = ax
self.ax2 = ax2
self.maxt = maxt
self.f = f
self.tdata = []
self.ydata = []
self.humdata = []
self.tempdata = []
self.TVOCdata = []
self.ax.set_ylim(0, 1023)
self.ax.set_xlim(0, self.maxt)
self.ax2.set_ylim(0, 100)
self.ax2.set_xlim(0, self.maxt)
self.line, = ax.plot(self.tdata, self.ydata)
self.line2, = ax2.plot(self.tdata, self.humdata)
self.line3, = ax2.plot(self.tdata, self.tempdata, 'g-')
self.line4, = ax.plot(self.tdata, self.TVOCdata)
def animate(self, y):
if path.exists(self.f):
data = pd.read_csv(self.f)
self.tdata.append(data['dur'].iloc[-1])
self.ydata.append(data['CO2'].iloc[-1])
self.line.set_data(self.tdata, self.ydata)
self.line.set_label('CO2')
I'd like to change the representation of the time axis from seconds to hours since the measurements that i'm doing might last for days at a time. Is there a handy way of doing this? Truly thankful for any kind of help.
Found a solution to this problem. The key is to use the ticker FuncFormatter-subclass to customize the ticks in the x-axis in the following way:
formatter = matplotlib.ticker.FuncFormatter(lambda s, x: time.strftime('%H:%M',time.gmtime(s // 60)))
self.ax.xaxis.set_major_formatter(mticker.FuncFormatter(formatter))
I'm attempting to subclass matplotlib.lines.Line2D to create lines whose width and dash spacings are in the same coordinates as the data being plotted. I've expanded on this answer to try to support dashed lines (with the dash spacings also being in data units) and have the following code currently:
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
class LineDataUnits(Line2D):
"""
A Line2D object, but with the linewidth and dash properties defined in data coordinates.
"""
def __init__(self, *args, **kwargs):
_lw_data = kwargs.pop("linewidth", 1)
_dashes_data = kwargs.pop("dashes", (1,))
super().__init__(*args, **kwargs)
self._lw_data = _lw_data
self._dashes_data = _dashes_data
self._dashOffset = 0
def _get_lw(self):
if self.axes is not None:
ppd = 72./self.axes.figure.dpi
trans = self.axes.transData.transform
return ((trans((1, self._lw_data))-trans((0, 0)))*ppd)[1]
else:
return 1
def _set_lw(self, lw):
self._lw_data = lw
def _get_dashes(self):
if self.axes is not None:
ppd = 72./self.axes.figure.dpi
trans = self.axes.transData.transform
return tuple([((trans((1, dash_data))-trans((0, 0)))*ppd)[1] for dash_data in self._dashes_data])
else:
return tuple((1, 0))
def _set_dashes(self, dashes):
self._dashes_data = dashes
_linewidth = property(_get_lw, _set_lw)
_dashSeq = property(_get_dashes, _set_dashes)
if __name__ == "__main__":
fig, ax = plt.subplots()
line1 = Line2D([0, 10], [3, 3], linewidth=10, color="blue", dashes=(3, 3), dash_capstyle="round")
line2 = LineDataUnits([0, 10], [6, 6], linewidth=0.5, color="red", dashes=(1, 1), dash_capstyle="round")
print("dash_capstyle:", line2.get_dash_capstyle())
ax.add_line(line1)
ax.add_line(line2)
ax.set_xticks(range(10))
ax.grid()
ax.set_xlim(0, 10)
ax.set_ylim(0, 10)
plt.show()
Which yields the following graph:
When I zoom in and out, the dash spacings do seem to remain constant in the data units, however as you can see in the graph, their values are not calculated correctly. Furthermore, it appears setting the dash_capstyle parameter in this class seems to have no effect.
Any help would be greatly appreciated.
Edit: Solution
After much more experimentation, I managed to get it working properly. Firstly, even though the line was being plotted with dashes, the Line2D still had its _linestyle parameter set to "-" (solid), which caused the cap style argument to be ignored. Secondly, there was an error in my conversion from data units to points. With the changes below, it now works perfectly.
# in __init__()
super().__init__(*args, **kwargs):
+ self.set_linestyle("--")
self._lw_data = _lw_data
# in _get_dashes()
- return tuple([((trans((1, dash_data))-trans((0, 0)))*ppd)[1] for dash_data in self._dashes_data])
+ dpu = (trans((1, 1)) - trans((0, 0)))[0]
+ return tuple([u*dpu*ppd for u in self._dashes_data])
# in __init__()
super().__init__(*args, **kwargs):
+ self.set_linestyle("--")
self._lw_data = _lw_data
# in _get_dashes()
- return tuple([((trans((1, dash_data))-trans((0, 0)))*ppd)[1] for dash_data in self._dashes_data])
+ dpu = (trans((1, 1)) - trans((0, 0)))[0]
+ return tuple([u*dpu*ppd for u in self._dashes_data])
I'm trying to animate a scatterplot but get the following error. I had it working previously but its now returning this error on repeat.
ValueError: 'vertices' must be a 2D list or array with shape Nx2
I'll attach the animation code below. I had it working before so know it works.
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
import matplotlib.transforms as transforms
XA = np.random.randint(80, size=(1000, 15))
YA = np.random.randint(80, size=(1000, 15))
XB = np.random.randint(80, size=(1000, 15))
YB = np.random.randint(80, size=(1000, 15))
XC = np.random.randint(80, size=(1000, 1))
YC = np.random.randint(80, size=(1000, 1))
fig, ax = plt.subplots(figsize = (10,6))
ax.axis('equal')
'''' Scatter Plot '''
scatter_A = ax.scatter(XA[0], YA[0], c=['blue'], alpha = 0.7, s = 20, edgecolor = 'black', zorder = 2)
scatter_B = ax.scatter(XB[0], YB[0], c=['white'], alpha = 0.7, s = 20, edgecolor = 'black', zorder = 2)
offset = lambda p: transforms.ScaledTranslation(p/82.,0, plt.gcf().dpi_scale_trans)
trans = plt.gca().transData
scatter_C = ax.scatter(XC[0], YC[0], c=['red'], marker = 'o', alpha = 0.7, s = 10, edgecolor = 'black', zorder = 2,transform=trans+offset(+2))
'''Animate Function '''
def animate(i) :
scatter_A.set_offsets([[[[[[[[[[[[[[[XA[0+i][0], YA[0+i][0]], [XA[0+i][1], YA[0+i][1]], [XA[0+i][2], YA[0+i][2]], [XA[0+i][3], YA[0+i][3]], [XA[0+i][4], YA[0+i][4]],[XA[0+i][5], YA[0+i][5]], [XA[0+i][6], YA[0+i][6]], [XA[0+i][7], YA[0+i][7]], [XA[0+i][8], YA[0+i][8]], [XA[0+i][9], YA[0+i][9]], [XA[0+i][10], YA[0+i][10]], [XA[0+i][11], YA[0+i][11]], [XA[0+i][12], YA[0+i][12]], [XA[0+i][13], YA[0+i][13]], [XA[0+i][14], YA[0+i][14]]]]]]]]]]]]]]]])
scatter_B.set_offsets([[[[[[[[[[[[[[[XB[0+i][0], YB[0+i][0]], [XB[0+i][1], YB[0+i][1]], [XB[0+i][2], YB[0+i][2]], [XB[0+i][3], YB[0+i][3]], [XB[0+i][4], YB[0+i][4]],[XB[0+i][5], YB[0+i][5]], [XB[0+i][6], YB[0+i][6]], [XB[0+i][7], YB[0+i][7]], [XB[0+i][8], YB[0+i][8]], [XB[0+i][9], YB[0+i][9]], [XB[0+i][10], YB[0+i][10]], [XB[0+i][11], YB[0+i][11]], [XB[0+i][12], YB[0+i][12]], [XB[0+i][13], YB[0+i][13]], [XB[0+i][14], YB[0+i][14]]]]]]]]]]]]]]]])
scatter_C.set_offsets([[XC[0+i][0], YC[0+i][0]]])
ani = animation.FuncAnimation(fig, animate, np.arange(0,1000),
interval = 100, blit = False)
Writer = animation.writers['ffmpeg']
writer = Writer(fps = 10, bitrate = 8000)
ax.autoscale()
plt.draw()
I am running Spyder 3.1.2 through Anaconda 1.6.4, Python 3.5, Python 5.1.0
The error message should give you all the hints you need. Removing the redundant brackets in your set_offsets() calls does the trick:
def animate(i) :
scatter_A.set_offsets([[XA[0+i][0], YA[0+i][0]], [XA[0+i][1], YA[0+i][1]], [XA[0+i][2], YA[0+i][2]], [XA[0+i][3], YA[0+i][3]], [XA[0+i][4], YA[0+i][4]],[XA[0+i][5], YA[0+i][5]], [XA[0+i][6], YA[0+i][6]], [XA[0+i][7], YA[0+i][7]], [XA[0+i][8], YA[0+i][8]], [XA[0+i][9], YA[0+i][9]], [XA[0+i][10], YA[0+i][10]], [XA[0+i][11], YA[0+i][11]], [XA[0+i][12], YA[0+i][12]], [XA[0+i][13], YA[0+i][13]], [XA[0+i][14], YA[0+i][14]]])
scatter_B.set_offsets([[XB[0+i][0], YB[0+i][0]], [XB[0+i][1], YB[0+i][1]], [XB[0+i][2], YB[0+i][2]], [XB[0+i][3], YB[0+i][3]], [XB[0+i][4], YB[0+i][4]],[XB[0+i][5], YB[0+i][5]], [XB[0+i][6], YB[0+i][6]], [XB[0+i][7], YB[0+i][7]], [XB[0+i][8], YB[0+i][8]], [XB[0+i][9], YB[0+i][9]], [XB[0+i][10], YB[0+i][10]], [XB[0+i][11], YB[0+i][11]], [XB[0+i][12], YB[0+i][12]], [XB[0+i][13], YB[0+i][13]], [XB[0+i][14], YB[0+i][14]]])
scatter_C.set_offsets([[XC[0+i][0], YC[0+i][0]]])
I'm surprised that your code worked before. Note that I'm not running exactly the same setup, I'm on macosx 10.13.5 with Python 3.6 installed through macports.
I'd like to create a barplot in matplotlib:
fig, ax = plt.subplots()
oldbar = ax.bar(x=ind, height=y, width=width)
I'd then like to pickle this barplot to file (either the dictionary or the axes - I'm not sure which is correct):
pickle.dump(oldbar, file('oldbar.pkl', 'w'))
I'd then like to reload this file, and then plot the old bar onto alongside a new bar plot, so I can compare them on a single axes:
fig, ax = plt.subplots()
newbar = ax.bar(x=ind, height=y, width=width)
oldbar = pickle.load(file('oldbar.pkl'))
# I realise the line below doesn't work
ax.bar(oldbar)
plt.show()
Ideally, I'd then like to present them as below. Any suggestions of how I might go about this?
You would pickle the figure instead the artists in it.
import matplotlib.pyplot as plt
import numpy as np
import pickle
ind = np.linspace(1,5,5)
y = np.linspace(9,1,5)
width = 0.3
fig, ax = plt.subplots()
ax.bar(x=ind, height=y, width=width)
ax.set_xlabel("x label")
pickle.dump(fig, file('oldbar.pkl', 'w'))
plt.close("all")
ind2 = np.linspace(1,5,5)
y2 = np.linspace(8,2,5)
width2 = 0.3
fig2 = pickle.load(file('oldbar.pkl'))
ax2 = plt.gca()
ax2.bar(x=ind2+width, height=y2, width=width2, color="C1")
plt.show()
However pickling the data itself may make more sense here.
import matplotlib.pyplot as plt
import numpy as np
import pickle
ind = np.linspace(1,5,5)
y = np.linspace(9,1,5)
width = 0.3
dic = {"ind":ind, "y":y, "width":width}
pickle.dump(dic, file('olddata.pkl', 'w'))
### new data
ind2 = np.linspace(1,5,5)
y2 = np.linspace(8,2,5)
width2 = 0.3
olddic = pickle.load(file('olddata.pkl'))
fig, ax = plt.subplots()
ax.bar(x=olddic["ind"], height=olddic["y"], width=olddic["width"])
ax.bar(x=ind2+olddic["width"], height=y2, width=width2)
ax.set_xlabel("x label")
plt.show()
Maybe this will help:
import pickle as pkl
import matplotlib.pyplot as plt
import numpy as np
class Data_set(object):
def __init__(self, x=[], y=[], name='data', pklfile=None,
figure=None, axes=None):
"""
"""
if pklfile is None:
self.x = np.asarray(x)
self.y = np.asarray(y)
self.name = str(name)
else:
self.unpickle(pklfile)
self.fig = figure
self.ax = axes
self.bar = None
def plot(self, width=0, offset=0, figure=None, axes=None):
if self.fig is None:
if figure is None:
self.fig = plt.figure()
self.ax = self.fig.subplots(1, 1)
else:
self.fig = figure
if axes is None:
self.ax = self.fig.subplots(1, 1)
else:
self.ax = axes
# maybe there's no need to keep track of self.fig, .ax and .bar,
# but just in case...
if figure is not None:
fig_to_use = figure
if axes is not None:
ax_to_use = axes
else:
ax_to_use = fig_to_use.subplots(1, 1)
else:
fig_to_use = self.fig
ax_to_use = self.ax
if not width:
width = (self.x[1]-self.x[0]) / 2.
self.bar = ax_to_use.bar(x=self.x+offset, height=self.y, width=width)
return fig_to_use, ax_to_use, self.bar
def pickle(self, filename='', ext='.pkl'):
if filename == '':
filename = self.name
with open(filename+ext, 'w') as output_file:
pkl.dump((self.name, self.x, self.y), output_file)
def unpickle(self, filename='', ext='.pkl'):
if filename == '':
filename = self.name
with open(filename + ext, 'r') as input_file:
# the name should really come from the filename, but then the
# above would be confusing?
self.name, self.x, self.y = pkl.load(input_file)
class Data_set_manager(object):
def __init__(self, datasets={}):
self.datasets = datasets
def add_dataset(self, data_set):
self.datasets[data_set.name] = data_set
def add_dataset_from_file(self, filename, ext='.pkl'):
self.datasets[filename] = Data_set(name=filename)
self.datasets[filename].unpickle(filename=filename, ext=ext)
def compare(self, width=0, offset=0, *args):
self.fig = plt.figure()
self.ax = self.fig.subplots(1, 1)
if len(args) == 0:
args = self.datasets.keys()
args.sort()
n = len(args)
if n == 0:
return None, None
if width == 0:
min_dx = None
for dataset in self.datasets.values():
sorted_x = dataset.x.copy()
sorted_x.sort()
try:
new_min_dx = np.min(dataset.x[1:] - dataset.x[:-1])
except ValueError:
# zero-size array to reduction operation minimum which
# has no identity (empty array)
new_min_dx = None
if new_min_dx < min_dx or min_dx is None:
min_dx = new_min_dx
if min_dx is None:
min_dx = 1.
width = float(min_dx) / (n + 1)
offset = float(min_dx) / (n + 1)
offsets = offset*np.arange(n)
if n % 2 == 0:
offsets -= offsets[n/2] - offset/2.
else:
offsets -= offsets[n/2]
i = 0
for name in args:
self.datasets.get(name, Data_set()).plot(width=width,
offset=offsets[i],
figure=self.fig,
axes=self.ax)
i += 1
self.ax.legend(args)
return self.fig, self.ax
if __name__ == "__main__":
# test saving/loading
name = 'test'
to_pickle = Data_set(x=np.arange(10),
y=np.random.rand(10),
name=name)
to_pickle.pickle()
unpickled = Data_set(pklfile=name)
print unpickled.name == to_pickle.name
# test comparison
blorg = Data_set_manager({})
x_step = 1.
n_bars = 4 # also try an odd number
for n in range(n_bars):
blorg.add_dataset(Data_set(x=x_step * np.arange(n_bars),
y=np.random.rand(n_bars),
name='teste' + str(n)))
fig, ax = blorg.compare()
fig.show()
It should work with both even and odd number of bars:
And as long as you keep a record of the names you've used (tip:look in the folder where you are saving them) you can reload the data and compare it with the new one.
More checks could be made (to make sure the file exists, that the x axis is something that can be subtracted before trying to do so, etc.), and it could also use some documentation and proper testing - but this should do in a hurry.