python – Possible to add an integer to an existing plot legend with matplotlib?

I am relatively new to Python and am making a set of scatter plots. I have found the correlation value for each plot and currently have it in the title. However, I was wondering if there is a way to add this value to the plot’s legend? Here is an example of a plot for reference. I am hoping to place the r value at the bottom of the legend. If this is confusing I am happy to clarify any details. Thanks.

plotting – Error band in the legend

I want to plot multiple data sets having error bands with legends such that the style of error band is also reflected in the corresponding legend. So for e.g. let’s say I have the following two data sets.

sinData = Table[{x, Around[Sin[x], 0.1*Sin[x]]}, {x, 0.1, Pi, 0.1}];
cosData = Table[{x, Around[Cos[x], 0.1*Sin[x]]}, {x, 0.1, Pi, 0.1}];

Now I plot them together with error bands and legend

ListPlot[{sinData, cosData}, IntervalMarkers -> "Bands", 
IntervalMarkersStyle -> {Blue, Red}, Joined -> True, 
IntervalMarkersStyle -> Gray, PlotRange -> All, 
PlotLegends -> LineLegend[{"sin", "cos"}], Frame -> True]

enter image description here

Here the problem is the legends do not have the error band. So I have the following two questions based on this problem.

1] Is it possible to show the same plot with legends having a small rectangular band in the same style as the plot where I would have the control over the height of that small rectangular band with the legend line in the middle. So for the above example the sin curve legend should look something like below with adjustable height.

enter image description here

2] Also is it possible to have different types of lined shading within the error band like for e.g. the commanly used are the chequered shading, forward line shading, backward line shading etc.
Below I have shown a portion of a plot with chequered shaded error band.

enter image description here

then it should have the corresponding chequered shaded legend

enter image description here

pandas – PYTHON Matplotlib : hatched marker in legend appears in different color than defined

I have defined 3 different markers including 1 hatched.
For the hatched marker I understood from the internet that separating the color of the hatch bar and the edge marker is confusing so I went the easy way and defined edgecolor = black.

Later on I call my legends and place them in order using plt.legend, however the hatched marker in graph appears with black hatch, and in the legend with white hatch, any idea why ?

Here is a selection of my code when I call them:

p1 = plt.scatter(x= df4_activite('To be'),
            y= df4_activite('Priorité'),
            s= 300,
            edgecolors='black',
            color='#8f1d28',
            alpha=1, label="Ressources",
            zorder = 5)


p2 = plt.scatter(x= df4_activite('Ressources'),
            y= df4_activite('Priorité'),
            s= 300,
            edgecolors='black',
            color='#399432',
            alpha=1, label="Ressources",
           zorder = 5)


 # hatched marker for the outlier:
p3 = plt.scatter(value_max-0.19,6,
            s =300, 
            marker="o",
            hatch=5*"https://stackoverflow.com/",
            edgecolors='black',
            facecolor='#399432',
            #label="outlier: {}".format(round(df4_activite(('Ressources','To be')).max().max(),1)),
            zorder = 5)

plt.legend((p1,p2,p3),
           ("marker 1", "marker 2", "marker 3"),
           prop={'size': legendlabel},
           bbox_to_anchor=(1.4,0.96))

The result with the marker issue looks like this:

enter image description here

r – ggplot: how to add legend to a plot composed of several geom_ribbon() and geom_line()?

Question: how can I add a legend to this specific plot?

I have

enter image description here

The legend should include:

nd$y_fem – the blue line – should be in the legend as “5-yrs probability of death”

nd$y_tre – the red line – should be in the legend as “3-yrs probability of death”

nd$y_et – the green line – should be in the legend as “1-yr probability of death”

Preferably, the legend should include both the line and the fill.

How can this be done?

ggplot(nd, aes(x=n_fjernet))  +
  geom_ribbon(aes(ymin = y_tre, ymax = y_fem), alpha = .15, fill="#2C77BF") +
  geom_line(aes(y=y_fem), size=3, color="white") +  
  geom_line(aes(y=y_fem), color="#2C77BF", size=.85) + 

  geom_ribbon(aes(ymin = y_et, ymax = y_tre), alpha = .15, fill="#E38072") +     
  geom_line(aes(y=y_tre), size=3, color="white") + 
  geom_line(aes(y=y_tre), color="#E38072", size=.85) +

  geom_ribbon(aes(ymin = 0, ymax = y_et), alpha = .15, fill="#6DBCC3") + 
  geom_line(aes(y=y_et), size=3, color="white") +
  geom_line(aes(y=y_et), color="#6DBCC3",  size=.85) + 

  scale_x_continuous(breaks = seq(0,10,2), limits=c(0,10)) 

