【问题标题】:Plotly python scatterplot y axis not orderingPlotly python scatterplot y轴没有排序
【发布时间】:2023-03-12 23:40:01
【问题描述】:

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 stack-overflow 有大量的质量控制。我必须继续添加细节,所以我希望这会有所帮助。

【问题讨论】:

  • 我没有足够的代表来嵌入图像。对不起!
  • 您也可以发布您的代码吗?
  • @TorinMay 我们开始吧。
  • 欢迎来到 Stackoverflow!看看minimal reproducible example。如果没有看到原始数据,真的很难猜出根本问题是什么。

标签: python pandas csv plot plotly


【解决方案1】:

嗯,也很好用。我的代码:

#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 的顺序符合预期):

【讨论】:

  • @JakeDownie 希望我的回答对你有所帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-22
  • 1970-01-01
  • 1970-01-01
  • 2020-12-15
  • 2020-07-19
  • 2022-07-06
  • 2023-03-26
相关资源
最近更新 更多