bendichter / brokenaxes

Create matplotlib plots with broken axes
MIT License
526 stars 47 forks source link

Should BrokenAxes also work for bar plots ? #64

Closed JanPolcher closed 3 years ago

JanPolcher commented 3 years ago

hello, here is a simple case which does not work for me

import matplotlib.pyplot as plt
from brokenaxes import brokenaxes
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,17,35,29,12]
fig,ax = plt.subplots()
ax = brokenaxes(ylims=((0, 5), (10, 35)), hspace=.05)
ax.bar(langs, students)
ax.set_xticklabels(langs)
plt.show()

The axis labelling done by the bar plot does not seem to be compatible with the changes BrokenAxes applies. Can this be fixed ?

bendichter commented 3 years ago

Thank you for including a minimal example.

import matplotlib.pyplot as plt
from brokenaxes import brokenaxes
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,17,35,29,12]
ax = brokenaxes(ylims=((0, 5), (10, 35)), hspace=.05)
ax.bar(langs, students)
ax.big_ax.set_xticklabels(langs)
plt.show()
JanPolcher commented 3 years ago

Sorry, I could break it again :

import matplotlib.pyplot as plt
from brokenaxes import brokenaxes
x = [1,2,3,4,5]
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,17,35,29,12]
broken=True
fig,ax = plt.subplots()
if broken :
    ax = brokenaxes(ylims=((0, 5), (10, 35)), hspace=.05)
    ax.bar(x, students)
    ax.big_ax.set_xticks(x)
    ax.big_ax.set_xticklabels(langs)
else :
    ax.bar(x, students)
    ax.set_xticks(x)
    ax.set_xticklabels(langs)
plt.show()

I cannot figure out how to suppress the default axis labelling. Going through the big_ax method. The strange thing is that I change the labelling of the un-broken axis !

zpatty commented 3 years ago

I've got a similar issue. The default axis labels are not suppressed.

import matplotlib.pyplot as plt
import numpy as np
from brokenaxes import brokenaxes

SMALL_SIZE = 8
MEDIUM_SIZE = 10
BIGGER_SIZE = 12
BIG_SIZE = 20

plt.rc('font', size=BIG_SIZE)          # controls default text sizes
plt.rc('axes', titlesize=BIG_SIZE)     # fontsize of the axes title
plt.rc('axes', labelsize=BIG_SIZE)    # fontsize of the x and y labels
plt.rc('xtick', labelsize=BIG_SIZE)    # fontsize of the tick labels
plt.rc('ytick', labelsize=BIG_SIZE)    # fontsize of the tick labels
plt.rc('legend', fontsize=SMALL_SIZE)    # legend fontsize
plt.rc('figure', titlesize=30)  # fontsize of the figure title

fig = plt.figure(figsize=(16, 10))
ax = brokenaxes(xlims=((0, 850), (2500, 2800)))

ax.broken_barh([(300, 200)], (31, 8),
               facecolors='tab:orange')

ax.broken_barh([(300, 400)], (21, 8), facecolors='tab:blue')

ax.broken_barh([(600, 100)], (11, 8),
               facecolors='tab:red')

ax.broken_barh([(100, 200), (2600, 200)], (41, 8),
               facecolors='tab:green')

ax.set_title('Gait Cycle for each activated limb')
ax.set_ylim(5, 55)
ax.set_xlabel('Time (ms)')
ax.big_ax.set_yticks([10, 20, 30, 40])
ax.big_ax.set_yticklabels(['Rear Up', 'Rear Down', 'Fore Down', 'Fore Up'])
ax.set_yticks([10, 20, 30, 40, 50], minor=True)
#ax.set_xticks(np.arange(100,2100,100), minor=True)
ax.grid(axis ='both', which='minor')
ax.grid(axis = 'both', which ='major')

plt.show()
bendichter commented 3 years ago

@zpatty Sorry for missing this earlier. This should give you what you are looking for:

import matplotlib.pyplot as plt
import numpy as np
from brokenaxes import brokenaxes

SMALL_SIZE = 8
MEDIUM_SIZE = 10
BIGGER_SIZE = 12
BIG_SIZE = 20

plt.rc('font', size=BIG_SIZE)          # controls default text sizes
plt.rc('axes', titlesize=BIG_SIZE)     # fontsize of the axes title
plt.rc('axes', labelsize=BIG_SIZE)    # fontsize of the x and y labels
plt.rc('xtick', labelsize=BIG_SIZE)    # fontsize of the tick labels
plt.rc('ytick', labelsize=BIG_SIZE)    # fontsize of the tick labels
plt.rc('legend', fontsize=SMALL_SIZE)    # legend fontsize
plt.rc('figure', titlesize=30)  # fontsize of the figure title

fig = plt.figure(figsize=(16, 10))
ax = brokenaxes(xlims=((0, 850), (2500, 2800)))

ax.broken_barh([(300, 200)], (31, 8),
               facecolors='tab:orange')

ax.broken_barh([(300, 400)], (21, 8), facecolors='tab:blue')

ax.broken_barh([(600, 100)], (11, 8),
               facecolors='tab:red')

ax.broken_barh([(100, 200), (2600, 200)], (41, 8),
               facecolors='tab:green')

ax.set_title('Gait Cycle for each activated limb')
ax.set_ylim(5, 55)
ax.set_xlabel('Time (ms)', labelpad=30)
ax.axs[0].set_yticks([10, 20, 30, 40])
ax.axs[0].set_yticklabels(['Rear Up', 'Rear Down', 'Fore Down', 'Fore Up'])
ax.set_yticks([10, 20, 30, 40, 50], minor=True)
#ax.set_xticks(np.arange(100,2100,100), minor=True)
ax.grid(axis ='both', which='minor')
ax.grid(axis = 'both', which ='major')

plt.show()