3

Is there any possibility to do a bar plot without y-(x-)axis? In presentations all redundant informations have to be erased, so I would like to begin to delete the axis. I did not see helpful informations in the matplotlib documentation. Maybe you have better solutions than pyplot..?

Edit: I would like to have lines around the bars except the axis at the bottom. Is this possible

#!/usr/bin/env python 
import matplotlib.pyplot as plt

ind = (1,2,3)
width = 0.8
fig = plt.figure(1)
p1 = plt.bar(ind,ind)
# plt.show() 
fig.savefig("test.svg")

Edit: I did not see using plt.show() that there is still the yaxis without ticks.

Stefan Bollmann
  • 640
  • 4
  • 12
  • 32

2 Answers2

8

To make the axes not visible, try something like

import matplotlib.pyplot as plt
ind = (1,2,3)
width = 0.8
fig,a = plt.subplots()
p1 = a.bar(ind,ind)
a.xaxis.set_visible(False)
a.yaxis.set_visible(False)
plt.show()

Is this what you meant?

gg349
  • 21,996
  • 5
  • 54
  • 64
  • Yes, this is, what I meant. (Ah, consulting matplotlib documentation, now I understand why I saw somewhere googling the subplot command. Somehow I thought "I do not need subplots..".) – Stefan Bollmann Sep 23 '13 at 21:00
  • dooh, my stomach knew it. The ticks of the yaxis are gone, but the frame is still there, when I save it as svg. I start a new question. That was, why I hesitated. – Stefan Bollmann Sep 23 '13 at 22:08
  • Ok, thx. You gave me the words to google for. At the end I used spines - I hope I will add my code tomorrow to help others. – Stefan Bollmann Sep 23 '13 at 22:53
  • http://stackoverflow.com/questions/14908576/how-to-remove-frame-from-matplotlib-pyplot-figure-vs-matplotlib-figure-frame <- `frameon` is also helpful here – tacaswell Sep 23 '13 at 22:53
  • @frameon Yeah, I saw that one, but that did not help at all. It only shows how to delete all axes if I am right. `http://matplotlib.org/examples/pylab_examples/spine_placement_demo.html` lead me to the outcome I want. – Stefan Bollmann Sep 23 '13 at 22:56
1

Here is the code I used at the end. It is not minimal anymore. Maybe it helps.

import matplotlib.pyplot as plt
import numpy as np

def adjust_spines(ax,spines):
    for loc, spine in ax.spines.items():
        if loc in spines:
            spine.set_smart_bounds(True)
        else:
            spine.set_color('none') # don't draw spine
    # turn off ticks where there is no spine
    if 'left' in spines:
        ax.yaxis.set_ticks_position('left')
    else:
        # no yaxis ticks
        ax.yaxis.set_ticks([])

def nbar(samples, data, err, bWidth=0.4, bSafe=True, svgName='out'):
    fig,a = plt.subplots(frameon=False)
    if len(data)!=len(samples):
        print("length(data) must be equal to length(samples)!")
        return
    ticks = np.arange(len(data))
    p1 = plt.bar(ticks, data, bWidth, yerr=err)
    plt.xticks(ticks+bWidth/2., samples )
    adjust_spines(a,['bottom'])
    a.xaxis.tick_bottom()
    if bSafe:
        fig.savefig(svgName+".svg")

samples = ('Sample1', 'Sample2','Sample3')
qyss = (91, 44, 59)
qysserr = (1,5,4)
nbar(samples,qyss,qysserr,svgName="test")

Thx to all contributors.

Stefan Bollmann
  • 640
  • 4
  • 12
  • 32