Home > Back-end >  Broken scatter graph with color bar?
Broken scatter graph with color bar?

Time:09-03

I am trying to make a scatter graph with a broken axis alongside a color bar. Ive converged on this code so far, but the color bar interferes with the right axis. Any help on this would be much appreciated. J

import numpy as np
import matplotlib.pyplot as plt

x = [1, 2]
y = [1, 2]
z = [0.1, 0.2]


# If we were to simply plot pts, we'd lose most of the interesting
# details due to the outliers. So let's 'break' or 'cut-out' the y-axis
# into two portions - use the top (ax1) for the outliers, and the bottom
# (ax2) for the details of the majority of our data
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
fig.subplots_adjust(hspace=0.05)  # adjust space between axes
sc = ax1.scatter(x, y, c=[z])
sc= ax2.scatter(x, y, c=[z])
cbar = fig.colorbar(sc)

# zoom-in / limit the view to different portions of the data
ax1.set_ylim(35, 45)  # outliers only
ax2.set_ylim(10, 15)  # most of the data

# hide the spines between ax and ax2
ax1.spines.bottom.set_visible(False)
ax2.spines.top.set_visible(False)
ax1.xaxis.tick_top()
ax1.tick_params(labeltop=False)  # don't put tick labels at the top
ax2.xaxis.tick_bottom()

# Now, let's turn towards the cut-out slanted lines.
# We create line objects in axes coordinates, in which (0,0), (0,1),
# (1,0), and (1,1) are the four corners of the axes.
# The slanted lines themselves are markers at those locations, such that the
# lines keep their angle and position, independent of the axes size or scale
# Finally, we need to disable clipping.

d = .5  # proportion of vertical to horizontal extent of the slanted line
kwargs = dict(marker=[(-1, -d), (1, d)], markersize=12,
              linestyle="none", color='k', mec='k', mew=1, clip_on=False)
ax1.plot([0, 1], [0, 0], transform=ax1.transAxes, **kwargs)
ax2.plot([0, 1], [1, 1], transform=ax2.transAxes, **kwargs)


plt.show()

CodePudding user response:

You can place the colorbar in its own axis (as in enter image description here

  • Related