简体   繁体   中英

Repeated Categorical X-Axis Labels in Matplotlib

I have a simple question: why are my x-axis labels repeated?

MWE 图

Here's an MWE: X-Axis Labels MWE

a = { # DATA -- 'CATEGORY': (VALUE, ERROR) 
      'Cats': (1, 0.105), 
      'Dogs': (2, 0.023), 
      'Pigs': (2.6, 0.134)
     }

compositions = list(a.keys()) # MAKE INTO LIST

a_vals = [i[0] for i in a.values()] # EXTRACT VALUES
a_errors = [i[1] for i in a.values()] # EXTRACT ERRORS

fig = plt.figure(figsize=(8, 6)) # DICTATE FIGURE SIZE   
bax = brokenaxes(ylims=((0,1.5), (1.7, 3)), hspace = 0.05) # BREAK AXES 
bax.plot(compositions, a_vals, marker = 'o') # PLOT DATA

for i in range(0, len(a_errors)): # PLOT ALL ERROR BARS
  bax.errorbar(i, a_vals[i], yerr = a_errors[i], capsize = 5, fmt = 'red') # FORMAT ERROR BAR

Here's stuff I tried:

You can use bax.locator_params(axis='x', nbins=len(compositions)) to reduce the number of x-ticks.

import matplotlib.pyplot as plt
from brokenaxes import brokenaxes

a = {  # DATA -- 'CATEGORY': (VALUE, ERROR)
    'Cats': (1, 0.105),
    'Dogs': (2, 0.023),
    'Pigs': (2.6, 0.134)
}

compositions = list(a.keys())  # MAKE INTO LIST

a_vals = [i[0] for i in a.values()]  # EXTRACT VALUES
a_errors = [i[1] for i in a.values()]  # EXTRACT ERRORS

fig = plt.figure(figsize=(8, 6))  # DICTATE FIGURE SIZE
bax = brokenaxes(ylims=((0, 1.5), (1.7, 3)), hspace=0.05)  # BREAK AXES
bax.plot(compositions, a_vals, marker='o')  # PLOT DATA

for i in range(0, len(a_errors)):  # PLOT ALL ERROR BARS
    bax.errorbar(i, a_vals[i], yerr=a_errors[i], capsize=5, fmt='red')  # FORMAT ERROR BAR
    
bax.locator_params(axis='x', nbins=len(compositions))

plt.show()

Result: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM