0

I created a random number graph and I want to have a more gap between the x axis and the graph because it looks hard to read. Does anyone have a idea how to do that? Thank you so much. Here is my example code.

from bqplot import Axis, LinearScale, OrdinalScale, Scatter, Figure, Tooltip, DateScale, Lines, Bars
import bql
import pandas as pd

from numpy import nan, isnan
from numpy.random import randint
import numpy as np


x_sc_bar_fsa = OrdinalScale()
y_sc_bar_fsa = LinearScale()
tt_bar_fsa = Tooltip(fields=['x', 'y'], formats=['','.2f']) 
bar_fsa = Bars(stroke = 'white',scales={'x':  x_sc_bar_fsa, 'y': y_sc_bar_fsa},padding=0.5,\
               tooltip=tt_bar_fsa, unhovered_style={'opacity': 0.9}, type = 'grouped')

ax_x_bar_fsa = Axis(scale=x_sc_bar_fsa)
ax_y_bar_fsa = Axis(scale=y_sc_bar_fsa, orientation='vertical',tick_format='0.2f')

#Final Output
fig_bar_fsa = Figure(title='Random Number Graph for Testing',marks=[], axes=[ax_x_bar_fsa, ax_y_bar_fsa],padding_x=0) 


def plot_bars_fsa(x_data, y_data, label_data,colors=['darkblue','royalblue','darkgreen','darkred','red']):
    if (len(x_data) == 0) | (len(y_data) == 0) | (len(label_data) == 0):
        fig_bar_fsa.marks = []  #Creates empty list if x-data, y-data and label data is missing i.e. =0
    else:    #otherwise display
        bar_fsa.x = x_data 
        bar_fsa.y = y_data
        y_sc_bar_fsa.min = 0
        y_sc_bar_fsa.max = np.nanmax([np.nanmax(x) for x in y_data]) * 1.1
        bar_fsa.labels = label_data
        bar_fsa.colors = colors # Defined above
        ax_x_bar_fsa.tick_rotate = -90 # This is the angle of the letters on the graph
        ax_x_bar_fsa.tick_style = {'text-anchor': 'end'}
        fig_bar_fsa.marks = [bar_fsa] #links marks to bar chart
        
        
example_df = pd.DataFrame(data=randint(0, 100, size=(10, 5)),
                          columns=['column_' + str(i) for i in range(5)])
example_df.index = ['namenumber1253','namenumber26675','namenumber339', 'namenumber2556','namenumber3109', 'namenumber5894','namenumber1355','namenumber685890', 'namenumber397', 'namenumber85']



plot_bars_fsa(example_df.index.values,\
                    [example_df[x].values for x in ['column_0','column_1','column_2','column_3','column_4']],\
                      ['column_0','column_1','column_2','column_3','column_4'])

fig_bar_fsa

enter image description here

jaewon lee
  • 11
  • 2

0 Answers0