Ploty在Ploty图表中显示随机条形图和每次错误的数据

时间:2018-10-12 10:56:35

标签: python graph bar-chart plotly

Python 3.5 离线进行

以下代码成功生成了输出,但是输出是随机的,并且没有在条形图中显示所有X轴数据。

.tab文件的输入(如下表所示) 输出:将下表绘制为BAR图

[Function group][count]


ke1           101
IO1            70
Andr1234       22
VN1            22
NAV1           20
SW1            17
DC123          16
OnOff          15
Linux          12
AD1            12
Brow1          10
QNXOS           9
OSSDK           9
DM1             7
Frm1            6
SYSINT          5
TABA1           4
Security        3
A1              3
DCM             3
Per1            2
Trace           2
Audio           1
Buildtools      1
ENV             1
Pathology       1

当我使用plotly进行绘制时,它会显示随机条形,并且并非所有标题都显示在图形的column1中。

获取所有X轴名称及其对应的Y轴需要进行哪些代码更改?

  1. 每次运行此代码时,它都会生成一个随机值图和X轴随机文本.X轴中未显示表格中所有文本标签,仅显示了10-12个标签。如何克服此问题为每个表格标签发布并绘制具有正确值的正确BAR图。

  2. 需要添加哪些内容才能在不悬停鼠标的情况下显示BAR上的计数。


import pandas as pd
from io import StringIO
import requests
import pandas as pd
import numpy as np
from bs4 import BeautifulSoup
from nsepy import get_history

import plotly
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.graph_objs as go
init_notebook_mode(connected=True)

from datetime import date
from datetime import datetime

datalist = pd.read_table(r'D:\python\ython\xxx.tab',sep='\t')   #sep=\s+|\t+
datalistFg=pd.value_counts(datalist['Function group'].values, sort=True)
datalistRp=pd.value_counts(datalist['Responsible person'].values, sort=True)


print(datalistFg)

#graphing place
otrace1 =go.Bar(
    #x=stock_opt_pe.index
    x=datalist['Function group'],
    y=datalistFg,
    text=datalistFg,
    #xaxis-type (enumerated: “-” | “linear” | “log” | “date” | “category” )
    #xaxis-type (enumerated: “-” | “linear” | “log” | “date” | “category” )
    #name='Function Group Vx RespPerson',
    #orientation = 'v',
    #marker = dict(
        #color = 'rgba(224, 224, 224, 0.6)',
        #line = dict(
            #color = 'rgba(246, 250, 206, 1.0)',
            #color = 'rgb(60, 60, 60)',
            #width = 0)
    #)
)

otrace2 = go.Bar(
    #x=stock_opt_pe.index
    x=datalist['Responsible person'],
    y=datalistRp,
    name='RP',
    orientation = 'v',
    marker = dict(
        color = 'rgba(128, 255, 0, 0.6)',
        line = dict(
            #color = 'rgba(240, 240, 240, 1.0)',
            #color = 'rgb(204, 255, 204)',
            width = 1.5)
    )
)

odata = [otrace1]
olayout = go.Layout(
    title = "TESTING",
    xaxis=dict(
    tickangle=35,
    showticklabels=True,
    type='category',
    title='Function group',
    tickmode='linear'),
#    barmode="linear"
)
fig = dict(data=odata, layout=olayout)
#fig = dict(data=odata)
iplot(fig, filename = 'D:/python/ython/Test-{}.html'.format("Testing"))
plot(fig,show_link = True, filename = 'D:/python/ython/test_{}.html'.format("Testing"))

1 个答案:

答案 0 :(得分:0)

下面的更新解决了该问题。唯一的问题是X轴显示1 2,3 .... 25。如果更正此错误以显示每个条的X轴标签为Ke1,IO1,VN1。 ..此解决方案将是完整的。

现在,除了X轴标签外,绘图工作正常。

import matplotlib.pyplot as plt
import pandas as pd
from io import StringIO
import requests
import pandas as pd
import plotly
plotly.__version__
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.graph_objs as go
init_notebook_mode(connected=True)

datalist = pd.read_table(r'D:\pon\oon\etml\106111025170_20180808062710.tab',sep='\t')   #sep=\s+|\t+

#graph plotly
otrace1 =go.Bar(
    x=datalist['Function group'].nunique(),
    y=pd.value_counts(datalist['Function group'].values,sort=True),
    text=pd.value_counts(datalist['Function group'].values,sort=True),
    textposition = 'auto',
    orientation = 'v',
    marker = dict(
        color = 'rgba(127,87,225,1.00)',
        line = dict(
            color = 'rgba(127,45,64,1.00)',
            width = 0),
    )
)

odata = [otrace1]
olayout = go.Layout(
    title = "TESTING",
    xaxis=dict(
    #tickangle=35,
    #showticklabels=True,
    #type='category',
    title='GroupName',
    titlefont=dict(
            family='Courier New, monospace',
            size=18,
            color='#ffff7f'
        ),
    tickmode='linear'),
    barmode="bar"
)
fig = dict(data=odata, layout=olayout)
#fig = dict(data=odata)
iplot(fig, filename = 'D:/pyon/ofhon/elml/Test-{}.html'.format("Testing"))
plot(fig,show_link = True, filename = 'D:/pyon/ofhon/elml/test_{}.html'.format("Testing"))