Skip to content
Snippets Groups Projects
Unverified Commit e3e5ba2d authored by Benjamin Loison's avatar Benjamin Loison
Browse files

Make `plt.{savefig,show}` work fine with dates

parent f237261b
No related branches found
No related tags found
No related merge requests found
......@@ -16,8 +16,6 @@ with open('data_keeping_all_blocks_since_last_m_higher_block.json') as f:
dataKeepingAllBlocksSinceLastMHigherBlock = json.load(f)
fig, targetsAxis = plt.subplots()
fig.subplots_adjust(right = 0.8)
fig.subplots_adjust(bottom = 0.4)
compressedSizeAxis = targetsAxis.twinx()
compressedScoreAxis = targetsAxis.twinx()
axes = [targetsAxis, compressedSizeAxis, compressedScoreAxis, targetsAxis.twinx(), compressedScoreAxis, compressedSizeAxis, compressedScoreAxis]
......@@ -26,8 +24,6 @@ targets = data['targets']
timestamps = [datetime.datetime.fromtimestamp(timestamp) for timestamp in data['timestamps']]
blockHeights = {timestamp: timestampsIndex for timestampsIndex, timestamp in enumerate(timestamps)}
for x, (target, nextTarget) in enumerate(zip(targets, targets[1:])):
if nextTarget < target:
firstTargetDecreaseCurve = plt.axvline(x = timestamps[x], color = 'purple', label = 'first target decrease')
......@@ -46,10 +42,18 @@ axesColors = ['blue', 'green', 'brown', 'orange', 'brown', 'red', 'purple']
axesLabels = ['targets', 'compressed blockchain size (in blocks)', 'compressed blockchain score', 'constant difficulty compressed blockchain size (in blocks)', 'constant difficulty compressed blockchain score', 'compressed blockchain size keeping all blocks since last m higher block (in blocks)', 'compressed blockchain score keeping all blocks since last m higher block']
axesYValues = [targets, data['compressSize'], data['compressScore'], dataConstantDifficulty['compressSize'], dataConstantDifficulty['compressScore'], dataKeepingAllBlocksSinceLastMHigherBlock['compressSize'], dataKeepingAllBlocksSinceLastMHigherBlock['compressScore']]
blockHeightsCache = {}
class CustomDateFormatter(mdates.ticker.Formatter):
def __call__(self, x, pos=0):
datetime_ = mdates.num2date(x)
result = datetime_.strftime(f'%m/%Y - {blockHeights[datetime_]}')
if not x in blockHeightsCache:
for timestampsIndex, timestamp in enumerate(timestamps):
if datetime_.timestamp() <= timestamp.timestamp():
blockHeightsCache[x] = timestampsIndex
break
blockHeight = f' - {blockHeightsCache[x]}' if x in blockHeightsCache else ''
result = datetime_.strftime('%m/%Y') + blockHeight
return result
plt.gca().xaxis.set_major_formatter(CustomDateFormatter())
......@@ -78,9 +82,9 @@ curves.insert(-2, legendNewLine[0])
curves += legendNewLine + targetRecomputationCurve + [firstTargetDecreaseCurve] + [targetIncreaseCurve]
labels = [curve.get_label() for curve in curves]
#plt.legend(curves, labels, loc='upper center', framealpha=1, bbox_to_anchor=(0.5, -0.125))
plt.legend(curves, labels, loc='upper center', framealpha=1, bbox_to_anchor=(0.5, -0.345))
targetsAxis.set_xlabel('Block height')
targetsAxis.set_xlabel('Block date and height')
for ax, color in zip(axes, axesColors):
ax.tick_params(axis='y', colors=color)
......@@ -88,6 +92,11 @@ for ax, color in zip(axes, axesColors):
title = 'Variable difficulty Bitcoin compressed blockchain evolution'
plt.title(title)
plt.show()
fig.subplots_adjust(top=0.965,
bottom=0.525,
left=0.125,
right=0.8)
fig.set_size_inches((11.2, 7.5), forward=False)
#plt.savefig(f'{title.lower().replace(" ", "")}.svg')
\ No newline at end of file
plt.savefig(f'{title.lower().replace(" ", "_")}.svg')
plt.show()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment