How to save a fig with a title above? - python

I created two graphs with a title for each ("petit titre") and a large title for both of them ("GRAND TITRE"). To nicely display the graph on my jupyter notebook, I anchored them as follow:
ax.set_title('petit titre A', pad=25, loc='left')
and
fig.suptitle("GRAND TITRE", y=1.10)
Then, jupyter displays this following graph:
Now, and here is my trouble, I would like to save it:
fig.savefig(os.path.join(path_img, 'Fig1.png'), dpi=600)
fig.savefig(os.path.join(path_img, 'Fig1.pdf'))
However, what I finaly get is this:
I do understand that, cause of my title anchored with an y-value = 1.1, I'm above the fig. But, it seems to me the only way to nicely organise my graph. How can I do to ask a plot taking into account the "grand titre" ? Idem if I have a legend box?
In advance, thanks for your help.
Best,

Try plt.figure(constrained_layout=True) when creating the figure and not anchoring the subtitle (i.e. just fig.suptitle("GRAND TITRE")). https://matplotlib.org/stable/tutorials/intermediate/constrainedlayout_guide.html#suptitle
Or, if you really want to anchor outside the figure, try fig.savefig(os.path.join(path_img, 'Fig1.png'), dpi=600, bbox_inches='tight') (https://matplotlib.org/stable/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure.savefig)

Related

Is there a way to have a size/radius legend in Bokeh?

I'd like to create a scatter plot in Bokeh. Right now, I am using bokeh.plotting.figure.circle to create one. If I set the radius=radvar argument, where radvar is a valid string for my source, can I add some kind of legend so the viewer can see the scale?
Here's an example of what I'm doing now:
p=figure(tools=TOOLS)
p.circle(
x=xvar,
y=yvar,
radius=radvar,
radius_units='screen',
color={
'field':colorvar,
'transform':color_mapper},
source=data)
Seaborn has support for this kind of legend. Here's an example I found on the internet:
I'm not picky with howthe scale is shown. It could be just outlines, for example.
for legend, as far as i know, no. but for glyphs, yes. points.glyph.size (points name refers to points = p.scatter(...),) you could create size in data and create different sizes.
`

Customising legend in Matplotlib

I’m trying to customise individual legend labels. In the example below, the legend contains two items. I’d like to make the text bold for only the second legend label.
Here’s a general outline of the code:
leg = plt.legend()
for text in leg.get_texts():
text.set_fontweight...
Here’s a runnable example:
import matplotlib.pyplot as plt
X=range(10)
Y=range(100,110)
Z=range(105,115)
plt.plot(X,Y,label='normal')
plt.plot(X,Z,label='bold')
fontweights=['normal','bold']
leg=plt.legend()
for fw,text in zip(fontweights,leg.get_texts()):
text.set_fontweight(fw)
plt.show()
Here's the output:
enter image description here
The plot produced shows that set_fontweight() changes both labels to bold. So is this a bug with set_fontweight(), or am I doing something wrong?
Similar functions, such as text.set_color(), can be used to modify legend labels individually.
Lastly, I’m using matplotlib version 3.2.2.
Thanks!
I have matplotlib 3.3.4 running, and get a bold and unbold legend entry - see attached image - so I think an upgrade should fix the problem.

How to get colormap plots to fill area?

I'm relatively new to visualization with python. I'm trying to visually show correlation between attributes using a color map, but for some reason the plots aren't filling the entire graph (see pic).
Also, I understand the ticks are bunched (there's 34 attributes), but I wanted to fix the fill issue first. For reference here is the code I have:
correlation = wounds.corr()
fig = plt.figure()
ax = fig.add_subplot(111)
cax = ax.matshow(correlation,cmap='coolwarm', vmin=-1, vmax=1)
fig.colorbar(cax)
ticks = np.arange(0,len(wounds.columns),1)
ax.set_xticks(ticks)
plt.xticks(rotation=90)
ax.set_yticks(ticks)
ax.set_xticklabels(wounds.columns)
ax.set_yticklabels(wounds.columns)
plt.savefig('correlation.jpg')
plt.show()
This is my first time posting here so forgive me if anything is wrong with my question.
Edit: Added code for reference
Best way is to use pandas profiling
Try it out! it would change your life :)
(you can use it directly in your Jupyter notebook):
import pandas_profiling
report = df.profile_report(style={'full_width':True})
If you data is really big you can sample it randomly:
report = df.sample(100000).profile_report(style={'full_width':True})
And you can save it to a file:
report.to_file(output_file="profiling_report.html")

Why am I facing with empty graph without any error?

import matplotlib.pyplot as plt
plt.figure(figsize=(20,10))
plt.plot(norway.Freedom, norway.Economy_GDP_per_Capita, color = 'navy', label = 'Norway')
plt.legend(loc = 'upper left')
plt.title('Effect of GDP against Freedom')
plt.xlabel('Freedom')
plt.ylabel('GDP')
plt.show()
Example row in the data:
Output:
I make a quick start to Data Science and I am trying to make some analysis on Kaggle. I write a kernel for plotting a line as you see in my code, although the graph is empty and I can not see anything.
Besides that, there is no error or something. I need help with it.
Please try to explain without going deep, I am a beginner.
Thanks everybody who will help.
I am not sure whether you guys can see my code and graph...
It's just because your dataframe has only one single row, therefore both norway.Freedom and norway.Economy_GDP_per_Capita are just single numbers so you are trying to plot one single point. If you try to plot this without markers, you'll see nothing. Try
plt.plot(norway.Freedom, norway.Economy_GDP_per_Capita, 'x', color = 'navy', label = 'Norway')
which adds x-markers to the plot.
Or choose a dataframe with more than one row of data...

multi chart in python

I have the following code:
pl.bar (x1,x2)
pl.show()
pl.plot(x1,x3)
pl.show
It generated two separate chart one bar chart and one plot. I want to have bar and plot in one single graph. Could you please let me know how I can make it?
Thanks,
Amir
Assuming pl is matplotlib.pylab:
pl.bar(x1,x2)
pl.plot(x1,x3,color='r')
pl.show()
Note the change of color for contrast.
Take out the first call to pl.show(). This way you add both items to pl before showing it.

Categories

Resources