My data

nd <- structure(list(y_et = c(0.473, 0.473, 0.472, 0.471, 0.471, 0.47, 
0.47, 0.469, 0.468, 0.468, 0.467, 0.467, 0.466, 0.465, 0.465, 
0.464, 0.464, 0.463, 0.462, 0.462, 0.461, 0.461, 0.46, 0.459, 
0.459, 0.458, 0.458, 0.457, 0.456, 0.456, 0.455, 0.455, 0.454, 
0.453, 0.453, 0.452, 0.452, 0.451, 0.45, 0.45, 0.449, 0.449, 
0.448, 0.447, 0.447, 0.446, 0.446, 0.445, 0.445, 0.444, 0.443, 
0.443, 0.442, 0.442, 0.441, 0.44, 0.44, 0.439, 0.439, 0.438, 
0.438, 0.437, 0.436, 0.436, 0.435, 0.435, 0.434, 0.433, 0.433, 
0.432, 0.432, 0.431, 0.431, 0.43, 0.429, 0.429, 0.428, 0.428, 
0.427, 0.427, 0.426, 0.425, 0.425, 0.424, 0.424, 0.423, 0.423, 
0.422, 0.421, 0.421, 0.42, 0.42, 0.419, 0.419, 0.418, 0.417, 
0.417, 0.416, 0.416, 0.415), y_tre = c(0.895, 0.894, 0.894, 0.893, 
0.893, 0.893, 0.892, 0.892, 0.891, 0.891, 0.89, 0.89, 0.889, 
0.889, 0.889, 0.888, 0.888, 0.887, 0.887, 0.886, 0.886, 0.886, 
0.885, 0.885, 0.884, 0.884, 0.883, 0.883, 0.882, 0.882, 0.881, 
0.881, 0.881, 0.88, 0.88, 0.879, 0.879, 0.878, 0.878, 0.877, 
0.877, 0.876, 0.876, 0.875, 0.875, 0.875, 0.874, 0.874, 0.873, 
0.873, 0.872, 0.872, 0.871, 0.871, 0.87, 0.87, 0.869, 0.869, 
0.868, 0.868, 0.867, 0.867, 0.866, 0.866, 0.865, 0.865, 0.865, 
0.864, 0.864, 0.863, 0.863, 0.862, 0.862, 0.861, 0.861, 0.86, 
0.86, 0.859, 0.859, 0.858, 0.858, 0.857, 0.857, 0.856, 0.856, 
0.855, 0.855, 0.854, 0.854, 0.853, 0.853, 0.852, 0.852, 0.851, 
0.851, 0.85, 0.85, 0.849, 0.848, 0.848), y_fem = c(0.974, 0.974, 
0.973, 0.973, 0.973, 0.973, 0.973, 0.973, 0.972, 0.972, 0.972, 
0.972, 0.972, 0.971, 0.971, 0.971, 0.971, 0.971, 0.971, 0.97, 
0.97, 0.97, 0.97, 0.97, 0.969, 0.969, 0.969, 0.969, 0.969, 0.968, 
0.968, 0.968, 0.968, 0.968, 0.967, 0.967, 0.967, 0.967, 0.967, 
0.966, 0.966, 0.966, 0.966, 0.966, 0.965, 0.965, 0.965, 0.965, 
0.965, 0.964, 0.964, 0.964, 0.964, 0.963, 0.963, 0.963, 0.963, 
0.963, 0.962, 0.962, 0.962, 0.962, 0.961, 0.961, 0.961, 0.961, 
0.961, 0.96, 0.96, 0.96, 0.96, 0.959, 0.959, 0.959, 0.959, 0.958, 
0.958, 0.958, 0.958, 0.957, 0.957, 0.957, 0.957, 0.957, 0.956, 
0.956, 0.956, 0.956, 0.955, 0.955, 0.955, 0.955, 0.954, 0.954, 
0.954, 0.954, 0.953, 0.953, 0.953, 0.952), n_fjernet = c(0, 0.1, 
0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.1, 1.2, 1.3, 1.4, 
1.5, 1.6, 1.7, 1.8, 1.9, 2, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 
2.8, 2.9, 3, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4, 
4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5, 5.1, 5.2, 5.3, 
5.4, 5.5, 5.6, 5.7, 5.8, 5.9, 6, 6.1, 6.2, 6.3, 6.4, 6.5, 6.6, 
6.7, 6.8, 6.9, 7, 7.1, 7.2, 7.3, 7.4, 7.5, 7.6, 7.7, 7.8, 7.9, 
8, 8.1, 8.2, 8.3, 8.4, 8.5, 8.6, 8.7, 8.8, 8.9, 9, 9.1, 9.2, 
9.3, 9.4, 9.5, 9.6, 9.7, 9.8, 9.9)), row.names = c(NA, -100L), class = c("data.table", 
"data.frame"))

