0

Is it possible to change the color of the markers using the given of hexcode?

Code:

import plotly as px

hydrogen= px.scatter(plot_data,x='Calorific Value (kcal/kg)',y='Hydrogen (%)',title='Hydrogen Plot',
                    width=800,height=800,hover_name='Lab',color='Hexcode')

Results from the code above

The goal of my code is to change the color of the markers to its hexcode. I think I have issue to my code. I was trying to do something like this

Results from the code above

I want to make my 2d plot like this same hexcode to its marker color.

2
  • There is an argument that you can use for the px.scatter function called color_discrete_map which allows you to pass in a dictionary that lets you specify the color that you would want of each value in the plot. Commented Aug 16, 2024 at 8:39
  • thank you for respond. But I am working with 300+ hexcode and specify each of the color of the markers can cause a lot time. Is there another way to solve this problem? Commented Aug 21, 2024 at 6:45

1 Answer 1

0

Yes you can do it. Suppose your data have three columns x, y and color (in hex code). The following code have been tested to give the desired output. feel free to test it.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import random

def generate_random_hex_color():
    return "#{:06x}".format(random.randint(0, 0xFFFFFF))

# Parameters
num_points = 100

# Generate random data
data = {
    'x': np.random.rand(num_points) * 100,  # Random x values
    'y': np.random.rand(num_points) * 100,  # Random y values
    'color': [generate_random_hex_color() for _ in range(num_points)]  # Random hex colors
}

# Create DataFrame
df = pd.DataFrame(data)

# Plot
plt.figure(figsize=(10, 6))
plt.scatter(df['x'], df['y'], c=df['color'], alpha=0.7, edgecolors='w', s=100)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Scatter Plot with Random Colors')
plt.grid(True)
plt.show()

The result of this code is illustrated in the following plot screenenter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.