Scale plot size of matplotlib plots in Qubole Notebook - python

Is there a possibility of increasing the size of the plot plotted using z.showplot() in qubole notebooks.
import matplotlib as plt
plt.figure()
plt.bar(pandas_df_hr_sg[:]['hour'],pandas_df_hr_sg[:]['count'])
plt.title('Hourly wise user visits')
plt.xlabel('Hour')
plt.ylabel('Visit Count')
z.showplot(plt)

Could you try setting the plot size by:
plt.figure(figsize=(8, 8))
This should ideally help in case of Zeppelin notebook in Qubole; let me know how this goes!

Related

Python - save a figure with the same size and setting

I plot figures a lot during my python (through Spyder env.) usage. However, when I try to use plt.savefig('figure.png'), the saved figure has a different size from the inline figure plotted on Spyder.
For ex., when I use this command:
plt.savefig('fig1.png')
The saved figure looks like this:
Note that there's something weird with the saved figure, e.g.: the title is cropped, the size is not proportional.
However, the following is the inline figure:
I tried to modify the size through matplotlib.pyplot documentation but couldn't find such setting. Does anyone know how to save the figure with the exact setting as the inline plot?
The inline figure size plotted in Spyder (or any other IDE or editor) depends on how the editor handles showing figures.
If you want to have an exact size as output of your code, use figsize before plotting code. (It uses inches)
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 10))
# Code to produce figure
You can also determine DPI when creating figure or saving.
plt.figure(figsize=(10, 10), dpi=300)
# or
plt.savefig(file_path, dpi=300)

How to make group bars on jupyter notebook?

I am trying to plot multiple bars on the same plot on my jupyter notebook. However, due to some reason, it does not work on the notebook,but it works on a normal python editor. Here is the code that I have used.
import matplotlib.pyplot as plt
plt.bar(['A','B','C'],[72,73,88])
plt.bar(['A','B','C'],[98,77,98])
plt.show()
Any help will be highly appreciated.
(Edit)
I am looking for somthing like this on my jupyter notebook.
I presume you want to have both bar plots side by side. So here you go:
import matplotlib.pyplot as plt
fig,axs = plt.subplots(1,2)
axs[0].bar(['A','B','C'],[72,73,88])
axs[1].bar(['A','B','C'],[98,77,98])
plt.show()

How to zoom in graphs in colab

I am able to get an interactive graph in Google colab with the code:
!pip install mpld3
%matplotlib notebook
mpld3.enable_notebook()
df.plot(x = 'Time', y = 'Data')
but the plot is really small.
In R you can click on the graph and it will reopen in the new window. Is there a similar method to do this with colab? Thanks.
google-colaboratory does not provide any native data visualization, but it does support a variety of third-party visualization packages (matplotlib, altair, bokeh, plotly, etc.) You should look in the documentation of the library you are using to see how to adjust the size of figures.
In your example, you appear to be using pandas matplotlib plotting API. As the documentation mentions, you can adjust plot sizes with the figsize argument:
df.plot(x='Time', y='Data', figsize=(16, 10))
mpld3 is an interactive module for matplotlib.
You can understand its working here.
A simplifies answer to the same could be
import matplotlib.pyplot as plt
import mpld3
from mpld3 import plugins
fig, ax = plt.subplots()
ax.grid(True, alpha=0.3)
plt.plot(x, y)
mpld3.display()
where x & y are arrays of corresponding axis values.

Make Python seaborn heatmap bigger

I use a heat map in Python to show the correlation between all parameters I have. The number of parameters however are that large that the heat map becomes to small to show the data.
Heat Map
The heat map is created using seaborn:
seaborn.heatmap(df.corr())
I tried to make it bigger using:
plt.subplots(figsize=(10,10))
seaborn.heatmap(df.corr())
but this didn't work since the image just remained its current size.
Does someone know another way of doing this? Or maybe another way to clearly plot the correlations between all parameters?
Regards, Ganesh
You should create the figure first (similar to how you tried) using:
fig, ax = plt.subplots(figsize=(10,10))
Then, pass in ax as an argument to seaborn.heatmap
import matplotlib.pyplot as plt
import seaborn
fig, ax = plt.subplots(figsize=(10,10))
seaborn.heatmap(df.corr(), ax=ax)
plt.show()

Scale plot size of Matplotlib Plots in Jupyter Notebooks

Is there a possibility to scale the plot size of matplotlib plots in jupyter notebooks? You could increase the plot size by changing the default values of figure.figsize, but this does not affect parameters like fontsize, linewidth, markersize etc. What I need is a plot where all the parameters are scaled accordingly.
P.S.: To display plots in jupyter notebooks I use %matplotlib inline, see screenshot below.
Edit
For completeness, here is a code snippet doing exactly what I needed:
def scale_plot_size(factor=1.5):
import matplotlib as mpl
default_dpi = mpl.rcParamsDefault['figure.dpi']
mpl.rcParams['figure.dpi'] = default_dpi*factor
You don't want to change the figure size. You want to change the dpi (dots per inch).
Also see Relationship between dpi and figure size.
import matplotlib.pyplot as plt
%matplotlib inline
def plot(dpi):
fig, ax=plt.subplots(dpi=dpi)
ax.plot([2,4,1,5], label="Label")
ax.legend()
for i in range(1,4):
plot(i*72)

Categories

Resources