python散点图y轴未排序

时间:2018-08-02 19:38:03

标签: python pandas csv plot plotly

Plotly.py的y轴有问题。数据未排序。即使在csv文件中,数据也按升序排序。 https://i.stack.imgur.com/SUOL3.png

import plotly
import plotly.plotly as py
import plotly.graph_objs as go
import pandas as pd
import numpy as np
import math

#What is the filename
filename = 'output.csv'
#What is the X Value?
xvalue = 'total_students'
#What is the Y Value?
yvalue = 'realcount'
#What is the plot title?
PlotTitle = 'total_students'

filename = input("What is the filename? (WITH .CSV) ")
xvalue = input("What is the X value? ")
yvalue = input("What is the y Value? ")
PlotTitle = input("What is the title of this chart? ")

esc1 = pd.read_csv('output.csv', encoding='cp1252')
def random_color():
    rgbl=[255,0,0]
    random.shuffle(rgbl)
    return tuple(rgbl)


esc1 = go.Scatter(
    x = esc1[xvalue],
    y = esc1[yvalue],
    name = 'School',
    mode = 'markers',
    marker = dict(
        size = 10,
        sizemode='area',
        sizeref=1,
        color = 'rgba(66, 134, 244, .8)'
    )
)


data = [esc1]

layout = dict(title = 'Total Student Scatter',
              yaxis = dict(zeroline = True,
                title=yvalue),
              xaxis = dict(zeroline = True,
                title=xvalue)
             )

#fig = dict(data=data, layout=layout)
#py.iplot(fig, filename='styled-scatter')

plotly.offline.plot({
    "data": data,
    "layout": layout
},filename=PlotTitle+'.html', auto_open=False)

print("Finished")

Jeeze堆栈溢出有大量的质量控制。我必须不断添加细节,希望对您有所帮助。

1 个答案:

答案 0 :(得分:0)

嗯,也很好。我的代码:

#import all the necessary libraries
import plotly
import plotly.plotly as py
import plotly.graph_objs as go
import pandas as pd
import numpy as np
import math
import random
#What is the filename
filename = 'output.csv'
#What is the X Value?
xvalue = 'total_students'
#What is the Y Value?
yvalue = 'realcount'
#What is the plot title?
PlotTitle = 'total_students'

filename = input("What is the filename? (WITH .CSV) ")
xvalue = input("What is the X value? ")
yvalue = input("What is the y Value? ")
PlotTitle = input("What is the title of this chart? ")
esc1 = pd.read_csv(filename)
#I don`t have your `output.csv` so I am created own DataFrame
#esc1 = pd.DataFrame({"total_students":["10","20","30","40","50"],\
#                     "realcount":["8","16","28","39","41"]})
def random_color():
    rgbl=[255,0,0]
    random.shuffle(rgbl)
    return tuple(rgbl)
#That`s your scatter trace 
trace = go.Scatter(
    x = esc1[xvalue],
    y = esc1[yvalue],
    name = 'School',
    mode = 'markers',
    marker = dict(
        size = 10,
        sizemode='area',
        sizeref=1,
        color = 'rgba(66, 134, 244, .8)'
    )
)
#Give trace to data
data = [trace]
#Create layout
layout = dict(title = 'Total Student Scatter',
              yaxis = dict(zeroline = True,
                title=yvalue),
              xaxis = dict(zeroline = True,
                title=xvalue)
             )
fig = dict(data=data, layout=layout)
plotly.offline.plot(fig, filename=PlotTitle+'.html', auto_open=False)
print("Finished")

并且绘图看起来不错(yaxis和xaxis顺序符合预期): Works as expected, is not it?