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

Plot ticklabels for KDE over time #82

Open
alexanderdrent opened this issue Jun 4, 2020 · 4 comments
Open

Plot ticklabels for KDE over time #82

alexanderdrent opened this issue Jun 4, 2020 · 4 comments
Assignees
Milestone

Comments

@alexanderdrent
Copy link

In the kde_over_time plotting function (and thus in the simple_kde function), the ticklabels are emptied using
ax.set_xticklabels([])
ax.set_yticklabels([])

It would be nice if the years and outcome values would be indicated along the axis to provide better insight in what is plotted.

@steipatr
Copy link
Contributor

It doesn't answer your feature request, but you can add ticklabels back in manually by creating the figure, manipulating the axes, and then plotting/saving it. Here's possible approach from a recent project:

#outcomes of interest
ops = ['m-hotelops', 'm-beachops', 'm-diveops', 'm-boatops', 'm-waterops', 'm-all-ops']

#create KDE over time figure
fig, axes = kde_over_time(experiments, outcomes, outcomes_to_show = ops, log=True, colormap=cm.viridis)

#for each subplot in figure, set ticks and labels
for key in axes:
    ax = axes[key]

    #x-axis
    ax.set_xlabel("time [years]")
    ax.set_xticks(np.arange(0, 1051, 350))
    ax.set_xticklabels(["0","10","20","30"], rotation="horizontal")
    
    #y-axis
    t = round(np.amax(outcomes[key]),2) #get upper bound of y-axis
    yticks = ax.get_yticks() # Get locations and labels
    ylabels = [t, 0]  
    ax.set_yticks([yticks[0], yticks[-1]])
    ax.set_yticklabels(ylabels, va="center", rotation="horizontal")

    #hacky legend label
    ax.text(1.15, 0.5, "Kernel Density", transform=ax.transAxes, verticalalignment='center', rotation=90)

#format and save/plot figure
fig.set_size_inches(10, (len(ops)*3) )
plt.tight_layout()  
plt.savefig("./figures/" + "ops.png", [dpi=300)

Results in:
ops

@EwoutH EwoutH added this to the 2.3.0 milestone Aug 31, 2022
@quaquel
Copy link
Owner

quaquel commented Nov 1, 2022

I started looking into this, but it is a nontrivial issue to fix in general. We have to mimic the behavior of the MaxNLocator. This locator uses the view limits, but in this function, our view limits are based on the number of steps for which the KDE is calculated. So instead, we have to use the minimum and maximum values from the time dimension. Still, I haven't yet found an easy way of implementing this without diving deeper into custom locators in matplotlib.

Looking quickly at the source code of MaxNLocator, it might be sufficient to extend this class and overwrite the __init__ to include a vmin and vmax, and then use this in the __call__ method. Still have to test this idea.

@EwoutH
Copy link
Collaborator

EwoutH commented Nov 1, 2022

Since I'm planning on a refactor of plotting for 2.4 anyways, shall I include this issue in that effort?

@quaquel
Copy link
Owner

quaquel commented Nov 1, 2022

yes that makes sense

@EwoutH EwoutH modified the milestones: 2.3.0, 2.4.0 Nov 1, 2022
@quaquel quaquel modified the milestones: 2.4.0, 2.5.0 Apr 19, 2023
@EwoutH EwoutH modified the milestones: 2.5.0, 3.0 Dec 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants