2

I'm using this basic code to display a plot using matplotlib. The lists for the x and y axis are large.

plt.plot(r,trajectory,"k.")
plt.title("Bifurcation diagram")
plt.xlabel("R")
plt.ylabel("Xn")
plt.show()

How can I format my plot better to view all the details?

With the above configuration what I see is

Initial

But what I want to get to is something that looks like this with each point visible (atleast high enough resolution to zoom in)

final

Update: I found that tuning the available parameters iteratively can yield good results. Used a combination of the answers below to understand parameters and arrive at this configuration.

f = plt.figure(figsize=(6,4),dpi=300)
plt.plot(r, trajectory, "k,", markersize=0.01, mew=0)
f.savefig("bifurcation_diag.png")
plt.title("Bifurcation diagram")
plt.xlabel("R")
plt.ylabel("Xn")
plt.show()

Output

3 Answers 3

2

You can use a smaller marker size. The problem is that there are so many data points that they are overlapping resulting in a dark image.

I explain this below using an example dataset. The left figure shows default marker size and the right figure shows the same data with a smaller marker size using argument ms=1. You can choose ms=2, ms=3 etc. as per your need.

import numpy as np

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4))

# Default marker size
ax1.plot(np.random.randint(0, 100, 10000), np.random.randint(0, 100, 10000), 'k.')

# Smaller marker size `ms=1`
ax2.plot(np.random.randint(0, 100, 10000), np.random.randint(0, 100, 10000), 'k.', ms=1)

enter image description here

5
  • you can also set the default marker size for your code using import matplotlib as mpl and then mpl.rcParams['lines.markersize'] = 1
    – DrBwts
    Commented May 28, 2020 at 19:31
  • Thanks! I used plt.plot(r,trajectory,"k.", ms=1) and reached a better version. Is there something I might be missing? Your plot with higher number of points looks much more fine grained compared to mine. Commented May 28, 2020 at 19:31
  • I experimented with lower marker sizes (eg. ms=0.1) but don't notice the plot getting better. Image here Commented May 28, 2020 at 19:38
  • 1
    @rs747 : Can you try adding , mew=None too?
    – Sheldore
    Commented May 28, 2020 at 19:52
  • This is what I got with plt.plot(r,trajectory,"k.", ms=1, mew=None) I definitely have an issue with the number of pixels in the plot as I can barely zoom into it. The specific study that I'm using this for involves "fractals" basically self-similar patterns when you zoom in. It would be extremely helpful if I could get a high-res image with maximum points visible. Commented May 28, 2020 at 19:59
1

You need a comma as marker, not a dot. The comma marker is just one pixel, while the dot is one "point". Set mew=0 to avoid that an outline is drawn, as the outline would be much larger than a pixel. You can also add alpha=0.2 (or similar) to have semi-transparent pixels.

This is how it could look like with:

plt.plot(r, trajectory, 'b,', markersize=.1, mew=0, alpha=0.2)

example plot

PS: The full code to reproduce the plot:

import numpy as np
from matplotlib import pyplot as plt

def logistic_map(x0, r, num_iter):
    x = np.zeros(num_iter)
    xi = x0
    for i in range(num_iter):
        xi = r * xi * (1 - xi)
        x[i] = xi
    return x

def bifurcations(x0, r_min, r_max, r_steps, num_iter, iter_to_skip):
    r_values = np.linspace(r_min, r_max, r_steps)
    bifurcations = [logistic_map(x0, r, num_iter)[iter_to_skip:] for r in r_values]
    plt.plot(r_values, bifurcations, "b,", markersize=.1, mew=0, alpha=0.2)
    plt.show()

bifurcations(0.1, 2.4, 4, 500, 1000, 5)

Setting alpha=0.1 and bifurcations(0.1, 2.4, 4, 2000, 2000, 500):

more detailed plot

5
  • Thanks for including a snippet. However, I'm not able to reproduce these results when I use plt.plot(r,trajectory,"b,", ms=.1, mew=0, alpha=0.2). This is the result. Since comma is at the level of pixels, is it possible I am missing something in terms of plot size/dpi? (I have not modified either of this in my code) Commented May 28, 2020 at 21:10
  • 1
    Try to use a larger alpha (or leaving it out). I probably used more iterations than you, so getting a darker plot when more pixels are aggregated.
    – JohanC
    Commented May 28, 2020 at 21:21
  • I used the same number of iterations (n & k). But I used rsteps=0.01. rsteps should be increments of r right? Is that a typo (rsteps=500) in your function call? Commented May 28, 2020 at 21:28
  • 1
    No typo. When using np.linspace(...., ...., rsteps), rsteps is the number of steps (e.g. 500). If you'd use np.arange(...., ...., rstep), rstep is the size of the step. A stepsize of 0.01 would be 160 steps.
    – JohanC
    Commented May 28, 2020 at 21:31
  • Thanks! I took the approach of incrementing rmin by rstep value each time instead of dividing (rmax-rmin)/rsteps. Your previous inference now adds up since your step increment is 0.0032. This output is good but a configuration that would allow zooming in to the plot get a fine grain view of the points would be extremely helpful (to notice fractals and other features). I wonder if matplotlib is the right package to achieve this. Commented May 28, 2020 at 21:36
0

I think you can obtain this result by setting the transparency of the plot. Try this:

#just add alpha to your plt.plot() method
plt.plot(r,trajectory,"k.", alpha=0.2)
4
  • This did not seem to make a difference. Image here. My intention is to have a fine grain view of the large number of points. Commented May 28, 2020 at 19:25
  • Try smaller values though... try alpha=0.005
    – Anwarvic
    Commented May 28, 2020 at 19:27
  • With smaller values of alpha the points seem to fade away as seen here. Not quite the intended effect Commented May 28, 2020 at 19:34
  • I think all it needs is some parameter tuning. I believe a suitable number for alpha will lead to a much better graph
    – Anwarvic
    Commented May 28, 2020 at 19:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.