Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Plotly中的条件格式_Python_Plotly_Plotly.js - Fatal编程技术网

Python Plotly中的条件格式

Python Plotly中的条件格式,python,plotly,plotly.js,Python,Plotly,Plotly.js,这个问题是关于如何在Plotly中进行条件格式设置 可能需要这样做的实例: 点需要着色(即彩虹)的散点图,作为2个变量的函数 颜色取决于参数值的交互式图表 直方图,其中的部分需要不同的颜色 在这里,我将特别询问直方图 以以下数据为例: data=np.random.normal(大小=1000) 我想有一个直方图,其中高于0的值被放在不同的颜色下 一个简单的解决办法是 hist1 = go.Histogram(x=data[data<0], o

这个问题是关于如何在Plotly中进行条件格式设置

可能需要这样做的实例:

  • 点需要着色(即彩虹)的散点图,作为2个变量的函数
  • 颜色取决于参数值的交互式图表
  • 直方图,其中的部分需要不同的颜色
在这里,我将特别询问直方图

以以下数据为例:

data=np.random.normal(大小=1000)

我想有一个直方图,其中高于0的值被放在不同的颜色下

一个简单的解决办法是

hist1 = go.Histogram(x=data[data<0], 
                    opacity=0.75, 
                    histnorm='density',
                    showlegend=False,
                    )
hist2 = go.Histogram(x=data[data>=0], 
                    opacity=0.75, 
                    histnorm='density',
                    showlegend=False,
                    )
layout = go.Layout(barmode='overlay')
fig = go.Figure(data=[hist1, hist2], layout=layout)
iplot(fig, show_link=False)

但是,我如何解决第二个问题呢

如果我想让histnorm=‘概率密度’得到结果图 “规范化”每个单独的直方图,以便它们看起来 不成比例的

。。。在将整个样本分割为两个不同的直方图之前,您似乎必须对其进行规范化。这意味着你应该做的是做出一个决定。但不幸的是,建议的解决方案似乎是为两条带有

df_pos = df.where(df < 0, 0)
df_neg = df.where(df > 0, 0)

我的建议对你效果如何?
df_pos = df.where(df < 0, 0)
df_neg = df.where(df > 0, 0)
# imports
import plotly.graph_objects as go
from plotly.offline import iplot
import pandas as pd
import numpy as np

# theme
import plotly.io as pio
#pio.templates
#pio.templates.default = "plotly_white"
pio.templates.default = "none"

# Some sample data
np.random.seed(123)
x = np.random.normal(0, 1, 1000)

# numpy binning
binned = np.histogram(x, bins=30, density=True)

# retain some info abou the binning
yvals=binned[0]
x_last = binned[1][-1]
xvals=binned[1][:-1]

# organize binned data in a pandas dataframe
df_bin=pd.DataFrame(dict(x=xvals, y=yvals))
df_bin_neg = df.where(df['x'] < 0)
df_bin_pos = df.where(df['x'] > 0)

# set up plotly figure
fig=go.Figure()

# neagtive x
fig.add_trace(go.Scatter(
    x=df_bin_neg['x'],
    y=df_bin_neg['y'],
    name="negative X",
    hoverinfo='all',
    fill='tozerox',
    #fillcolor='#ff7f0e',
    fillcolor='rgba(255, 103, 0, 0.7)',

    line=dict(color = 'rgba(0, 0, 0, 0)', shape='hvh')
))

# positive x
fig.add_trace(go.Scatter(
    x=df_bin_pos['x'],
    y=df_bin_pos['y'],
    name="positive X",
    hoverinfo='all',
    fill='tozerox',
    #opacity=0.2,
    #fillcolor='#ff7f0e',
    #fillcolor='#1f77b4',
    fillcolor='rgba(131, 149, 193, 0.9)',
    line=dict(color = 'rgba(0, 0, 0, 0)', shape='hvh')
))

# adjust layout to insure max values are included
ymax = np.max([df_bin_neg['y'].max(), df_bin_neg['y'].max()])
fig.update_layout(yaxis=dict(range=[0,ymax+0.1]))

# adjust layout to match OPs original
fig.update_xaxes(showline=True, linewidth=1, linecolor='black', mirror=False, zeroline=False, showgrid=False)
fig.update_yaxes(showline=False)#, linewidth=2, linecolor='black', mirror=True)

fig.show()