【问题标题】:how can i delete the short lines of the colorbar in matplotlib如何删除 matplotlib 中颜色条的短线
【发布时间】:2018-03-31 10:01:37
【问题描述】:

我用 matplotlib 绘图,我在颜色栏中有短线:

如何删除短线以获得这样的颜色条:

我需要在我的代码中进行哪些更改? 我的代码在这里:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import unicode_literals
from matplotlib import rc
from matplotlib.colors import LogNorm
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
import matplotlib as cm

phi = open("file1.txt").read().splitlines()
psi = open("file2.txt").read().splitlines()

# normal distribution center at x=0 and y=5
x = np.array(phi).astype(np.float)
y = np.array(psi).astype(np.float)

plt.hist2d(x, y, bins=400, cmap='bone', norm=LogNorm())
plt.colorbar()
plt.title('Distribution')
plt.xlabel(r' Phi ($\phi$)', fontsize=15)
plt.ylabel(r'Psi ($\psi$)', fontsize=15)
plt.rc('xtick', labelsize=11)
plt.rc('ytick', labelsize=11)
plt.xlim((-180,180))
plt.ylim((-180,180))
plt.axhline(0, color='black')
plt.axvline(0, color='black')
plt.grid()
plt.show()

【问题讨论】:

    标签: matplotlib plot colorbar short


    【解决方案1】:

    我想你的意思是你想从颜色栏中删除次要刻度。

    您可以通过使用来自matplotlib.ticker 模块的LogLocator 设置刻度的位置来做到这一点。默认情况下,这没有任何次要刻度(即基数的整数幂之间没有刻度),因此我们可以使用 LogLocator 而无需其他选项。

    您只需要在制作时保留对colorbar 的引用(这里我称之为cb),然后您可以使用set_ticks() 方法。确保您还设置了update_ticks=True,以便在绘制之前更新刻度。

    (我使用了一些假数据,但没有更改脚本中的其他任何内容):

    import matplotlib.ticker as ticker
    
    ...
    
    cb = plt.colorbar()
    cb.set_ticks(ticker.LogLocator(), update_ticks=True)
    
    ...
    

    【讨论】:

      【解决方案2】:

      要完全删除刻度线(“如何删除短线?”),您可以在颜色条的轴上使用 tick_params

      cbar = plt.colorbar()
      cbar.ax.tick_params(size=0)
      

      【讨论】:

        猜你喜欢
        • 2015-02-24
        • 2011-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-15
        • 1970-01-01
        • 2021-01-05
        • 2019-02-07
        相关资源
        最近更新 更多