Ubuntu 18.04 LTS: Ipython Matplotlib inline visualization: graphs are not plotted - matplotlib

I am working in Ubuntu 18.04. - Linux distro.
When I use Python I have no problem producing my graphs, tables and plots output.
When I switch to IPython instead of the expected table I get
Figure size 432x288 with 1 Axes
This is the script I am using from Dr. Hilpisch Python for finance O'Reilly books
Python 3.8.5 (default, Sep 4 2020, 07:30:14)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.18.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import matplotlib as mpl
In [2]: mpl.version
Out[2]: '3.3.2'
In [3]: import matplotlib.pyplot as plt
In [4]: plt.style.use('seaborn')
In [5]: mpl.rcParams['font.family'] = 'serif'
In [6]: %matplotlib inline
In [7]: import numpy as np
In [8]: np.random.seed(1000)
In [9]: y = np.random.standard_normal(20)
In [10]: x = np.arange(len(y))
In [11]: plt.plot(x, y);
Figure size 432x288 with 1 Axes
Thank You for your help

Related

Unable to generate plot using matplotlib

I am a beginner to Python and experimenting with a plot. the script runs fine but plot does not show up.
the matplotlib and numpy libraries are installed.
import numpy as np
f= h5py.File('3DIMG_05JUN2021_0000_L3B_HEM_DLY.h5','r')
#Studying the structure of the file by printing what HDF5 groups are present
for key in f.keys():
print(key) #Names of the groups in HDF5 file.
# will print the variables in the file
#Get the HDF5 group
ls=list(f.keys())
print("ls")
print(ls)
tsurf = f['HEM_DLY'][:]
print("tsurf")
print(tsurf)
tsurf1=np.squeeze(tsurf)
print(tsurf1.shape)
import matplotlib.pyplot as plt
im= plt.plot(tsurf1)
#plt.colorbar()
plt.imshow(im)```
Python version is 3 running on Ubuntu
Difficult to give you the exact answer without the dataset (please update the question with the dataset), but for sure, plt.plot does not return an object that can be plotted with plt.imshow
Try instead:
ax = plt.plot(tsurf1)
plt.show()
Probably the error was on the final plot.Try this:
import numpy as np
import matplotlib.pyplot as plt
f= h5py.File('/path','r')
ls=list(f.keys())
tsurf = f['your_key_str'][:]
tsurf1=np.squeeze(tsurf)
im= plt.plot(tsurf1)
plt.show(im) # <-- plt.show() NOT plt.imshow()

How to Plot a Table of Pandas DataFrame using Matplotlib

I want to plot a table of Pandas DataFrame using Matplolib tight_layout() in Colab.
First, LaTex was not found while running my code in Colab. I tried this but then I got an ValueError: list.remove(x): x not in list error. I get the same error in Jupyter-Lab too but in the terminal, it works!
How can I make this code work in Colab?
import pandas as pd
import matplotlib.pyplot as plt
# sample data
df = pd.DataFrame()
df['P(S)'] = [0.4, 0.3]
df['P(F)'] = [0.2, 0.1]
fig, ax = plt.subplots()
# hide axes
fig.patch.set_visible(False)
ax.axis('off')
ax.axis('tight')
ax.table(cellText=df.values, colLabels=df.columns, loc='center')
fig.tight_layout()
plt.show()
/usr/local/lib/python3.6/dist-packages/matplotlib/figure.py in get_default_bbox_extra_artists(self)
2234 bbox_artists.extend(ax.get_default_bbox_extra_artists())
2235 # we don't want the figure's patch to influence the bbox calculation
-> 2236 bbox_artists.remove(self.patch)
2237 return bbox_artists
2238
ValueError: list.remove(x): x not in list

%matplotlib inline not working with axes

in IPython if I use the following code, I can see the plot inline:
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot([1,2,3],[4,5,6])
but if I use this code, the graph does not appear inline or in separate window:
%matplotlib inline
import matplotlib.pyplot as plt
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot([1,2,3],[4,5,6])
I am running Python 3.5.2 |Anaconda 4.1.1 (64-bit)| on Windows 7
matplotlib version is 1.5.1

Anaconda Python 3.4 using Seaborn figure too tight

I am developing hierarchal clusters in the form of dendrograms using Python 3.4 and Seaborn, using the work of Olga Botvinnik (http://nbviewer.ipython.org/gist/olgabot/bfe1e3638af3eea52fb1#). My goal is to cluster U.S. cities based on greenhouse gas emissions. I was able to successfully read my csv file and create a figure with residential and commercial buildings emissions on the x axis and city names on the y axis, but I cannot see any of the city names because they are too squished together. The image needs to be elongated so that I can read it. Can anyone point me in a good direction?
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
data = pd.read_csv('/Users/JCMartel 1/Desktop/ghg_directory/rescom.csv', index_col=0)
data.index = data.index.map(lambda x: x.strip())
sns.clustermap(data);
#Need to improve layout
fig = plt.gcf()
fig.savefig('clustermap_bbox_tight.png', bbox_inches='tight')
Following is the final script that I am using:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
data = pd.read_csv('/Users/JCMartel 1/Desktop/ghg_directory/ghgmodel4.csv', index_col=0)
data.index = data.index.map(lambda x: x.strip())
cmap = sns.cubehelix_palette(as_cmap=True, rot=-.3, light=1)
sns.clustermap(data, col_cluster=False, cmap=cmap, linewidths=.5, figsize=(8, 30))

matplotlib axis don't converge properly

I make a graph using matplotlib and save it as a pdf. When I zoom in there is a gap where the x- and y-axis converge. Is there any way to get rid of this?
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 3])
y = np.array([1, 2, 3])
plt.scatter(x, y)
plt.savefig('Scatter_Plot.pdf')
Unfortunately I can not upload pictures here - but here is a link:
http://de.tinypic.com/r/25gckcw/8
Thanks
I've updated matplotlib 1.3.1 -> 1.4.3
Now everything looks perfect!