Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add axes.violinplot test from test_datetime.py#27521

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
tanvincible wants to merge5 commits intomatplotlib:mainfromtanvincible:patch-1

Conversation

tanvincible
Copy link

@tanvincibletanvincible commentedDec 15, 2023
edited
Loading

PR summary

This PR is modeled for#26864Axes.violinplot.

Image of generated plot:

image

@tanvincibletanvincible marked this pull request as ready for reviewDecember 15, 2023 12:25
@QuLogicQuLogic mentioned this pull requestDec 16, 2023
64 tasks
@ksunden
Copy link
Member

From#27485 (comment):

All that said, I am now curious if at least the positioning values (position andwidth) work as desired (with the latter being atimedelta). Because while doing statistics on dates can get strange fast (not impossible, just have to be careful about knowing what you are doing), saying "I want a violin plot comparing measurements taken on different dates" seems completely reasonable.

I think my leaning is to deprioritize fixing of units along the stats axis of these, but ask that this test case is written to focus on positions instead for the time being.

While I agree that the "primary" piece of violinplot generally doesn't make sense/doesn't work with dates, there is stilla piece thatshould work (Though I admittedly haven't tested it)

@tanvincible
Copy link
Author

tanvincible commentedDec 19, 2023
edited
Loading

@ksunden Not sure which "piece" you are talking about... Should I test again withwidth being atimedelta?
Unfortunately, keepingpositions = date_timedeltas andwidths = timedelta(days=5) didn't worked:

importmatplotlib.pyplotaspltimportnumpyasnpfromdatetimeimportdatetime,timedeltafromcollectionsimportdefaultdictimportpytestimportmatplotlibasmpl@pytest.mark.xfail(reason="Test for violinplot not written yet")@mpl.style.context("default")classTestViolinPlot:deftest_violinplot(self):np.random.seed(42)n_samples=100dates_values= [(datetime(2023,1,1)+timedelta(days=np.random.randint(1,15)),np.random.randn())for_inrange(n_samples)]values_by_date=defaultdict(list)fordate,valueindates_values:values_by_date[date.toordinal()].append(value)date_ordinals,values=zip(*values_by_date.items())date_timedeltas= [datetime.fromordinal(date)-datetime(2023,1,1)fordateindate_ordinals]fig,ax=plt.subplots()result=ax.violinplot(values,positions=date_timedeltas,widths=timedelta(days=5),showmeans=True,showextrema=True)ax.set_title('Violin Plot with DateTime and Timedelta Positions')ax.set_xticks(date_timedeltas)ax.set_xticklabels([datetime.fromordinal(date).strftime('%Y-%m-%d')fordateindate_ordinals],rotation=45,ha='right')ax.set_xlabel('Dates')ax.set_ylabel('Values')assertresultisnotNone,"Failed to create violin plot"

Pytest Output:

=================================================testsessionstarts=================================================platformwin32--Python3.11.5,pytest-7.4.3,pluggy-1.3.0Matplotlib:3.8.2Freetype:2.6.1rootdir:C:\Users\XXXXplugins:anyio-3.5.0,asdf-3.0.1,mpl-0.16.1collected1itemXXXX\test_untitled2.pyx                                                                             [100%]=================================================1xfailedin0.50s==================================================

Edit: Corrected grammatical mistakes.

@tanvincible
Copy link
Author

Oops, I wrote the wrong test in the PR description. (The test gives an output (plus it doesn't deal with timedelta); my bad!)
Removed the test :)

@ksunden
Copy link
Member

positions is adatetime,widths is atimedelta:

fromdatetimeimportdatetime,timedeltaimportnumpyasnpimportmatplotlib.pyplotaspltnp.random.seed(19680801)n_samples=100values=np.random.randn(n_samples)fig,ax=plt.subplots()result=ax.violinplot(values,positions=[datetime(2023,1,10)],widths=[timedelta(days=10)],showmeans=True,showextrema=True)

Gives:

violin

This shows a plot that iscentered on on a datetime and has a width given by the time delta.

It automatically sets the ticks and formatter for the X axis.

So it is demonstrating that units are adhered to by positions/width.

A more complete test would probably have multiple violins (possibly on the same Axes) with different widths/positions/etc.

While thevalues arenot handed datetime values (in any sense) here, which is one way violincould have (but does not fully) supported units, the position and width (x-axis) values are supported.

tanvincible reacted with thumbs up emoji

@tanvincibletanvincible changed the titleRemove axes.violinplot test from test_datetime.pyAdd axes.violinplot test from test_datetime.pyDec 20, 2023
@tanvincible
Copy link
Author

@ksunden Could you please review?

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Projects
Status: Waiting for author
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@tanvincible@ksunden@dstansby

[8]ページ先頭

©2009-2025 Movatter.jp