以错误的顺序绘声绘色地打勾

2024-05-17 23:46:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据集,我正在用python绘图绘制,由于某些原因,y轴刻度的顺序错误。当y轴上的值减小时,图表上的直线将变高

以下是数据集结构的一个小示例:

dfmeds = 

Start       Name                Medication  End           Dose
2020-12-09  Yosemite Sam        Lexapro     2021-06-30    5
2020-12-10  Yosemite Sam        Lexapro     2021-06-30    5
2020-12-11  Yosemite Sam        Lexapro     2021-06-30    5
2020-12-12  Yosemite Sam        Lexapro     2021-06-30    5
2020-12-13  Yosemite Sam        Lexapro     2021-06-30    5
2020-12-14  Yosemite Sam        Lexapro     2021-06-30    5
2020-12-15  Yosemite Sam        Lexapro     2021-06-30    4
2020-12-16  Yosemite Sam        Lexapro     2021-06-30    4
2020-12-17  Yosemite Sam        Lexapro     2021-06-30    4
2020-12-18  Yosemite Sam        Lexapro     2021-06-30    4
2020-12-19  Yosemite Sam        Lexapro     2021-06-30    4
2020-12-20  Yosemite Sam        Lexapro     2021-06-30    3
2020-12-21  Yosemite Sam        Lexapro     2021-06-30    3
2020-12-22  Yosemite Sam        Lexapro     2021-06-30    3
2020-12-23  Yosemite Sam        Lexapro     2021-06-30    3
2020-12-24  Yosemite Sam        Lexapro     2021-06-30    3
2020-12-25  Yosemite Sam        Lexapro     2021-06-30    2
2020-12-26  Yosemite Sam        Lexapro     2021-06-30    2
2020-12-27  Yosemite Sam        Lexapro     2021-06-30    2
2020-12-28  Yosemite Sam        Lexapro     2021-06-30    2

我用来创建图表的代码

    fig2 = px.line(dfmeds, x='Start', y="Dose", color = "Medication",
        # labels={"Episode_Count": tally + " per Shift",
        #         "Target":"Target",
        #         "Yr_Mnth": "Date" },
        title="Medication Dosages")
    fig2.update_xaxes(tickangle=45,)
    fig2.update_yaxes(tickmode='linear')
    fig2.update_layout(template = 'plotly_white',hovermode="x unified")

令人沮丧的是,这是我的输出:

enter image description here

请特别注意绿色痕迹。你们这些善良的灵魂有没有在网络年鉴中遇到过这种奇怪的现象?!我查看了文档中的y记号,但找不到任何控制订单的方法

关于现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场现场##########################

正如两人在评论中指出的那样,缺乏数字顺序的原因是“剂量”列作为一个对象传入,因此被视为一个类别

因此,我将数据类型更改为数字,以解决:

    dfmeds["Dose"] = pd.to_numeric(dfmeds["Dose"])

尽管这带来了一个新问题,但由于剂量值的很大范围,y轴刻度都聚集在一起:

enter image description here

我觉得我应该能够通过格式化y记号来解决这个问题,尽管最好的情况是保持分类输入并控制顺序,因为这意味着每个轨迹的值可以在y轴上清晰地看到

如果有人有任何建议,我们将不胜感激


Tags: 数据顺序sam图表update原因现场start
1条回答
网友
1楼 · 发布于 2024-05-17 23:46:39

正如问题的注释部分所指出的,y轴值作为对象传入,因此被分类处理

通过将DFMED['Dose']转换为数字,我还必须从一些单元格中提取文本,这些单元格也在第一行中处理:

#strip non numeric characters
dfmeds['Dose'] = dfmeds['Dose'].str.extract('(\d+)', expand=False)
#convert to numeric
dfmeds['Dose'] = pd.to_numeric(dfmeds['Dose'])

“编辑”中提到的问题是我将“tickmode”设置为线性。也可以通过删除以下内容轻松解决:

fig2.update_yaxes(tickmode='linear')

我希望这能帮助其他需要帮助的可怜的灵魂,快乐的编程

相关问题 更多 >