x值重叠的Bokeh图

2024-09-29 22:00:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我用bokeh库做了一个绘图,除了x个分类变量的重叠外,输出似乎还可以

有一个简单的方法来分开每个月,这样彼此之间就没有重叠了

我尝试使用dodge(),但无法在函数中定义参数

cumple.txt file:
{"people": 
[{"name": "daniela", "birthday": "8/05/1990"}, 
{"name": "mariano", "birthday": "5/08/1990"}, 
{"name": "agustin", "birthday": "27/11/1993"}, 
{"name": "laura", "birthday": "20/10/2000"}]}

以下是我的代码和输出:

from collections import Counter
import json
from bokeh.plotting import figure, show, output_file

with open("cumple.txt", "r") as f:
    info = json.load(f)#carga todo el dictionario en info

s=[]
for i in info['people']:
    s.append(i['birthday'])


months=[]

num_to_string = {
    1: "January",
    2: "February",
    3: "March", 
    4: "April",
    5: "May",
    6: "June",
    7: "July",
    8: "August",
    9: "September",
    10: "October",
    11: "November",
    12: "December"
}

month=0
for x in s:
    month=int(x.split('/')[1])[![enter image description here][1]][1]
    months.append(num_to_string[month])


output_file("figura.html")

x_categories = []

for u in num_to_string.values():
    x_categories.append(u)
print(x_categories)

x=[]
for j in months:
    x.append(j)
print (x)


y=[]

for r in Counter(months).values():
    y.append(r)
print(y)
p = figure(title='cumple de amigos',x_range=x_categories, x_axis_label='months', y_axis_label='amount of birthdays')
p.title.align = 'center'
p.vbar(x=x, top=y, width=0.5)

show(p)

print(Counter(months))

Tags: tonameinimportinfoforcounternum
1条回答
网友
1楼 · 发布于 2024-09-29 22:00:34

避免标签重叠的两种明显方法:

  1. 通过设置plot_width增加图形的宽度:

    p = figure(title='cumple de amigos',x_range=x_categories, x_axis_label='months', y_axis_label='amount of birthdays', plot_width=1000)
    
  2. 旋转X轴标签(不要忘记导入pi):

    p.xaxis.major_label_orientation = pi/4
    

相关问题 更多 >

    热门问题