Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making the color of external nodes to be the same as its corresponding Arcs color to destination #36

Closed
NaifahNurya opened this issue Aug 30, 2023 · 11 comments
Labels
question Further information is requested

Comments

@NaifahNurya
Copy link

Thank you for this library which is easy to use. I use the following code to show the parcel source and destination.

from pycirclize import Circos
import pandas as pd


df = pd.read_csv('deliveredparcel.csv', index_col=0)
df.index.name = None


# Initialize Circos from matrix for plotting Chord Diagram
circos = Circos.initialize_from_matrix(
    df,
    space=5,
    cmap="tab10",#"viridis", or "tab10"
    ticks_interval=100,
    label_kws=dict(size=12),#label_kws=dict(size=12, r=110),
    link_kws=dict(ec="black", lw=0.5, direction=-1),
)

circos.savefig("example07.png")
fig = circos.plotfig()

The final output is shown in the figure below (see attached file). Is there a way to modify the code so that the color of the external nodes matches the color of their corresponding arcs if those arcs are the sources? For example, in the figure, the external node "City4" has a green color, and since it is the source city delivering to "City5," the color of the corresponding arc (because it is the source) should also be green to match its external node. In summary, the color of the arc should be determined by the color of its corresponding "source" external node.

example07

@moshi4
Copy link
Owner

moshi4 commented Aug 30, 2023

Hi @NaifahNurya,

It seems like you can do what you want by converting the matrix dataframe to a transposed matrix and setting link direction=1.

from pycirclize import Circos
import pandas as pd


df = pd.read_csv('deliveredparcel.csv', index_col=0)
df.index.name = None


# Initialize Circos from matrix for plotting Chord Diagram
circos = Circos.initialize_from_matrix(
-   df,
+   df.T # Convert to transposed matrix
    space=5,
    cmap="tab10",#"viridis", or "tab10"
    ticks_interval=100,
    label_kws=dict(size=12),#label_kws=dict(size=12, r=110),
-   link_kws=dict(ec="black", lw=0.5, direction=-1),
+   link_kws=dict(ec="black", lw=0.5, direction=1), # Set direcion=1
)

circos.savefig("example07.png")
fig = circos.plotfig()

If it doesn't work, please provide the deliveredparcel.csv file for reference. I'll try to think of a solution again.

@NaifahNurya
Copy link
Author

Some how it works but not 100%, because the color of the arc not exactly the same as the color of the external node (which is a source). As you can see the figure in the attached file. For example, from City4 to City5 (the color of external node "City5" is dark gray, but its corresponding arc is light gray, however it is required to be dark gray). The same to City, City7 and City8 their color is dark but their corresponding arc from it as a source is a light. Also I provide deliveredparcel.csv] for reference as requested.
example07
deliveredparcel.csv

@moshi4
Copy link
Owner

moshi4 commented Aug 30, 2023

Unfortunately, Circos.initialize_from_matrix() does not provide a good solution to this issue.
You can make the colors match perfectly by setting the alpha value of the link color to 1.0, but the visibility of the overlapping link display will be poor.

Example Code: Link color alpha = 1.0

from pycirclize import Circos
import pandas as pd

df = pd.read_csv('deliveredparcel.csv', index_col=0)
df.index.name = None

# Initialize Circos from matrix for plotting Chord Diagram
circos = Circos.initialize_from_matrix(
    df.T,
    space=5,
    cmap="tab10",
    ticks_interval=100,
    label_kws=dict(size=12),
    link_kws=dict(ec="black", lw=0.5, direction=1, alpha=1.0), # Set alpha=1.0, by default 0.5
)

circos.savefig("example07.png")
fig = circos.plotfig()

example07.png

example07

@NaifahNurya
Copy link
Author

Thank you @moshi4 , now it looks as required. One more thing, Is there a means of labelling a % of each arc from source to destination, Calculated over the all arc (entire dataset). I checked on the documentation [https://moshi4.github.io/pyCirclize/] but I didn't find it.

@moshi4
Copy link
Owner

moshi4 commented Aug 31, 2023

Is there a means of labelling a % of each arc from source to destination, Calculated over the all arc (entire dataset).

pyCirclize does not have this functionality.

@NaifahNurya
Copy link
Author

Okay, thank you for your feedback. @moshi4

@NaifahNurya
Copy link
Author

@moshi4 , I have another question. I want to use the alpha value in the cmap. However when I applied it I got an error. Using the same code as an example, in the "link_kws" it works fine, but in the "cmap" It rises an error. My intention to use the same alpha value in both "link_kws" and "cmap", The following is the error which I got " File "C:\Users\admin\anaconda3\envs\pycirclize\lib\site-packages\pycirclize\circos.py", line 262, in initialize_from_matrix
name2color.update(cmap)
ValueError: dictionary update sequence element #0 has length 7; 2 is required"

Any solution/suggestion?

from pycirclize import Circos
import pandas as pd

df = pd.read_csv('deliveredparcel.csv', index_col=0)
df.index.name = None

# Initialize Circos from matrix for plotting Chord Diagram
circos = Circos.initialize_from_matrix(
    df.T,
    space=5,
    cmap=("tab10", alpha=0.8) #The error happens due to alpha value in this line
    ticks_interval=100,
    label_kws=dict(size=12),
    link_kws=dict(ec="black", lw=0.5, direction=1, alpha=0.8), # Set alpha=0.8 by default 0.5
)

circos.savefig("example07.png")
fig = circos.plotfig()

@moshi4
Copy link
Owner

moshi4 commented Sep 9, 2023

cmap=("tab10", alpha=0.8) is obviously wrong argument for Circos.initialize_from_matrix(), so it is natural that you get an error.
Circos.initialize_from_matrix() does not provide the functionality you need.

@NaifahNurya
Copy link
Author

Thank you for reply @moshi4 . So, how to resolve the issue? To make the transparency of the sector to be the same as the transparency of its corresponding link (arc)? Because for the link we can set it in link_kws by setting alpha value.

@moshi4
Copy link
Owner

moshi4 commented Sep 10, 2023

Circos.initialize_from_matrix() does not provide the functionality you need.

It may be possible to solve this problem by directly rewriting the code of Circos.initialize_from_matrix() as follows.

outer_track.axis(fc=color)

-   outer_track.axis(fc=color)
+   outer_track.axis(fc=color, alpha=0.8)

@moshi4 moshi4 closed this as completed Sep 10, 2023
@NaifahNurya
Copy link
Author

Thank you @moshi4

@moshi4 moshi4 added the question Further information is requested label May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants