Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
From#27485 (comment):
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 commentedDec 19, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@ksunden Not sure which "piece" you are talking about... Should I test again with 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. |
Oops, I wrote the wrong test in the PR description. (The test gives an output (plus it doesn't deal with timedelta); my bad!) |
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: 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. |
@ksunden Could you please review? |
Uh oh!
There was an error while loading.Please reload this page.
PR summary
This PR is modeled for#26864
Axes.violinplot
.Image of generated plot: