![]() Line, = ax.plot(x, i * x, label='$y = %ix$'%i)Īx.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05),Īlternatively, shrink the current plot's width, and put the legend entirely outside the axis of the figure (note: if you use tight_layout(), then leave out ax.set_position(): import matplotlib.pyplot as pltĪx.set_position() Similarly, make the legend more horizontal and/or put it at the top of the figure (I'm also turning on rounded corners and a simple drop shadow): import matplotlib.pyplot as plt If we do the same thing, but use the bbox_to_anchor keyword argument we can shift the legend slightly outside the axes boundaries: import matplotlib.pyplot as plt So, let's start with a generic example: import matplotlib.pyplot as plt To add to what and already said, you can use the bbox_to_anchor keyword argument to place the legend partially outside the axes and/or decrease the font size.īefore you consider decreasing the font size (which can make things awfully hard to read), try playing around with placing the legend in different places: There are a number of ways to do what you want. Is it possible to obtain this – that is, a legend positioned centered and below the plot (that does not change location with window size) while listing its items horizontally? In this example, it would be great to have virginica, versicolor, and setosa listed left to right in the legend (instead of top to bottom). Moreover, when placing the legend below the plot, it may look better to have legend items listed horizontally (instead of vertically). Here is an example image of the legend being positioned too low: ![]() Because of this, the legend will sometimes accidentally overlap the plot (by being positioned too high up) or be separated from the plot by an awkwardly-large distance (by being positioned too low). However, I notice that this legend position changes based on how I view the plot (the dimensions I make the plot window, etc). I am able to get the legend below and centered to the plot by the following: plot_ly(data = iris, x = Sepal.Length, y = Petal.Length, mode = "markers", color = Species) %>% layout(legend = list(x = 0.35, y = -0.5)) The default legend items are positioned vertically and located to the right of the plot, as shown here: plot_ly(data = iris, x = Sepal.Length, y = Petal.Length, mode = "markers", color = Species) One thing I am unable to figure out is how (if it is possible) to reposition legend items so that they are listed horizontally and centered below the plot. #ot(fig,filename="subplots.I have been tweaking legends in plotly and R. Give it a try! PlotĬomplete code from plotly.subplots import make_subplotsįig.add_trace(go.Scatter(x=, y=,įig.append_trace(go.Scatter(x=, y=, And of course the same goes for the 2018 traces. And all 2017 traces except the first have showlegend=False. With the setup below, all 2017 traces are assigned to the same legendgroup="2017". I wish to have only 2 legends: 20, instead of 6 legends, easier if all the 2017 has same color along the 3 subplotsĪ correct combination of legendgroup and showlegend should do the trick. I fail to find this sample code on Plotly website.Įdit: this is a sample code: from plotly.subplots import make_subplotsįig.update_layout(height=600, width=600, title_text="Stacked Subplots") ![]() I don't need different colors amont different subplot, I can just have 3 different colors and 3 legends for the 3 years on all subplots, would be ideal if I click on for example 2017 that all the 2017 curve/line dissappear across the 25 subplots.Īnyone can share a sample code? it can be 2 instead of 25 for illustration purpose. I can use the subplot sample code but then all the 75 legends (25 X 3) will be all together with different colors and it's messy. For each subplot I have 3 seperate line:2017 ,20 with 3 times "go.Scatter", each subplot represents one country (25 countries) with always these 3 years.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |