Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Matplotlib colorbar moves second x axis drawn with twiny #5904

Closed
Labels
Difficulty: Easyhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuesGood first issueOpen a pull request against these issues if there are no active ones!status: has patchpatch suggested, PR still needed
@Gabriel-p

Description

@Gabriel-p

Bug report originated in SO post here:http://stackoverflow.com/q/34979781/1391441


I'm trying to add a second x axis to the top of a plot usingtwiny.

If I make a simple scatter plot with no colorbar, the top x axis is correctly aligned with the bottom x axis (MWE is below):

enter image description here

If I add a colorbar though, the top x axis is displaced:

enter image description here


MWE

import numpy as npimport matplotlib.pyplot as pltfrom mpl_toolkits.axes_grid1 import make_axes_locatableimport matplotlib.gridspec as gridspecX = np.array([0., 0.5, 1., 1.5, 2., 2.5, 3., 3.5, 4.])X2 = np.array([122, 85, 63, 50, 23, 12, 7, 5, 2])Y = np.cos(X*20)Z = np.sin(X*20)fig = plt.figure()gs = gridspec.GridSpec(1, 2)ax1 = plt.subplot(gs[1])ax2 = ax1.twiny()ax1.set_xlim(-0.2, max(X)+0.2)plt.tick_params(axis='both', which='major', labelsize=10)ax1.minorticks_on()ax1.grid(b=True, which='major', color='gray', linestyle='--', lw=0.3)SC = ax1.scatter(X, Y, c=Z)ax1.set_xlabel("Original x-axis")ax2.set_xlim(ax1.get_xlim())ax2.set_xticks(X)ax2.set_xticklabels(X2)ax2.set_xlabel("Second x-axis")# Colorbar.the_divider = make_axes_locatable(ax1)color_axis = the_divider.append_axes("right", size="2%", pad=0.1)cbar = plt.colorbar(SC, cax=color_axis)cbar.set_label('B', fontsize=10, labelpad=4, y=0.5)cbar.ax.tick_params(labelsize=10)plt.show()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Difficulty: Easyhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuesGood first issueOpen a pull request against these issues if there are no active ones!status: has patchpatch suggested, PR still needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp