1. <legend id='KaweR'><style id='KaweR'><dir id='KaweR'><q id='KaweR'></q></dir></style></legend>

      <i id='KaweR'><tr id='KaweR'><dt id='KaweR'><q id='KaweR'><span id='KaweR'><b id='KaweR'><form id='KaweR'><ins id='KaweR'></ins><ul id='KaweR'></ul><sub id='KaweR'></sub></form><legend id='KaweR'></legend><bdo id='KaweR'><pre id='KaweR'><center id='KaweR'></center></pre></bdo></b><th id='KaweR'></th></span></q></dt></tr></i><div id='KaweR'><tfoot id='KaweR'></tfoot><dl id='KaweR'><fieldset id='KaweR'></fieldset></dl></div>
    2. <tfoot id='KaweR'></tfoot>

        <bdo id='KaweR'></bdo><ul id='KaweR'></ul>

        <small id='KaweR'></small><noframes id='KaweR'>

      1. 将线段添加到绘图图的简洁方法(使用 python/jupyter 笔记本)?

        时间:2023-09-28

              <tfoot id='i1XQ1'></tfoot>

            1. <small id='i1XQ1'></small><noframes id='i1XQ1'>

                <tbody id='i1XQ1'></tbody>
            2. <legend id='i1XQ1'><style id='i1XQ1'><dir id='i1XQ1'><q id='i1XQ1'></q></dir></style></legend>

                <i id='i1XQ1'><tr id='i1XQ1'><dt id='i1XQ1'><q id='i1XQ1'><span id='i1XQ1'><b id='i1XQ1'><form id='i1XQ1'><ins id='i1XQ1'></ins><ul id='i1XQ1'></ul><sub id='i1XQ1'></sub></form><legend id='i1XQ1'></legend><bdo id='i1XQ1'><pre id='i1XQ1'><center id='i1XQ1'></center></pre></bdo></b><th id='i1XQ1'></th></span></q></dt></tr></i><div id='i1XQ1'><tfoot id='i1XQ1'></tfoot><dl id='i1XQ1'><fieldset id='i1XQ1'></fieldset></dl></div>
                  <bdo id='i1XQ1'></bdo><ul id='i1XQ1'></ul>

                • 本文介绍了将线段添加到绘图图的简洁方法(使用 python/jupyter 笔记本)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想用这样的几个水平线段创建一个棒棒糖图 -

                  I want to create a lollipop plot with several horizontal line segments like this - https://python-graph-gallery.com/184-lollipop-plot-with-2-group. I'd like to use plotly since I prefer the graphics (and easy interactivity) but can't find a succint way.

                  There's both line graphs (https://plot.ly/python/line-charts/) and you can add lines in the layout (https://plot.ly/python/shapes/#vertical-and-horizontal-lines-positioned-relative-to-the-axes), but both of these solutions require each line segment to be added separately, with about 4-8 lines of code each. While I could just for-loop this, would appreciate if anyone can point me to anything with inbuilt vectorization, like the matplotlib solution (first link)!

                  Edit: Also tried the following code, to first make the plot ala matplotlib, then convert to plotly. The line segments disappear in the process. Starting to think it's just impossible.

                  mpl_fig = plt.figure()
                  
                  # make matplotlib plot - WITH HLINES
                  plt.rcParams['figure.figsize'] = [5,5]
                  ax = mpl_fig.add_subplot(111)
                  ax.hlines(y=my_range, xmin=ordered_df['value1'], xmax=ordered_df['value2'], 
                  color='grey', alpha=0.4)
                  ax.scatter(ordered_df['value1'], my_range, color='skyblue', alpha=1, 
                  label='value1')
                  ax.scatter(ordered_df['value2'], my_range, color='green', alpha=0.4 , 
                  label='value2')
                  ax.legend()
                  
                  # convert to plotly
                  plotly_fig = tls.mpl_to_plotly(mpl_fig)
                  plotly_fig['layout']['xaxis1']['showgrid'] = True
                  plotly_fig['layout']['xaxis1']['autorange'] = True
                  plotly_fig['layout']['yaxis1']['showgrid'] = True
                  plotly_fig['layout']['yaxis1']['autorange'] = True
                  
                  # plot: hlines disappear :/
                  iplot(plotly_fig)
                  

                  解决方案

                  Plotly doesn't provide a built in vectorization for such chart, because it can be done easily by yourself, see my example based on your provided links:

                  import pandas as pd
                  import numpy as np
                  import plotly.offline as pyo
                  import plotly.graph_objs as go
                  
                  # Create a dataframe
                  value1 = np.random.uniform(size = 20)
                  value2 = value1 + np.random.uniform(size = 20) / 4
                  df = pd.DataFrame({'group':list(map(chr, range(65, 85))), 'value1':value1 , 'value2':value2 })
                  
                  my_range=range(1,len(df.index)+1)
                  
                  # Add title and axis names
                  data1 = go.Scatter(
                          x=df['value1'],
                          y=np.array(my_range),
                          mode='markers',
                          marker=dict(color='blue')
                      )
                  
                  
                  data2 = go.Scatter(
                          x=df['value2'],
                          y=np.array(my_range),
                          mode='markers',
                          marker=dict(color='green')
                      )
                  
                  # Horizontal line shape
                  shapes=[dict(
                          type='line',
                          x0 = df['value1'].loc[i],
                          y0 = i + 1,
                          x1 = df['value2'].loc[i],
                          y1 = i + 1,
                          line = dict(
                              color = 'grey',
                              width = 2
                          )
                      ) for i in range(len(df['value1']))]
                  
                  
                  layout = go.Layout(
                      shapes = shapes,
                      title='Lollipop Chart'
                  )
                  
                  # Plot the chart
                  fig = go.Figure([data1, data2], layout)
                  
                  pyo.plot(fig)
                  

                  With the result I got:

                  这篇关于将线段添加到绘图图的简洁方法(使用 python/jupyter 笔记本)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 Plotly 中启用和禁用对数刻度作为查看器? 下一篇:Plotly:如何为 x 轴上的时间序列设置主要刻度线/网格线的值?

                  相关文章

                    • <bdo id='hYoUO'></bdo><ul id='hYoUO'></ul>
                  1. <tfoot id='hYoUO'></tfoot>
                  2. <small id='hYoUO'></small><noframes id='hYoUO'>

                      <legend id='hYoUO'><style id='hYoUO'><dir id='hYoUO'><q id='hYoUO'></q></dir></style></legend>

                      <i id='hYoUO'><tr id='hYoUO'><dt id='hYoUO'><q id='hYoUO'><span id='hYoUO'><b id='hYoUO'><form id='hYoUO'><ins id='hYoUO'></ins><ul id='hYoUO'></ul><sub id='hYoUO'></sub></form><legend id='hYoUO'></legend><bdo id='hYoUO'><pre id='hYoUO'><center id='hYoUO'></center></pre></bdo></b><th id='hYoUO'></th></span></q></dt></tr></i><div id='hYoUO'><tfoot id='hYoUO'></tfoot><dl id='hYoUO'><fieldset id='hYoUO'></fieldset></dl></div>