Google Sheets: How can I Set the Order on Items in a Chart Legend?

I have these two charts which pull the same data from the same table. The legend items appear in reverse order in the second chart. Anyone know how to make those match?

Reference: https://imgur.com/a/FbLRdyX

How is the default GeoRegionValuePlot bar legend defined?

I want to change the default bar legend from GeoRegionValuePlot but I can't find any documentation on how it is defined. Is it possible to get the default legend from a combination of options like PlotLegends, ColorFunction, and ColorFunctionScaling?

Python Matplotlib creating a color map legend in a scatter plot that uses a third variable

I have a plot. I managed to plot the point cloud using a third variable to indicate the color of the marker. Now I want to create a color map indicating the intensity of the third variable. I have referred to the following, but I have not resolved my problem.
Referred: Matplotlib: Add a color legend to the point cloud

My current code and exit:

plt.scatter(xadata,ydata,c=zdata,cmap='gist_heat')
plt.show()

enter description of image here

Any idea on how to solve it?

WV music legend Bill Withers dies at 81 | Forum promotion

Bill Withers, the soft-spoken baritone behind classic songs like "Ain & # 39; t No Sunshine", "Lean on Me" and "Use Me" has passed away. Withers was 81 years old. According to a family statement to the Associated Press, he died Monday in Los Angeles of heart complications. (Friday morning, Withers' official Facebook page shared an obituary from Billboard that references the PA report.)

Withers, who disdained the machinations of the record industry, stopped recording in 1985, just 14 years after becoming a star with his debut album, Just As I Am.

The son of a West Virginia coal miner, William Harrison Withers, Jr. was born on July 4, 1938. He grew up with a stutter and was one of 13 children in his family. Only six survived childhood.

He was the first man in his family not to go into the mines, and he was looking forward to moving away from the place where he had grown up, as he told NPR & # 39 ; s Morning Edition in 2015.

Withers' father died when he was just 13 years old. Another family tragedy soon struck. "My social idol was my older brother," Withers told NPR. "He got injured in the coal mines – he was run over by a coal cart – so he couldn't work there anymore." Withers' brother became a postman and he also saw a way out of the mines.

Withers joined the Navy after graduating from high school in 1956. After nine years, he first moved to San Jose, California and, a few years later, to Los Angeles. . For a while, he was a dairy, then worked in an aircraft parts manufacturing plant. In the evening, he sat as a singer in small clubs in the city. Between shifts, he learned to play the guitar and started writing his own songs, which he started shopping on labels.

Withers was signed for the first time by Clarence Avant at Sussex Records; Avant hired Booker T. Jones to produce Just As I Am in 1971. (Stephen Stills played solo guitar.)

The album culminated in the hit single "Ain & # 39; t No Sunshine", which went to No. 3 on the Billboard charts and won a Grammy for Best R&B Song the following year.

But, as Withers told NPR, "Ain & # 39; t No Sunshine" had started as a B side; the representatives of the label did not see the promise of the song. "The jockeys discs, God bless them, turned them over, and that's how I started," he said, adding a zinger: "I'm calling A&R ( the record label "artist and repertoire" makers "antagonistic & redundant" ", and that's why – because they make these great decisions like that.

The cover of Just As I Am shows Withers standing at the door of the factory where he was still working while he was recording the project, carrying his lunch bucket. At that time, Withers was already 32 years old.

The following year, Withers released a second album, Still Bill. His first single, "Lean on Me", went to number 1; the second single from the album, "Use Me", went to number 2. Withers also became a sought-after songwriter for other artists, composing for stars such as Gladys Knight and José Feliciano . He made two other albums for Sussex – Live At Carnegie Hall in 1973 and + & # 39; Justments in 1974 – before the label folded.

Withers signed with Columbia Records in 1975, but it was not a happy arrangement. Withers wanted to continue writing his own songs, but later he said in interviews that Columbia had tried to turn him into someone he was not – by pushing him to record songs. covers of Elvis Presley, for example. Columbia thought it was difficult to work with him. Either way, it was clear that the two sides simply did not overlap.

None of Withers' five albums for Columbia reached the Top 40. In 1981, he had his last big hit: "Just The Two Of Us", a duet with saxophonist Grover Washington Jr. Four years later , his recording contract with Columbia has ended, and Withers, for all intents and purposes, has moved away from the public as a performing artist. (However, he sometimes continued to write songs for others: for example, he wrote for Jimmy Buffett's 2004 album License To Chill, as well as for George Benson's Songs And Stories project in 2009.)

He was inducted into the Rock and Roll Hall of Fame in 2015. At the time, he told Rolling Stone: "I see this as a reward for attrition. The few songs that I have written in my brief career, there isn’t a genre that somebody didn’t record them. I’m not a virtuoso, but I was able to write songs to which people could identify. I don't think I hurt a guy from Slab Fork, West Virginia. "

python – Format sub-traces with Matplotlib – Legend, titles, space

I am trying to use subplots with my data but the result is ugly. Could you improve my code? For a reproducible example, I provide data.

plot_data1.to_json()
'{"('price', 'cu2003')":{"0.0":0.0,"0.05":0.0,"0.1":0.0,"0.15000000000000002":0.0,"0.2":0.0,"0.25":0.0108436348,"0.30000000000000004":0.0216146115,"0.35000000000000003":0.0216755175,"0.4":0.0218316778,"0.45":0.0219418541,"0.5":0.0220531481,"0.55":0.0325273772,"0.6000000000000001":0.0433041031,"0.65":0.0437588885,"0.7000000000000001":0.0441111601,"0.75":0.0561041293,"0.8":0.0657966882,"0.8500000000000001":0.08670207,"0.9":0.110314396,"0.9500000000000001":0.1924818841,"1.0":0.5920403465},"('price', 'cu2004')":{"0.0":0.0,"0.05":0.0,"0.1":0.0,"0.15000000000000002":0.0,"0.2":0.0,"0.25":0.0215354797,"0.30000000000000004":0.02159594,"0.35000000000000003":0.0217367677,"0.4":0.0218221495,"0.45":0.0219514872,"0.5":0.02236386,"0.55":0.0431825542,"0.6000000000000001":0.0435492651,"0.65":0.0438260107,"0.7000000000000001":0.0449387709,"0.75":0.065267051,"0.8":0.0666592601,"0.8500000000000001":0.0876136239,"0.9":0.1118193,"0.9500000000000001":0.1999777802,"1.0":0.7091412742},"('price', 'cu2005')":{"0.0":0.0,"0.05":0.0,"0.1":0.0,"0.15000000000000002":0.0,"0.2":0.0107388316,"0.25":0.0214569252,"0.30000000000000004":0.0215354797,"0.35000000000000003":0.0216896215,"0.4":0.0217841194,"0.45":0.0219178082,"0.5":0.0323275862,"0.55":0.0430338892,"0.6000000000000001":0.0434074878,"0.65":0.0437301848,"0.7000000000000001":0.0537172325,"0.75":0.0649420933,"0.8":0.0664378253,"0.8500000000000001":0.0871934605,"0.9":0.1107787748,"0.9500000000000001":0.1994901917,"1.0":0.7296849088},"('price', 'cu2006')":{"0.0":0.0,"0.05":0.0,"0.1":0.0,"0.15000000000000002":0.0,"0.2":0.0107596299,"0.25":0.0109075044,"0.30000000000000004":0.0214615302,"0.35000000000000003":0.0215308429,"0.4":0.0217556837,"0.45":0.0221116639,"0.5":0.0324815938,"0.55":0.0332299513,"0.6000000000000001":0.0430848772,"0.65":0.0437015186,"0.7000000000000001":0.0541653125,"0.75":0.0645647261,"0.8":0.066830029,"0.8500000000000001":0.0870890486,"0.9":0.111049417,"0.9500000000000001":0.1952701237,"1.0":0.739677633},"('price', 'cu2007')":{"0.0":0.0,"0.05":0.0,"0.1":0.0,"0.15000000000000002":0.0,"0.2":0.0107307651,"0.25":0.0108342362,"0.30000000000000004":0.0109349371,"0.35000000000000003":0.0214661372,"0.4":0.0216731686,"0.45":0.0220361393,"0.5":0.0322026621,"0.55":0.0326299761,"0.6000000000000001":0.0427853246,"0.65":0.0435019032,"0.7000000000000001":0.0536768653,"0.75":0.0554877372,"0.8":0.0665705093,"0.8500000000000001":0.0859660434,"0.9":0.1104362231,"0.9500000000000001":0.1879699248,"1.0":0.782196761},"('price', 'cu2008')":{"0.0":0.0,"0.05":0.0,"0.1":0.0,"0.15000000000000002":0.0106963312,"0.2":0.0108084738,"0.25":0.0109926349,"0.30000000000000004":0.0214178625,"0.35000000000000003":0.0216379963,"0.4":0.0221018897,"0.45":0.0321750322,"0.5":0.0326299761,"0.55":0.0428311382,"0.6000000000000001":0.0434877147,"0.65":0.0535274596,"0.7000000000000001":0.0545077946,"0.75":0.0649561546,"0.8":0.0759960916,"0.8500000000000001":0.0961846746,"0.9":0.1218431546,"0.9500000000000001":0.1988730527,"1.0":0.94578247},"('price', 'cu2009')":{"0.0":0.0,"0.05":0.0,"0.1":0.0,"0.15000000000000002":0.0,"0.2":0.010686044,"0.25":0.0107642626,"0.30000000000000004":0.0108283703,"0.35000000000000003":0.0212765957,"0.4":0.0215053763,"0.45":0.0216943269,"0.5":0.0320307495,"0.55":0.032449973,"0.6000000000000001":0.042593973,"0.65":0.0433228636,"0.7000000000000001":0.053561864,"0.75":0.0641162642,"0.8":0.074906367,"0.8500000000000001":0.0883782589,"0.9":0.1300672014,"0.9500000000000001":0.2331261101,"1.0":0.778679535},"('price', 'cu2010')":{"0.0":0.0,"0.05":0.0,"0.1":0.0,"0.15000000000000002":0.0106598444,"0.2":0.0107712193,"0.25":0.0110217128,"0.30000000000000004":0.0215354797,"0.35000000000000003":0.0319216855,"0.4":0.0323939099,"0.45":0.0427441761,"0.5":0.0532141337,"0.55":0.054206418,"0.6000000000000001":0.0646342777,"0.65":0.0747224594,"0.7000000000000001":0.0853697578,"0.75":0.0967014075,"0.8":0.1099989,"0.8500000000000001":0.1320422535,"0.9":0.1623025319,"0.9500000000000001":0.2554278416,"1.0":2.5132705016},"('price', 'cu2011')":{"0.0":0.0,"0.05":0.0,"0.1":0.0,"0.15000000000000002":0.0106462259,"0.2":0.0107921433,"0.25":0.0212811236,"0.30000000000000004":0.0216614318,"0.35000000000000003":0.0323415265,"0.4":0.0431872166,"0.45":0.0539548937,"0.5":0.0650406504,"0.55":0.0758972135,"0.6000000000000001":0.0873362445,"0.65":0.0985761227,"0.7000000000000001":0.1099021871,"0.75":0.1275645796,"0.8":0.1402827236,"0.8500000000000001":0.1620045361,"0.9":0.2016129032,"0.9500000000000001":0.3650442478,"1.0":2.5024587477},"('price', 'cu2012')":{"0.0":0.0,"0.05":0.0,"0.1":0.0,"0.15000000000000002":0.0106269926,"0.2":0.0107296137,"0.25":0.0109625082,"0.30000000000000004":0.0214546235,"0.35000000000000003":0.0318775901,"0.4":0.0324184137,"0.45":0.0429000429,"0.5":0.0532028091,"0.55":0.0638365784,"0.6000000000000001":0.0744522442,"0.65":0.085515767,"0.7000000000000001":0.0965043963,"0.75":0.1092060719,"0.8":0.1280956447,"0.8500000000000001":0.1486357363,"0.9":0.1768542058,"0.9500000000000001":0.2403846154,"1.0":1.3820441397},"('price', 'cu2101')":{"0.0":0.0,"0.05":0.0,"0.1":0.0,"0.15000000000000002":0.0106168383,"0.2":0.0107933081,"0.25":0.0214247456,"0.30000000000000004":0.031870817,"0.35000000000000003":0.04264847,"0.4":0.0531067446,"0.45":0.0638841567,"0.5":0.0750509274,"0.55":0.0860770389,"0.6000000000000001":0.0967950097,"0.65":0.1076310408,"0.7000000000000001":0.1183686646,"0.75":0.1379749522,"0.8":0.1543380002,"0.8500000000000001":0.1828153565,"0.9":0.2363304329,"0.9500000000000001":0.7269617276,"1.0":2.8963414634},"('price', 'cu2102')":{"0.0":0.0,"0.05":0.0,"0.1":0.0,"0.15000000000000002":0.0,"0.2":0.0106871861,"0.25":0.0212134069,"0.30000000000000004":0.0322130355,"0.35000000000000003":0.0537345513,"0.4":0.0873553178,"0.45":0.1270244522,"0.5":0.1591005515,"0.55":0.1960143744,"0.6000000000000001":0.2392604676,"0.65":0.2866546342,"0.7000000000000001":0.32873807,"0.75":0.3646894776,"0.8":0.4151586119,"0.8500000000000001":0.4710416444,"0.9":0.5397819281,"0.9500000000000001":0.7334963325,"1.0":3.0713447798}}'
plot_data2 = df_min_chng_3m.quantile(np.arange(0,1.05, 0.05)) *100
'{"('price', 'cu2003')":{"0.0":0.0,"0.05":0.0,"0.1":0.0,"0.15000000000000002":0.0,"0.2":0.0,"0.25":0.0108436348,"0.30000000000000004":0.0216146115,"0.35000000000000003":0.0216755175,"0.4":0.0218316778,"0.45":0.0219418541,"0.5":0.0220531481,"0.55":0.0325273772,"0.6000000000000001":0.0433041031,"0.65":0.0437588885,"0.7000000000000001":0.0441111601,"0.75":0.0561041293,"0.8":0.0657966882,"0.8500000000000001":0.08670207,"0.9":0.110314396,"0.9500000000000001":0.1924818841,"1.0":0.5920403465},"('price', 'cu2004')":{"0.0":0.0,"0.05":0.0,"0.1":0.0,"0.15000000000000002":0.0,"0.2":0.0,"0.25":0.0215354797,"0.30000000000000004":0.02159594,"0.35000000000000003":0.0217367677,"0.4":0.0218221495,"0.45":0.0219514872,"0.5":0.02236386,"0.55":0.0431825542,"0.6000000000000001":0.0435492651,"0.65":0.0438260107,"0.7000000000000001":0.0449387709,"0.75":0.065267051,"0.8":0.0666592601,"0.8500000000000001":0.0876136239,"0.9":0.1118193,"0.9500000000000001":0.1999777802,"1.0":0.7091412742},"('price', 'cu2005')":{"0.0":0.0,"0.05":0.0,"0.1":0.0,"0.15000000000000002":0.0,"0.2":0.0107388316,"0.25":0.0214569252,"0.30000000000000004":0.0215354797,"0.35000000000000003":0.0216896215,"0.4":0.0217841194,"0.45":0.0219178082,"0.5":0.0323275862,"0.55":0.0430338892,"0.6000000000000001":0.0434074878,"0.65":0.0437301848,"0.7000000000000001":0.0537172325,"0.75":0.0649420933,"0.8":0.0664378253,"0.8500000000000001":0.0871934605,"0.9":0.1107787748,"0.9500000000000001":0.1994901917,"1.0":0.7296849088},"('price', 'cu2006')":{"0.0":0.0,"0.05":0.0,"0.1":0.0,"0.15000000000000002":0.0,"0.2":0.0107596299,"0.25":0.0109075044,"0.30000000000000004":0.0214615302,"0.35000000000000003":0.0215308429,"0.4":0.0217556837,"0.45":0.0221116639,"0.5":0.0324815938,"0.55":0.0332299513,"0.6000000000000001":0.0430848772,"0.65":0.0437015186,"0.7000000000000001":0.0541653125,"0.75":0.0645647261,"0.8":0.066830029,"0.8500000000000001":0.0870890486,"0.9":0.111049417,"0.9500000000000001":0.1952701237,"1.0":0.739677633},"('price', 'cu2007')":{"0.0":0.0,"0.05":0.0,"0.1":0.0,"0.15000000000000002":0.0,"0.2":0.0107307651,"0.25":0.0108342362,"0.30000000000000004":0.0109349371,"0.35000000000000003":0.0214661372,"0.4":0.0216731686,"0.45":0.0220361393,"0.5":0.0322026621,"0.55":0.0326299761,"0.6000000000000001":0.0427853246,"0.65":0.0435019032,"0.7000000000000001":0.0536768653,"0.75":0.0554877372,"0.8":0.0665705093,"0.8500000000000001":0.0859660434,"0.9":0.1104362231,"0.9500000000000001":0.1879699248,"1.0":0.782196761},"('price', 'cu2008')":{"0.0":0.0,"0.05":0.0,"0.1":0.0,"0.15000000000000002":0.0106963312,"0.2":0.0108084738,"0.25":0.0109926349,"0.30000000000000004":0.0214178625,"0.35000000000000003":0.0216379963,"0.4":0.0221018897,"0.45":0.0321750322,"0.5":0.0326299761,"0.55":0.0428311382,"0.6000000000000001":0.0434877147,"0.65":0.0535274596,"0.7000000000000001":0.0545077946,"0.75":0.0649561546,"0.8":0.0759960916,"0.8500000000000001":0.0961846746,"0.9":0.1218431546,"0.9500000000000001":0.1988730527,"1.0":0.94578247},"('price', 'cu2009')":{"0.0":0.0,"0.05":0.0,"0.1":0.0,"0.15000000000000002":0.0,"0.2":0.010686044,"0.25":0.0107642626,"0.30000000000000004":0.0108283703,"0.35000000000000003":0.0212765957,"0.4":0.0215053763,"0.45":0.0216943269,"0.5":0.0320307495,"0.55":0.032449973,"0.6000000000000001":0.042593973,"0.65":0.0433228636,"0.7000000000000001":0.053561864,"0.75":0.0641162642,"0.8":0.074906367,"0.8500000000000001":0.0883782589,"0.9":0.1300672014,"0.9500000000000001":0.2331261101,"1.0":0.778679535},"('price', 'cu2010')":{"0.0":0.0,"0.05":0.0,"0.1":0.0,"0.15000000000000002":0.0106598444,"0.2":0.0107712193,"0.25":0.0110217128,"0.30000000000000004":0.0215354797,"0.35000000000000003":0.0319216855,"0.4":0.0323939099,"0.45":0.0427441761,"0.5":0.0532141337,"0.55":0.054206418,"0.6000000000000001":0.0646342777,"0.65":0.0747224594,"0.7000000000000001":0.0853697578,"0.75":0.0967014075,"0.8":0.1099989,"0.8500000000000001":0.1320422535,"0.9":0.1623025319,"0.9500000000000001":0.2554278416,"1.0":2.5132705016},"('price', 'cu2011')":{"0.0":0.0,"0.05":0.0,"0.1":0.0,"0.15000000000000002":0.0106462259,"0.2":0.0107921433,"0.25":0.0212811236,"0.30000000000000004":0.0216614318,"0.35000000000000003":0.0323415265,"0.4":0.0431872166,"0.45":0.0539548937,"0.5":0.0650406504,"0.55":0.0758972135,"0.6000000000000001":0.0873362445,"0.65":0.0985761227,"0.7000000000000001":0.1099021871,"0.75":0.1275645796,"0.8":0.1402827236,"0.8500000000000001":0.1620045361,"0.9":0.2016129032,"0.9500000000000001":0.3650442478,"1.0":2.5024587477},"('price', 'cu2012')":{"0.0":0.0,"0.05":0.0,"0.1":0.0,"0.15000000000000002":0.0106269926,"0.2":0.0107296137,"0.25":0.0109625082,"0.30000000000000004":0.0214546235,"0.35000000000000003":0.0318775901,"0.4":0.0324184137,"0.45":0.0429000429,"0.5":0.0532028091,"0.55":0.0638365784,"0.6000000000000001":0.0744522442,"0.65":0.085515767,"0.7000000000000001":0.0965043963,"0.75":0.1092060719,"0.8":0.1280956447,"0.8500000000000001":0.1486357363,"0.9":0.1768542058,"0.9500000000000001":0.2403846154,"1.0":1.3820441397},"('price', 'cu2101')":{"0.0":0.0,"0.05":0.0,"0.1":0.0,"0.15000000000000002":0.0106168383,"0.2":0.0107933081,"0.25":0.0214247456,"0.30000000000000004":0.031870817,"0.35000000000000003":0.04264847,"0.4":0.0531067446,"0.45":0.0638841567,"0.5":0.0750509274,"0.55":0.0860770389,"0.6000000000000001":0.0967950097,"0.65":0.1076310408,"0.7000000000000001":0.1183686646,"0.75":0.1379749522,"0.8":0.1543380002,"0.8500000000000001":0.1828153565,"0.9":0.2363304329,"0.9500000000000001":0.7269617276,"1.0":2.8963414634},"('price', 'cu2102')":{"0.0":0.0,"0.05":0.0,"0.1":0.0,"0.15000000000000002":0.0,"0.2":0.0106871861,"0.25":0.0212134069,"0.30000000000000004":0.0322130355,"0.35000000000000003":0.0537345513,"0.4":0.0873553178,"0.45":0.1270244522,"0.5":0.1591005515,"0.55":0.1960143744,"0.6000000000000001":0.2392604676,"0.65":0.2866546342,"0.7000000000000001":0.32873807,"0.75":0.3646894776,"0.8":0.4151586119,"0.8500000000000001":0.4710416444,"0.9":0.5397819281,"0.9500000000000001":0.7334963325,"1.0":3.0713447798}}'

f, ax = plt.subplots(2,1)


colors = ('#a6cee3','#1f78b4','#b2df8a','#33a02c','#fb9a99','#e31a1c','#fdbf6f',
           '#ff7f00','#cab2d6','#6a3d9a','#ffff99','#b15928')
plot_data1.plot(kind = 'bar', color = colors, ax = ax(0))
plt.title('Quantiles of Maximum Percent Change of Price over a 3 Minutes window', fontsize = 18)

plt.ylabel('Maximum Percent Change of Price n Over a 3 minutes window (%)')
xs = plt.xticks()
plt.xticks(xs(0), (f'{x:.2f}' for x in plot_data.index))




colors = ('#a6cee3','#1f78b4','#b2df8a','#33a02c','#fb9a99','#e31a1c','#fdbf6f',
           '#ff7f00','#cab2d6','#6a3d9a','#ffff99','#b15928')
plot_data2.plot(kind = 'bar', color = colors, ax = ax(1))
plt.title('Quantiles of Minimum Percent Change of Price over a 3 Minutes window', fontsize = 18)

plt.ylabel('Minimum Percent Change of Price n Over a 3 minutes window (%)')
xs = plt.xticks()
plt.xticks(xs(0), (f'{x:.2f}' for x in plot_data.index))

enter description of image here

r – Rearrange the columns of a bar graph and its legend

When processing and plotting data in R using tidyverse, by default it leaves the order of the bar graphs in the original order of the column factors:

library(tidyverse)

mpg$trans <- as.factor(mpg$trans)

mpg %>%
  group_by(trans) %>%
  count(trans) %>%
  ggplot(., aes(x = trans, y = n, fill = trans)) +
  geom_col()

insert description of image here

I would like the bars to appear in order of frequency. I had already asked a similar question here, but at the time I had not considered the case with a caption. So when I use the solution to this question, I get the result below:

mpg %>%
  group_by(trans) %>%
  count(trans) %>%
  ggplot(., aes(x = reorder(trans, n), y = n, fill = trans)) +
  geom_col()

insert description of image here

Note the use of the function reorder within the function ggplot to leave the bars in the order I want. However, the order of the elements in the legend trans remains in the initial order of factor levels.

What I would like: that the bars be in the same order as that shown in the previous graph, with the legend in the same order. In the CMR above, the order of the subtitles should be

  • auto (l3)
  • auto (s4)
  • auto (s5)
  • manual (m5)
  • auto (l4)

How can I do it automatically? In other words, without having to define factor levels trans manually?

Ignore the legend of the overlapping X-axis. I ended up not solving this problem in my example, but the final version of the graph will solve this problem.