Mainland
Mainland

Reputation: 4554

python dataframe 3d bar plot overlapping issue of axis label and ticks

I successfully drawned a 3d bar plot. The problem is overlapping of axis ticks with label. How do I avoid it? Also, how do I change the figure size?

My code:

df = 
               raw          Stage1    Stage2      Stage3       Stage4
asdfghdfs   1249.0          661.0     661.0       654.0        647.0
bgdfghdfs   1237.0          654.0     654.0       648.0        642.0
dfdfghdfs   1236.0          653.0     653.0       647.0        641.0
qwdfghdfs   1240.0          652.0     652.0       647.0        641.0
hjdfghdfs   1236.0          652.0     652.0       647.0        641.0
kldfghdfs   1234.0          651.0     651.0       645.0        640.0
sddfghdfs   1236.0          648.0     648.0       643.0        637.0
rfdfghdfs   1228.0          646.0     646.0       640.0        633.0


X = np.arange(len(df))
color_list  = ['#acc2d9', '#a8ff04', '#70b23f', '#952e8f', '#388004', '#efb435',
   '#0c06f7', '#2242c7', '#05ffa6', '#0cb577']
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
ly,lx = len(X),len(df.columns)
xpos = np.arange(0,lx,1)    # Set up a mesh of positions
ypos = np.arange(0,ly,1)
xpos, ypos = np.meshgrid(xpos, ypos)
xpos = xpos.flatten()   # Convert positions to 1D array
ypos = ypos.flatten()
zpos = np.zeros(lx*ly)
dx = 0.5 * np.ones_like(zpos)
dy = dx.copy()
dz = np.array(df).flatten()
cs = color_list[:lx] * ly
ax.bar3d(xpos,ypos,zpos, dx, dy, dz,alpha=0.8, color=cs)
ax.set_yticks(np.arange(len(df.index))+1.5)
ax.set_yticklabels(df.index)
ax.set_xticks(np.arange(len(df.columns)))
ax.set_xticklabels(df.columns)
ax.tick_params(axis='both', which='major', labelsize=10)
plt.gcf().autofmt_xdate()
ax.set_xlabel('Test case')
ax.set_ylabel('Sample category')
ax.set_zlabel("Number of Samples")
plt.subplots_adjust(left=0.18, right=0.79, top=0.99, bottom=0.08)        
plt.show()

Present output:

enter image description here

Upvotes: 0

Views: 446

Answers (1)

r-beginners
r-beginners

Reputation: 35115

You can adjust the spacing with the 'labelpad', so you can adjust the value to your liking.

import pandas as pd
import numpy as np
import io

data = '''
              raw          Stage1    Stage2      Stage3       Stage4
asdfghdfs   1249.0          661.0     661.0       654.0        647.0
bgdfghdfs   1237.0          654.0     654.0       648.0        642.0
dfdfghdfs   1236.0          653.0     653.0       647.0        641.0
qwdfghdfs   1240.0          652.0     652.0       647.0        641.0
hjdfghdfs   1236.0          652.0     652.0       647.0        641.0
kldfghdfs   1234.0          651.0     651.0       645.0        640.0
sddfghdfs   1236.0          648.0     648.0       643.0        637.0
rfdfghdfs   1228.0          646.0     646.0       640.0        633.0
'''

df = pd.read_csv(io.StringIO(data), delim_whitespace=True)

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

X = np.arange(len(df))
color_list  = ['#acc2d9', '#a8ff04', '#70b23f', '#952e8f', '#388004', '#efb435',
   '#0c06f7', '#2242c7', '#05ffa6', '#0cb577']

fig = plt.figure()

ax = Axes3D(fig)
ly,lx = len(X),len(df.columns)
xpos = np.arange(0,lx,1)    # Set up a mesh of positions
ypos = np.arange(0,ly,1)
xpos, ypos = np.meshgrid(xpos, ypos)
xpos = xpos.flatten()   # Convert positions to 1D array
ypos = ypos.flatten()
zpos = np.zeros(lx*ly)
dx = 0.5 * np.ones_like(zpos)
dy = dx.copy()
dz = np.array(df).flatten()
cs = color_list[:lx] * ly
ax.bar3d(xpos,ypos,zpos, dx, dy, dz,alpha=0.8, color=cs)
ax.set_yticks(np.arange(len(df.index))+1.5)
ax.set_yticklabels(df.index)
ax.set_xticks(np.arange(len(df.columns)))
ax.set_xticklabels(df.columns)
ax.tick_params(axis='both', which='major', labelsize=10)
plt.gcf().autofmt_xdate()
ax.set_xlabel('Test case', labelpad=20)
ax.set_ylabel('Sample category', labelpad=15)
ax.set_zlabel("Number of Samples", labelpad=10)
plt.subplots_adjust(left=0.18, right=0.79, top=0.99, bottom=0.08)
for tick in ax.xaxis.get_major_ticks():
        tick.label1.set_horizontalalignment('left')  

 
plt.show()

enter image description here

Upvotes: 1

Related Questions