matplotlib中条形图内的轴斜线标记是否损坏? [英] Broken axis slash marks inside bar chart in matplotlib?

查看:68
本文介绍了matplotlib中条形图内的轴斜线标记是否损坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过matplotlib在轴上放置断开的轴斜杠标记的示例,例如

解决方案

仅显示原理,您可以在条形超出下轴上限的每个位置以及条形超出的每个位置放置相同类型的线超过上轴的下限.

 将pandas导入为pd导入matplotlib.pyplot作为plt将numpy导入为npXX = pd.Series([200,400,100,1400],index = ['x1','x2','x3','x4'])图,(ax1,ax2)= plt.subplots(2,1,sharex = True,figsize =(5,6))ax1.spines ['bottom'].set_visible(False)ax1.tick_params(axis ='x',which ='both',bottom = False)ax2.spines ['top'].set_visible(False)bs = 500ts = 1000ax2.set_ylim(0,bs)ax1.set_ylim(ts,1500)ax1.set_yticks(np.arange(1000,1501,100))bars1 = ax1.bar(XX.index,XX.values)bars2 = ax2.bar(XX.index,XX.values)对于ax2.get_xticklabels()中的刻度:tick.set_rotation(0)d = 0.015kwargs = dict(transform = ax1.transAxes,color ='k',clip_on = False)ax1.plot((-d,+ d),(-d,+ d),** kwargs)ax1.plot((1- d,1 + d),(-d,+ d),** kwargs)kwargs.update(transform = ax2.transAxes)ax2.plot((-d,+ d),(1-d,1 + d),** kwargs)ax2.plot((1- d,1 + d),(1- d,1 + d),** kwargs)对于zip(bars1,bars2)中的b1,b2:posx = b2.get_x()+ b2.get_width()/2.如果b2.get_height()>bs:ax2.plot((posx-3 * d,posx + 3 * d),(1-d,1 + d),color ='k',clip_on = False,transform = ax2.get_xaxis_transform())如果b1.get_height()>ts:ax1.plot((posx-3 * d,posx + 3 * d),(-d,+ d),color ='k',clip_on = False,transform = ax1.get_xaxis_transform())plt.show() 

它看起来不太好,但是当然可以采用更好的形状.

I've seen matplotlib examples of placing the broken axis slash marks on the axes, such as this one.

My questions, how can I place it where the bars are broken? Can this be done in a programmatic way for updating time-series plots month over month?

Below is an example of what I want done, using excel. Notice the tildes in the Paris June bar and the Madrid May bar. The tildes hide a portion of the bar it's covering.

I'm also providing easier sample data and what I've been able to do so far.

XX = pd.Series([200,400,100,1400],index=['x1','x2','x3','x4'])
fig, (ax1,ax2) = plt.subplots(2,1,sharex=True,
                         figsize=(5,6))
ax1.spines['bottom'].set_visible(False)
ax1.tick_params(axis='x',which='both',bottom=False)
ax2.spines['top'].set_visible(False)
ax2.set_ylim(0,500)
ax1.set_ylim(1200,1500)
ax1.set_yticks(np.arange(1000,1501,100))
XX.plot(ax=ax1,kind='bar')
XX.plot(ax=ax2,kind='bar')
for tick in ax2.get_xticklabels():
    tick.set_rotation(0)
d = .015  
kwargs = dict(transform=ax1.transAxes, color='k', clip_on=False)
ax1.plot((-d, +d), (-d, +d), **kwargs)      
ax1.plot((1 - d, 1 + d), (-d, +d), **kwargs)
kwargs.update(transform=ax2.transAxes)  
ax2.plot((-d, +d), (1 - d, 1 + d), **kwargs)  
ax2.plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs)
plt.show()

解决方案

Just to show the principle, you can put the same kind of line at every position where a bar exceeds the upper limit of the lower axes and also where a bar exceeds the lower limit of the upper axes.

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

XX = pd.Series([200,400,100,1400],index=['x1','x2','x3','x4'])
fig, (ax1,ax2) = plt.subplots(2,1,sharex=True,
                         figsize=(5,6))
ax1.spines['bottom'].set_visible(False)
ax1.tick_params(axis='x',which='both',bottom=False)
ax2.spines['top'].set_visible(False)

bs = 500
ts = 1000

ax2.set_ylim(0,bs)
ax1.set_ylim(ts,1500)
ax1.set_yticks(np.arange(1000,1501,100))

bars1 = ax1.bar(XX.index, XX.values)
bars2 = ax2.bar(XX.index, XX.values)

for tick in ax2.get_xticklabels():
    tick.set_rotation(0)
d = .015  
kwargs = dict(transform=ax1.transAxes, color='k', clip_on=False)
ax1.plot((-d, +d), (-d, +d), **kwargs)      
ax1.plot((1 - d, 1 + d), (-d, +d), **kwargs)
kwargs.update(transform=ax2.transAxes)  
ax2.plot((-d, +d), (1 - d, 1 + d), **kwargs)  
ax2.plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs)

for b1, b2 in zip(bars1, bars2):
    posx = b2.get_x() + b2.get_width()/2.
    if b2.get_height() > bs:
        ax2.plot((posx-3*d, posx+3*d), (1 - d, 1 + d), color='k', clip_on=False,
                 transform=ax2.get_xaxis_transform())
    if b1.get_height() > ts:
        ax1.plot((posx-3*d, posx+3*d), (- d, + d), color='k', clip_on=False,
                 transform=ax1.get_xaxis_transform())
plt.show()

It doesn't look great, but can of course be adapted with a nicer shape.

这篇关于matplotlib中条形图内的轴斜线标记是否损坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