I want to illustrate my spendings on a plot, but the tuning the plot properties is trickier than I expected.
The data for the plot is fetch from a SQL server with the pandas method. Basically it is summing my spendings monthly.
MonthlyFoodSpendingsResult = pd.read_sql_query(query, con=connection)
Below is the dataframe format. Note that the date entries might not start from the begining of the month. I don't know if that could be the cause for the trouble.
CATEGORY DATE MONTH SUM
0 Food 2016-01-03 1 478.04
1 Food 2016-02-01 2 488.24
2 Food 2016-03-01 3 498.24
...
Below is the code for the plot.
# Plot settings
p1 = figure(
title = 'Plot 1',
x_axis_label='Month',
plot_height=200,
plot_width=400,
x_axis_type='datetime'
)
p1.xaxis.ticker = MonthsTicker(months=[1,2,3,4,5,6,7,8,9,10,11,12])
p1.xaxis.major_label_orientation = "vertical"
# Render glyphs
p1.vbar(
x=MonthlyFoodSpendingsResult['DATE'],
top=MonthlyFoodSpendingsResult['SUM'],
width=0.5 #Does not have affect
)
For the plot 1 I would like to increase the column width but the parameter does not seem to have an affect for the column size. Even if its 100, the bar is the same.
For the plot 2 I have changed the x=MonthlyFoodSpendingsResult['DATE']
to MONTH and then the width is affected, but the month labels are gone.
The plot 3 is the same as 2 but I removed the p1.xaxis.ticker
and the now the labels are ms.
So I would like to have something like plot 3 but with all months as the labels.
To get control over widths and have the month names on the axes, consider using a categorial axis instead of using a datetime
axis.
An example:
month_names = {1:"Jan", 2:"Feb", 3:"Mar", 4:"Apr", 5:"May", 6:"Jun",
7:"Jul", 8:"Aug", 9:"Sep", 10:"Oct", 11:"Nov", 12:"Dec"}
#Create a column with month names instead of numbers
data = pd.DataFrame(data = {"MONTH": list(range(1,13)),
"SUM": [randint(300, 600) for _ in range(12)]})
data = data.assign(MONTH_NAME=data.MONTH.apply(month_names.get))
p1 = figure(
plot_height=200,
x_range=list(month_names.values()) # Creates a categorical axis
)
p1.vbar(x=data["MONTH_NAME"], top=data["SUM"], width=.9)