How to center bar plot in python matplotlib? - python

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()

Related

Matplotlib plot lines are choppy

I have some plots generated by matplotlib, which I have been generating using the same code (for different data) for several years. The plots always come out looking normal. This time, I re-ran the same code using different data, and the plots showed up like
(Note that the missing border at the top is the result of my erasing the legend. The problem is the choppy, broken up blue and orange data lines.) Many of the plots generated are broken up like this, but not all are. All were normal the previous times that I ran this code.
I have no idea what's going on to make the lines all broken up like this. I tried re-starting my computer and it didn't help. I can't seem to find anything online about this issue. Does anyone have any idea what's going on here?
Here's the code that generated it (excerpted)
`
import matplotlib
from numpy import NaN
matplotlib.use('Agg')
from matplotlib import pyplot as plt
from PyPDF2 import PdfFileMerger, PdfFileReader
for chan in channels:
dProd.plot(x='Date',y=[a_chan,b_chan],secondary_y=b_chan)
plt.title(chan).get_figure().savefig(folder+'\\'+prod+'\\plots\\'+chan+'.pdf')
plt.close('all')
`

python matplotlib plotly graph_objects how to save a figure

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.

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

SVG rendering issues using iPython inline plots

when I use inline plots in iPython (QtConsole), the first plot looks (more or less) fine, but then it gets weirder and weirder. When I plot something several times (so plot, see it displayed, plot again, see output etc.), it looks like it is being overlaid with the skewed previous picture. So after plotting a diagonal line (x=y) 4 times in a row I get something like this
If i right click and export it as svg everything looks good
(Exported PNG picture remains wrecked as the first one).
I guess the problem is similar to https://github.com/ipython/ipython/issues/1866, but I didn't got the upshot of the discussion (it got too technical and complicated for me to follow).
Is there any solution or work around for this issue?
I'm using
python 2.7
matplotlib 1.4.1
IPython 2.1.0
Here is a working example:
%matplotlib inline
% config InlineBackend.figure_format = 'svg'
import matplotlib.pyplot as plt
a=range(10)
fig,ax=plt.subplots()
ax.plot(a,a)
ax.axis('off')
if you remove plt.axis('off') line, weird things happen only outside of the axis box.
P.S. Originally I encountered this problem in connection with drawing graphs with networkx. If I use draw from networkx this problem does not occur. If I use draw_networkx, same as described above happens. That might point to the core of the problem... I'm trying to figure out what line of code makes one work better than the other...
After tinkering around with the draw and draw_networkx functions from networkx module, I found the workaround which makes the difference between draw and draw_networkx in this case.
Adding fig.set_facecolor('w') overlays whatever is in the background, so the new plots are started with a white sheet (but not a blank one, I guess).
So new working example is:
%matplotlib inline
% config InlineBackend.figure_format = 'svg'
import matplotlib.pyplot as plt
a=range(10)
fig,ax=plt.subplots()
fig.set_facecolor('w')
ax.plot(a,a)
ax.axis('off')

Categories