python matplotlib plotly graph_objects how to save a figure - python

How can I locally save the graphics which is produced like that:
import matplotlib.pyplot as plt
import plotly.graph_objects as go
fig2 = go.Figure(go.Bar(x=xData, y=yData, marker_color='lightblue'))
fig2.update_layout(title_text=title)
fig2.update_layout(width=900, height=350)
fig2.update_yaxes(showticklabels=True,range=(0, 350))
fig2.show()
Under other circumstances in the same jupyter notebook.
fig.savefig('filename') works well. But here fig2 is lacking this method.
As a newcomer all those different methods to produce graphics in the python environment are driving me crazy. Appreciate any help.

Related

draw function in scikit-geometry fails to display

i'm getting started with scikit-geometry and am trying to reproduce some of the examples, but the plot function doesn't work as expected.
i'm working in VSCode and managing my environment with Anaconda on Windows 10.
i'm just reading through the samples in the notebook. the snippet runs, but no graphic is displayed.
import skgeom
from skgeom.draw import draw
from matplotlib import pyplot as plt
import math
a = skgeom.Point2(4, 5)
print('a:', a)
draw(a, color='purple')
the docs indicate that the draw function is built from the matplotlib plot function, but it doesn't support the .show() method, so i'm not really sure what i'm doing wrong.
suggestions?

How to center bar plot in python matplotlib?

I am working on a Coursera cert and I noticed that the plots in their example labs and assignments come out like this:
I would like to do a couple things. One, if possible(but might not be necessary), to remove the legend "figure 1", the on/off symbol and the buttons at the bottom of the image. By the way, I really don't understand where this feature comes from, I hope someone can explain. Two, the main thing I want to fix is making all the labels(ticks) in the y-axis visible, i.e. move them towards the right.
The Python version in their Jupyter notebook is:
from platform import python_version
print(python_version())
3.6.2
The imports I am working with:
%matplotlib notebook
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings('ignore')
And the code that generated the plot:
categ_series.plot(kind='barh', figsize=(8, 6))
plt.show()

Display animation outside of jupyter notebook

I want to use Jupyter notebook to host my code for a presentation, but I don't want to embed animation into the notebook. (Because it is time-consuming to embed the video.) I want to run the cells and pop up a screen as if I am running the code in the terminal.
from matplotlib.animation import FuncAnimation
from matplotlib.pyplot import plot, show, subplots, title # annotate
from IPython.display import HTML
anim = FuncAnimation(fig, update, frames=numlin, interval=100, fargs=(
d, g, lr_D, lr_G, hasFake, speed, show_sample),
init_func=init, blit=True, repeat=0)
HTML(anim.to_html5_video())
Why using the notebook? The main reason to use the notebook is that I have many different setups for an experiment. I want to use different cells to represent different configurations, and if people want to see results from a particular configuration, I can run it right away.
Time difference. The HTML function takes over a minute to generate the video I need. While in the terminal, the animation would just start. I want to prototype quickly during a meeting while the audience asks to show the results from different initial conditions.
There is also an unexpected behavior from the notebook. The video from the notebook is different from that popped up in the terminal. The video in the notebook did not erase existing frames while drawing, making the animation looks messy and cannot track the trajectory as good as its counterpart.
Animation from the notebook's output
Animation from the terminal's output
This drawing behavior is another reason why I don't want to use the notebook to display the animation.
Will the notebook needs to show other plots. I hope so, but it is not necessary. I can open another notebook for just plots if needed.
Please let me know if I do not explain it well.
Animation inside of notebook
Reading the question, I wonder if you are aware of the %matplotlib notebook backend. While it will show the animation inside the notebook, I feel that it would suit all the described needs.
%matplotlib notebook
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
a = np.random.rand(10,4)
fig, ax =plt.subplots()
ax.axis([0,1,0,1])
points1, = plt.plot([],[], ls="", marker="d", color="indigo")
points2, = plt.plot([],[], ls="", marker="o", color="crimson")
def update(i):
points1.set_data(a[i:i+2,0],a[i:i+2,1])
points2.set_data(a[i:i+2,2],a[i:i+2,3])
return points1, points2
anim = FuncAnimation(fig, update, frames=len(a)-1, repeat=True)
Note that using this kind of animation, where the data is updated with set_data is showing the same whether saved to video or shown on screen.
Hence, if it wasn't for the time it takes to replace the video, you could well use it in the initially shown way, deleting %matplotlib notebook and adding
from IPython.display import HTML
HTML(anim.to_html5_video())
If using matplotlib 2.1, you may also opt for a JavaScript animation,
from IPython.display import HTML
HTML(ani.to_jshtml())
Animation in new window
If you want to have a window appearing, you should neither use %matplotlib inline nor %matplotlib notebook, instead replace the first line in the above code by
%matplotlib tk

Seaborn Plot doesn't show up

I am creating a bar chart with seaborn, and it's not generating any sort of error, but nothing happens either.
This is the code I have:
import pandas
import numpy
import matplotlib.pyplot as plt
import seaborn
data = pandas.read_csv('fy15crime.csv', low_memory = False)
seaborn.countplot(x="primary_type", data=data)
plt.xlabel('crime')
plt.ylabel('amount')
seaborn.plt.show()
I added "seaborn.plt.show() in an effort to have it show up, but it isn't working still.
You should place this line somewhere in the top cell in Jupyter to enable inline plotting:
%matplotlib inline
It's simply plt.show() you were close. No need for seaborn
I was using PyCharm using a standard Python file and I had the best luck with the following:
Move code to a Jupyter notebook (which can you do inside of PyCharm by right clicking on the project and choosing new - Jupyter Notebook)
If running a chart that takes a lot of processing time it might not have been obvious before, but in Jupyter mode you can easily see when the cell has finished processing.

My graph is not showing up and my iPython Notebooks never stops working

When I run the code below my iPython Notebooks starts working on it (I see black dot in the top right corner), but it never stops. I cannot stop it either by pressing button with the black square.
I opened my other notebook and it shows the histogram without any problem.
What can be the reason?
Thank you.
import matplotlib.pyplot as plt
from numpy.random import normal
gaussian_numbers = normal(size=1000)
plt.hist(gaussian_numbers)
plt.title("Gaussian Histogram")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()
UPDATE: Created a new notebook. Passed all the code from the previous notebook (including code that gets the data from the internet). Can any other code affect working and displaying the histogram?
There is not other code working when I run histogram.
yo need to use%matplotlib inline
%matplotlib inline
import matplotlib.pyplot as plt
from numpy.random import normal
gaussian_numbers = normal(size=1000)
plt.hist(gaussian_numbers)
plt.title("Gaussian Histogram")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()
see this other thread to automatically do this on the IPython configuration Automatically run %matplotlib inline in iPython Notebook

Categories