如何使用Plotly Express和Plotly隐藏图例

时间:2019-06-22 04:03:23

标签: python plotly data-visualization plotly-python

我试图通过首先在Plotly Express中创建一个简单的条形图,然后使用Plotly对其进行更新以精巧地学习Plotly。我想隐藏传说。

我正试图通过隐藏图例来更新原始图形,但无法使其正常工作。 This is my traceback error

我的代码

import plotly.plotly as py
import plotly.graph_objs as go
import plotly_express as px
import pandas as pd


df = pd.read_csv('C:/Users/Documents/Python/CKANMay.csv')

fig = px.bar(df, x="Publisher", y="Views", color="Publisher", barmode="overlay")

fig.update(fig, showlegend="false")

This is what the chart looks like now with the legend。基本上,我希望右边的那个可怕的传说消失

3 个答案:

答案 0 :(得分:2)

在按图创建图形后,要禁用图例,可以使用以下命令:

List.NonNullCount(List.Range(Record.FieldValues(_),6,35))

对于高级用户: 您还可以通过设置每个跟踪的showlegend属性来为图中的单个跟踪启用/禁用图例。例如:

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

struct node {
    char value[];
    struct node* next; // pointer of structure type
};

// set existing type, node, to the alias, node_t
typedef struct node node_t;


node_t *create_new_node(char value) {

    // create space for node with malloc
    node_t *result = malloc(sizeof(node_t));

    // set the value of the new node
    result->value = value;

    //strcpy(result->value, value);
    // set the value's next pointer to null

    result->next = NULL;
    return result;
}

node_t *insert_at_head(node_t **head, node_t *node_to_insert) {

    node_to_insert->next = *head;
    *head = node_to_insert;
    return node_to_insert;
}

//Prints linked list
void printlist(node_t* head) {
    node_t *temporary = head;

    while (temporary != NULL) {
        //print out the value of the node that temporary points to

        // printf("%d - ", temporary->value);
        // to move along the list
        temporary = temporary->next;
    }
    printf("\n");
}

int main() {

    node_t *tmp;
    // declaring head pointer
    node_t *head = NULL;

    // CREATING LINKED LIST
    // for (int i = 0; i < 25; i++) {
    //     tmp = create_new_node(i);
    //     // sending the address of the head variable
    //     //calling by reference
    //     //SINCE HEAD IS ALREADY A NODE POINTER
    //     insert_at_head(&head, tmp);
    // }
    printlist(head);
    
    tmp = create_new_node("I like food");
    insert_at_head(&head, tmp);

}

您可以在此处查看示例:https://plotly.com/python/legend/

答案 1 :(得分:1)

尝试一下:

    my_data = [go.Bar( x = df.Publisher, y = df.Views)]
    my_layout = go.Layout({"title": "Views by publisher",
                           "yaxis": {"title":"Views"},
                           "xaxis": {"title":"Publisher"},
                           "showlegend": False})

    fig = go.Figure(data = my_data, layout = my_layout)

    py.iplot(fig)
  • 自变量showlegend是您未在代码中指定的布局对象的一部分
  • 如果未将布局对象my_layout包装在go.Layout()中,则该代码也可能起作用。只需保留my_layout一本字典即可。

希望它对您有用。

答案 2 :(得分:0)

原始代码存在的问题是fig.update()不以fig作为参数。该行可能只是fig.update(layout_showlegend=False)