首页 > 解决方案 > 如何在绘图图中编辑轴刻度标签?

问题描述

举一个简单的例子,假设我们用非数字轴刻度绘制以下数据框:

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.cm import coolwarm

data = {'Col1':['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'],
        'Col2':['1000', '2000', '3000', '4000', '5000', '6000', '7000', '8000', '9000', '10000']}
df = pd.DataFrame(data, columns=['Col1', 'Col2'])

fig, ax = plt.subplots()

s = ax.scatter(df.Col1, df.Col2, c=df.Col2, cmap=coolwarm)
plt.colorbar(s)

这绘制了下图。可以看到非数字 x 和 y 刻度: 在此处输入图像描述

我想把它转换成一个情节图。

from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.tools as tls

plotly_fig = tls.mpl_to_plotly(fig)

init_notebook_mode(connected=True)
iplot(plotly_fig)

这绘制了以下交互式图形,我在下面显示其屏幕截图: 在此处输入图像描述

可以看出,x 和 y 刻度现在已替换为虚拟数值。(并且颜色条完全丢失,但这是另一天的问题)。

要在绘图图中添加正确的轴标签,我首先查看以下"data"部分plotly_fig

plotly_fig["data"][0]

Scatter({
    'marker': {'color': [rgba(58,76,192,1.0), rgba(92,123,229,1.0),
                         rgba(130,165,251,1.0), rgba(170,198,253,1.0),
                         rgba(205,217,236,1.0), rgba(234,211,199,1.0),
                         rgba(246,183,156,1.0), rgba(240,141,111,1.0),
                         rgba(217,88,71,1.0), rgba(179,3,38,1.0)],
               'line': {'color': [rgba(58,76,192,1.0), rgba(92,123,229,1.0),
                                  rgba(130,165,251,1.0), rgba(170,198,253,1.0),
                                  rgba(205,217,236,1.0), rgba(234,211,199,1.0),
                                  rgba(246,183,156,1.0), rgba(240,141,111,1.0),
                                  rgba(217,88,71,1.0), rgba(179,3,38,1.0)],
                        'width': 1.0},
               'size': 6.0,
               'symbol': 'circle'},
    'mode': 'markers',
    'uid': '2a0dfe88-4ae4-11e9-8fcc-8c164500f4c6',
    'x': [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0],
    'xaxis': 'x',
    'y': [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0],
    'yaxis': 'y'
})

我们可以看到plotly_fig["data"][0]["x"]并且plotly_fig["data"][0]["y"]都设置为[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]。所以我认为编辑这些就足够了:

plotly_fig['data'][0]['x'] = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
plotly_fig['data'][0]['y'] = ['1000', '2000', '3000', '4000', '5000', '6000', '7000', '8000', '9000', '10000']

但这并没有改变任何东西 - 它仍然使用虚拟轴标签进行绘图。

我如何解决它?


编辑1:现在看时plotly_fig["data"][0],我发现plotly_fig["data"][0]["x"]plotly_fig["data"][0]["y"]没有出现在其值周围的引号中:

Scatter({
    'marker': {'color': [rgba(58,76,192,1.0), rgba(92,123,229,1.0),
                         rgba(130,165,251,1.0), rgba(170,198,253,1.0),
                         rgba(205,217,236,1.0), rgba(234,211,199,1.0),
                         rgba(246,183,156,1.0), rgba(240,141,111,1.0),
                         rgba(217,88,71,1.0), rgba(179,3,38,1.0)],
               'line': {'color': [rgba(58,76,192,1.0), rgba(92,123,229,1.0),
                                  rgba(130,165,251,1.0), rgba(170,198,253,1.0),
                                  rgba(205,217,236,1.0), rgba(234,211,199,1.0),
                                  rgba(246,183,156,1.0), rgba(240,141,111,1.0),
                                  rgba(217,88,71,1.0), rgba(179,3,38,1.0)],
                        'width': 1.0},
               'size': 6.0,
               'symbol': 'circle'},
    'mode': 'markers',
    'uid': '2a0dfe88-4ae4-11e9-8fcc-8c164500f4c6',
    'x': [A, B, C, D, E, F, G, H, I, J],
    'xaxis': 'x',
    'y': [1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000],
    'yaxis': 'y'
})

在更深入的研究中,它们似乎是元组,而不是我输入它们的列表:

plotly_fig['data'][0]['x']
>>> ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J')

plotly_fig['data'][0]['y']
>>> ('1000',
 '2000',
 '3000',
 '4000',
 '5000',
 '6000',
 '7000',
 '8000',
 '9000',
 '10000')

这就是轴标签没有改变的原因吗?如果是,如何解决?


编辑2:现在,我发现的唯一解决方法是首先绘制一个虚拟散点图 -

  1. 一个类似的颜色图(例如,至少在我看来,相当于 matplotlib 的coolwarm似乎是 plotly 的RdBu),
  2. 将在 y 轴上的类似值范围(例如,1000 到 11000),以确保颜色条的限制正确显示,以及
  3. 轴标题,如果你想要的话。

像这样:

import numpy as np
import plotly.graph_objs as go

z=np.random.randint(1000, 11000, size=20)
trace=dict(type='scatter',
          x=3+np.random.rand(20),
          y=-2+3*np.random.rand(20),
          mode='markers',
          marker=dict(color= z, 
                      colorscale='RdBu', size=14, colorbar=dict(thickness=20)))

axis_style=dict(zeroline=False, showline=True, mirror=True)
layout=dict(width=600, height=450,
            xaxis=axis_style,
            yaxis=axis_style,
           hovermode='closest')
fig=go.FigureWidget(data=[trace], layout=layout)
fig

在此处输入图像描述

然后用真实数据更新图形轨迹,然后正确生成轴刻度和颜色条:

trace.update(dict(x=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'], 
                  y=['1000', '2000', '3000', '4000', '5000', '6000', '7000', '8000', '9000', '10000']
                 )
            )

fig=go.FigureWidget(data=[trace], layout=layout)
fig

在此处输入图像描述

但它不应该这么复杂。更不用说在实际情况下,我不知道实际的 x 和 y 值来更新虚拟图,这就是我在 matplotlib 中绘制它们的原因(因为生成这些图的库旨在绘制 matplotlib仅数字),然后将它们转换为 plotly。

标签: pythonmatplotlibplotlyplotly.js

解决方案


推荐阅读