简体   繁体   中英

Plotly Color list for Scatter Plots with Graph Objects

I would like to get the CSS names of the colors for Graph Object in plotly.

I create a picture in this way:

fig_confirmed = go.Figure()
# Add trace to the figure;
for index,country in enumerate(df_frame["Country/Region"]):
dataframe = pd.read_csv('./country_data/{}.csv'.format(country))
print(country,index,named_colorscales[index])
fig_confirmed.add_trace(go.Scatter(x=dataframe['date'], y=dataframe['Confirmed'],
                                   mode='lines+markers',
                                   line_shape='spline',
                                   name='{}'.format(country),
                                   line=dict(color=named_colorscales[index], width=4),
                                   marker=dict(size=4, color='#f4f4f2',
                                               line=dict(width=1, color='#921113')),
                                   text=dataframe['date'],
                                   hovertext=[
                                       country + " Confirmed cases <br>{:,f}%".format(i)
                                       for i in dataframe["Confirmed"]

                                   ],
                                   hovertemplate="<b>%{text}</b><br></br>"
                                                 + "%{hovertext}"
                                                 + "<extra></extra>"
                                   ))

I am trying to get the CSS names using

import plotly.express as px
named_colorscales = px.colors.named_colorscales()

but apparently the CSS names when dealing with graph objects are not the same. so element of this list are not accepted. I wonder how to get the list of CSS names for graph objects.

For example if index=0, I get as color 'aggrnyl' and I get an error saying

Invalid value of type 'builtins.str' received for the 'color' property of scatter.line Received value: 'aggrnyl'

The 'color' property is a color and may be specified as:
  - A hex string (e.g. '#ff0000')
  - An rgb/rgba string (e.g. 'rgb(255,0,0)')
  - An hsl/hsla string (e.g. 'hsl(0,100%,50%)')
  - An hsv/hsva string (e.g. 'hsv(0,100%,100%)')
  - A named CSS color:
        aliceblue, antiquewhite, aqua, aquamarine, azure,
        beige, bisque, black, blanchedalmond, blue,
        blueviolet, brown, burlywood, cadetblue,
        chartreuse, chocolate, coral, cornflowerblue,
        cornsilk, crimson, cyan, darkblue, darkcyan,
        darkgoldenrod, darkgray, darkgrey, darkgreen,
        darkkhaki, darkmagenta, darkolivegreen, darkorange,
        darkorchid, darkred, darksalmon, darkseagreen,
        darkslateblue, darkslategray, darkslategrey,
        darkturquoise, darkviolet, deeppink, deepskyblue,
        dimgray, dimgrey, dodgerblue, firebrick,
        floralwhite, forestgreen, fuchsia, gainsboro,
        ghostwhite, gold, goldenrod, gray, grey, green,
        greenyellow, honeydew, hotpink, indianred, indigo,
        ivory, khaki, lavender, lavenderblush, lawngreen,
        lemonchiffon, lightblue, lightcoral, lightcyan,
        lightgoldenrodyellow, lightgray, lightgrey,
        lightgreen, lightpink, lightsalmon, lightseagreen,
        lightskyblue, lightslategray, lightslategrey,
        lightsteelblue, lightyellow, lime, limegreen,
        linen, magenta, maroon, mediumaquamarine,
        mediumblue, mediumorchid, mediumpurple,
        mediumseagreen, mediumslateblue, mediumspringgreen,
        mediumturquoise, mediumvioletred, midnightblue,
        mintcream, mistyrose, moccasin, navajowhite, navy,
        oldlace, olive, olivedrab, orange, orangered,
        orchid, palegoldenrod, palegreen, paleturquoise,
        palevioletred, papayawhip, peachpuff, peru, pink,
        plum, powderblue, purple, red, rosybrown,
        royalblue, rebeccapurple, saddlebrown, salmon,
        sandybrown, seagreen, seashell, sienna, silver,
        skyblue, slateblue, slategray, slategrey, snow,
        springgreen, steelblue, tan, teal, thistle, tomato,
        turquoise, violet, wheat, white, whitesmoke,
        yellow, yellowgreen
named_colorscales = []
import matplotlib
for name, hex in matplotlib.colors.cnames.items():
 # print(name, hex)
 named_colorscales.append(name)

don't know if it is an orthodox way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM