I'm using plotly and I ahave created pue chart. the problem is that it works on jupyter ntoebook but when I download the graph the legend is overlap with the chart.
This is the plot on jupyter notebook:
but when I download it it as png it looks like this:
as you can see the legend items have very long names, but i'm not sure that cause the problem. Is there any way to control the spacing between the chart to the legend?
Edit: code sample:
import plotly.express as px
import plotly.graph_objects as go
fig = px.pie(df, values='count', names='LC', title='Land cover')
fig.update_layout(title_x=0.48)
fig.show()
I have also tried to play with the location of the legend this way:
fig.update_layout(title_x=0.48,legend={"x" : 1.7, "y" : 1})
but as I said, in jupyter notebook it worls but in the PNG is overlap just the same, even when X is equal to 3:
Related
I am trying to layer a swarmplot on top of a violin chart just like the seaborn documentation has here: https://seaborn.pydata.org/generated/seaborn.catplot.html
But in that example they are just displaying to the screen so the following works
sns.catplot(data=df, x="age", y="class", kind="violin", color=".9", inner=None)
sns.swarmplot(data=df, x="age", y="class", size=3)
But I need to save to a file like so..
cat_plot = sns.catplot ([args go here])
cat_plot.savefig ([args])
How do I add a swarmplot to my cat_plot FacetGrid so when I save it they are layered on top of each other in the resulting jpg? Or is there another way I should be trying to accomplish this?
sns.catplot returns a FacetGrid with one or more subplots. When there is only one subplot, the subsequent sns.swarmplot() will draw onto that same subplot. Afterward, you can simply save the figure, which will include the original catplot together with all elements that have been added, either via other seaborn functions, or directly via matplotlib.
As catplot can create multiple subplots, the recommended way to add a smarmplot to all of them would be via g.map_dataframe(sns.swarmplot, x="age", y="class", size=3). (But as there is only one subplot in your example, your code also works with calling sns.swarmplot directly.)
import seaborn as sns
df = sns.load_dataset('titanic')
g = sns.catplot(data=df, x="age", y="class", kind="violin", color=".9", inner=None)
g.map_dataframe(sns.swarmplot, x="age", y="class", size=3)
g.savefig('combined-violin-swarmplot.png')
If the plotly Figure is wider than the screen, I cannot see the right part because the horizontal scroll bar is not present.
I have something like this:
I looked for everywhere but it seems that no one had this problem before.
Any advices?
Thank you in advance
I also put the code here, as requested
import plotly.express as px
long_df = px.data.medals_long()
fig = px.bar(long_df, x="nation", y="count", color="medal", title="Long-Form Input")
fig.update_layout(width=1800, height=800)
I am trying to animate a multi series line graph using plotly. However after days of going through the documentation I still can't seem to find a solution.
Currently my code is as follows:
df = px.data.gapminder().query("continent=='Oceania' ")
fig = px.line(df, x="year" , y="lifeExp", color="country" , animation_frame="year", animation_group="lifeExp" , range_y=[68,84] , range_x=[1950,2010])
plot(fig)
This however generates and empty plot. Please help.
I am able to successfully generate a scatter plot and a bar graph using similar code.
For better understanding please view below link :
I have found an exact example of what I am looking for implemented in R.
https://plot.ly/r/cumulative-animations/#cumulative-lines-animation
For the empty plot, try changing the default renderer by adding this above your code:
import plotly.io as pio
pio.renderers.default = 'notebook'
There is some documentation on different renderers.
I am using matplotlib version 3.1.1 on an ubuntu 18.04 machine and have tried this code in both python 3.6.8 and 3.7.4 on 2 different boxes.
The problem is that when there are more than 10 items the color repeats itself and I made a quick change to make hatch marks to differentiate. While this works well in display, the hatches do not show up when I savefig("*.pdf"). savefig("*.png") seems to work fine as seen in the figures below.
I have searched for this issue in multiple places but can't seem to find a solution that works. I have also tried PdfPages.
Simple code and output are attached below. I appreciate any suggestions.
Code:
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
matplotlib.__version__ #3.1.1
df=pd.DataFrame({'A':[0.1]*12, 'B':[0.1]*12}).T
tit='title'
ax=df.plot.bar(figsize=(11,8.5),stacked=True,title=tit)
nrows=len(df)
bars = ax.patches
hatches=['','-', '+', 'x','/','//','O','o','\\','\\\\']*nrows*10 #times rows*colors
#
hatches.sort()
hatches=hatches[0:len(bars)]
i=0
for bar in bars:# goes down a column with same color, then next with diff color and so on.
bar.set_hatch(hatches[i])
#bar.text('1')
#print(bar)
i+=1
ax.legend(#loc='lower center', loc='upper center',bbox_to_anchor=(0.5, -.05,0,0),fancybox=True, shadow=True, ncol=5,fontsize=5.5)
#plt.savefig('barhatch.png')
plt.savefig('barhatch.pdf')
screenshot of actual plot:
screenshot of actual plot
savefig("out.png"):
screenshot of the pdf as I can't seem to attach:
Try to use different facecolor for the filled space and edgecolor for the hatch.
I suppose the hatches are there but shown in the same color as the filled space below. I have experienced something similar with fill_between and found a solution in this GitHub discussion.
I found an interesting chart illustrating the specific bands of different trace chemical atmospheric species that can be used for detecting on satellite.
The figure above use rounded rectangle presented for the spectral measurement range.
I want to reproduce this kind of art with python.
For now, I could use Plotly package for plotting table in the same style.
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.tools import FigureFactory as FF
fig = go.Figure()
data_matrix = [['Tracer gas/nm', '200', '300','400','500'],['HCHO', "", ],
['CHOCHO',],['BrO', ], ['O3', ],['O2', ],['NO2',]]
table = FF.create_table(data_matrix)
py.iplot(table, filename='simple_table')
# py.image.save_as(fig, filename='a-simple-plot.png')
The figure shows like this:
But I found two tricky problems:
(1) I couldn't save the chart into figure.
(2) I couldn't plot the rounded rectangle on the chart.
Any advice with better solution would be appreciated!