使用列名作为散景中的 x 轴绘制行中的数据
Plot data from a row using column name as x axis in bokeh
我正在开始一个项目,我想从这个数据集创建一个交互式图表:
现在我只是想绘制从 2000 到 2012 列的第一行,为此我使用这个:
import pandas as pd
from bokeh.io import output_file
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.plotting import show
output_file('test.html')
df = pd.read_csv('Swedish_Population_Statistics.csv', encoding="ISO-8859-1")
df.dropna(inplace=True) # Drop rows with missing attributes
df.drop_duplicates(inplace=True) # Remove duplicates
# Drop all the column I don't use for now
df.drop(['region', 'marital_status', 'sex'], inplace=True, axis=1)
x = df.loc[[0]]
print(x)
这给了我这个数据框
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
0
10406
10362
10322
10288
10336
10336
10429
10585
10608
10718
10860
11121
11288
现在我想将列名作为 x 轴,将行值作为 y 轴。
这就是我卡住的地方。
我想代码应该是这样的,但不知道在 x 和 y 中放什么
x = df.columns.tolist() #Take columns names into a list
y = df.loc[[0]].values.tolist() # Take the first row
source = ColumnDataSource(x, y)
p = figure(title="Test")
p.line(x='x', y='y', source=source, line_color="blue", line_width=2)
我收到这个错误:
BokehUserWarning: ColumnDataSource's columns must be of the same
length. Current lengths: ('x', 13), ('y', 1)
我不明白为什么长度与我在两者上使用的 tolist()
不同。
非常感谢任何帮助,过去 3 小时我一直在努力寻找解决方案,但没有成功。
好吧,我发现了我的问题,主要是 y
是一个二维列表,但我需要一个一维列表。
这让我想到了这个工作代码:
output_file('test.html')
df = pd.read_csv('Swedish_Population_Statistics.csv', encoding="ISO-8859-1")
df.dropna(inplace=True) # Drop rows with missing attributes
df.drop_duplicates(inplace=True) # Remove duplicates
# Drop all the column I don't use for now
df.drop(['region', 'marital_status', 'sex'], inplace=True, axis=1)
x = df.columns.tolist()
y = df.loc[[0]]
temp = []
temp2 = []
# Append each value of the dataframe row in a 1-dimension list one by one
for i in range(13):
temp.append(y[str(2000+i)].tolist())
temp2.append(temp[i][0])
p = figure(title="Test", sizing_mode="scale_both")
p.line(x, temp2, line_color="blue", line_width=2)
p.circle(x, temp2, fill_color="white", size=8)
show(p)
结果如下:
Plot
无需创建循环。你走在正确的轨道上,但你不应该使用双括号
>>> df.loc[0].values.tolist()
[111, 222, 333]
那么x
和y
的维度是一样的
我正在开始一个项目,我想从这个数据集创建一个交互式图表:
现在我只是想绘制从 2000 到 2012 列的第一行,为此我使用这个:
import pandas as pd
from bokeh.io import output_file
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.plotting import show
output_file('test.html')
df = pd.read_csv('Swedish_Population_Statistics.csv', encoding="ISO-8859-1")
df.dropna(inplace=True) # Drop rows with missing attributes
df.drop_duplicates(inplace=True) # Remove duplicates
# Drop all the column I don't use for now
df.drop(['region', 'marital_status', 'sex'], inplace=True, axis=1)
x = df.loc[[0]]
print(x)
这给了我这个数据框
2000 | 2001 | 2002 | 2003 | 2004 | 2005 | 2006 | 2007 | 2008 | 2009 | 2010 | 2011 | 2012 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 10406 | 10362 | 10322 | 10288 | 10336 | 10336 | 10429 | 10585 | 10608 | 10718 | 10860 | 11121 | 11288 |
现在我想将列名作为 x 轴,将行值作为 y 轴。
这就是我卡住的地方。
我想代码应该是这样的,但不知道在 x 和 y 中放什么
x = df.columns.tolist() #Take columns names into a list
y = df.loc[[0]].values.tolist() # Take the first row
source = ColumnDataSource(x, y)
p = figure(title="Test")
p.line(x='x', y='y', source=source, line_color="blue", line_width=2)
我收到这个错误:
BokehUserWarning: ColumnDataSource's columns must be of the same length. Current lengths: ('x', 13), ('y', 1)
我不明白为什么长度与我在两者上使用的 tolist()
不同。
非常感谢任何帮助,过去 3 小时我一直在努力寻找解决方案,但没有成功。
好吧,我发现了我的问题,主要是 y
是一个二维列表,但我需要一个一维列表。
这让我想到了这个工作代码:
output_file('test.html')
df = pd.read_csv('Swedish_Population_Statistics.csv', encoding="ISO-8859-1")
df.dropna(inplace=True) # Drop rows with missing attributes
df.drop_duplicates(inplace=True) # Remove duplicates
# Drop all the column I don't use for now
df.drop(['region', 'marital_status', 'sex'], inplace=True, axis=1)
x = df.columns.tolist()
y = df.loc[[0]]
temp = []
temp2 = []
# Append each value of the dataframe row in a 1-dimension list one by one
for i in range(13):
temp.append(y[str(2000+i)].tolist())
temp2.append(temp[i][0])
p = figure(title="Test", sizing_mode="scale_both")
p.line(x, temp2, line_color="blue", line_width=2)
p.circle(x, temp2, fill_color="white", size=8)
show(p)
结果如下:
Plot
无需创建循环。你走在正确的轨道上,但你不应该使用双括号
>>> df.loc[0].values.tolist()
[111, 222, 333]
那么x
和y
的维度是一样的