我有如下所示的pandas数据框。目标是在一个画布上生成多个(在本例中为12个)x-y散点图,以使代码对于任何数量的“ y”列均通用,但始终具有相同的“ x_values”列(以下数据框的最右列)。
y1 y2 y3 y4 y5 y6 y7 y8 y9 y10 y11 y12 x_values
0 1.975284 1.975200 1.975200 1.974999 1.975202 1.975202 1.975202 1.975202 1.975202 1.975202 1.975202 1.975202 0
1 1.975202 1.975202 1.975202 1.975202 1.975299 1.975247 1.975156 1.975156 1.975319 1.975276 1.975277 1.964492 0.05
2 1.975447 1.975187 1.975187 1.974587 1.975394 1.975301 1.975020 1.975020 1.975455 1.975448 1.975448 1.951889 0.10
3 1.975361 1.975197 1.975197 1.974781 1.975487 1.975342 1.974792 1.974792 1.975544 1.975608 1.975608 1.957457 0.15
4 1.975284 1.975200 1.975200 1.975413 1.975299 1.975247 1.975156 1.975156 1.975042 1.975277 1.975277 1.981666 -0.05
5 1.975447 1.975187 1.975187 1.975827 1.975394 1.975301 1.975020 1.975020 1.974911 1.975448 1.975448 1.986136 -0.10
6 1.975361 1.975197 1.975197 1.975636 1.975488 1.975342 1.974792 1.974793 1.974630 1.975609 1.975608 1.989482 -0.15
我使用的代码如下所示:
import pandas as pd
import numpy as np
import bokeh.io
import bokeh.models
import bokeh.plotting
bokeh.io.output_notebook()
df = pd.read_csv('/Users/rubenm/Desktop/VOnewset/27042019_freqs/plotter_script/ofile.txt', sep = ',', header = None, skiprows = 3)
cols_int_list=list(range(1,len(df.columns) + 1))
newcols = ['y' + str(k) for k in cols_int_list]
df.columns = newcols
x_vals = ['0','0.05','0.10','0.15','-0.05','-0.10','-0.15']
df['x_values']= x_vals
colors = ['deepskyblue','dodgerblue','blue','navy','limegreen','green','darkgreen','tomato','orange','deeppink','magenta','purple','peru','saddlebrown','plum']
source = ColumnDataSource(df)
p = figure(plot_height=500)
i = 0
for name in df.columns:
if not name.startswith('y'): continue
print(name)
p.scatter('x_values', name, source=source, fill_color=colors[i], legend=name)
i += 1
p.legend.location = 'top_left'
p.legend.click_policy = 'hide'
show(p)
答案 0 :(得分:0)
我看到的问题是您正在将每个值绘制为y值,而不是数据中的任何x值。我会尝试将x值显式地称为x_vals或df [x_values],但您甚至不需要将其分配给df
data= {'x': 'x_vals', 'y': df }
source = ColumnDataSource(data)