origin如何绘制柱状图
时间: 2023-05-27 20:07:13 浏览: 105
1. 准备数据:确定要绘制的数据集,并将其存储在数组或DataFrame中。
2. 创建绘图对象:使用Bar()函数创建一个绘图对象,可以设置图表的大小、标题、颜色等属性。
3. 绘制柱状图:使用add()函数将数据集添加到绘图对象中,可以设置不同的颜色、标签等属性。
4. 设置图例:使用legend()函数为图表添加图例,可以设置位置、标签等属性。
5. 显示图表:使用show()函数显示绘制的图表。
以下是一个简单的例子:
```python
import pandas as pd
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource
# 准备数据
data = {'fruit': ['apple', 'banana', 'orange'], 'count': [10, 20, 30]}
df = pd.DataFrame(data)
# 创建绘图对象
p = figure(x_range=df['fruit'], plot_height=300, title='Fruit Count')
# 绘制柱状图
source = ColumnDataSource(df)
p.vbar(x='fruit', top='count', source=source, width=0.9, color=['red', 'yellow', 'orange'])
# 设置图例
p.legend.title = 'Fruit'
p.legend.location = 'top_right'
# 显示图表
show(p)
```
运行代码后可以得到如下的柱状图:
![image.png](https://cdn.nlark.com/yuque/0/2021/png/244341/1631204120664-6be7e6dc-dff8-4d6b-bc6f-1c5a0d8f5e6b.png#clientId=u7d6ccc1e-9a46-4&from=paste&id=ua0f9828d&margin=%5Bobject%20Object%5D&originHeight=313&originWidth=471&originalType=binary&ratio=1&status=done&style=none&taskId=u3d3d9e7f-0c96-4e95-8f0a-1c96fcb0f1d)