Why am I facing with empty graph without any error? - python

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...

Related

Sns Scatter plot with differet color coding and different markers

I would like to create a Seaborn scatter-plot, using the following dataframe:
df = pd.DataFrame({'A':[1,2,3,4],'B':[2,4,6,8],'C':['y','y','n','n'],'D':[1,1,2,2]})
In my graph A should be the x-variable and B the y-variable. Furthermore I would like to color based on column D. Finally, when C='y' the marker should be open-faced (no facecolor) and when C='n' the marker should have a closed. My original idea was to use the hue and style parameter:
sns.scatterplot(x='A', y='B',
data=df, hue='D',style ='C')
However, I did not manage to obtain the graph I am looking for. Could somebody help me with this? Thank you in advance.
One cannot specify entire marker styles (so 'marker' and 'fillstyle' keys in your case) for matplotlib yet. Have a look on the answer to this post.
So the only thing left for you is to use different markers right away and specify them (as list or dictionary)
sns.scatterplot(data=df, x='A', y='B', hue='D', style='C', markers=['o', 's'])
plt.show()
Apparently, it is very hard to even create non-filled markers in seaborn, as this post explains. The only option is to do some matplotlib-seaborn-hybrid thing... So if you accept to plot things twice onto the same axis (one for a filled marker and one for the unfilled markers), you still have to dig yourself into the quirks of seaborn...

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

Colorbar for each row in ImageGrid

Disclaimer: I am very inexperienced using matplotlib and python in general.
Here is the figure I'm trying to make:
Using GridSpec works well for laying out the plots, but when I try to include a colorbar on the right of each row, it changes the size of the corresponding subplot. This seems to be a well known and unavoidable problem with GridSpec. So at the advice of this question: Matplotlib 2 Subplots, 1 Colorbar
I've decided to remake the whole plot using ImageGrid. Unfortunately the documentation only lists the options cbar_mode=[None|single|each] whereas I want 1 colobar per row. Is there a way to do this inside a single ImageGrid? or will I have to make 2 grids and deal with the nightmare of alignment.
What about the 5th plot at the bottom? Is there a way to include that in the image grid somehow?
The only way I can see this working is to somehow nest two ImageGrids into a GridSpec in a 1x3 column. this seems overly complicated and difficult so I don't want to build that script until I know its the right way to go.
Thanks for any help/advice!
Ok I figured it out. It seems ImageGrid uses subplot somehow inside it. So I was able to generate the following plot using something like
TopGrid = ImageGrid( fig, 311,
nrows_ncols=(1,2),
axes_pad=0,
share_all=True,
cbar_location="right",
cbar_mode="single",
cbar_size="3%",
cbar_pad=0.0,
cbar_set_cax=True
)
<Plotting commands for the top row of plots and colorbar>
BotGrid = ImageGrid( fig, 312,
nrows_ncols=(1,2),
axes_pad=0,
share_all=True,
cbar_location="right",
cbar_mode="single",
cbar_size="3%",
cbar_pad=0.0,
)
<Plotting commands for bottom row . . .>
StemPlot = plt.subplot(313)
<plotting commands for bottom stem plot>
EDIT: the whitespace in the color plots is intentional, not some artifact from adding the colorbars

Python using custom color in plot

I'm having a problem that (I think) should have a fairly simple solution. I'm still a relative novice in Python, so apologies if I'm doing something obviously wrong. I'm just trying to create a simple plot with multiple lines, where each line is colored by its own specific, user-defined color. When I run the following code as a test for one of the colors it ends up giving me a blank plot. What am I missing here? Thank you very much!
import numpy as np
import matplotlib.pyplot as plt
from colour import Color
dbz53 = Color('#DD3044')
*a bunch of arrays of data, two of which are called x and mpt1*
fig, ax = plt.subplots()
ax.plot(x, mpt1, color='dbz53', label='53 dBz')
ax.set_yscale('log')
ax.set_xlabel('Diameter (mm)')
ax.set_ylabel('$N(D) (m^-4)$')
ax.set_title('N(D) vs. D')
#ax.legend(loc='upper right')
plt.show()
The statement
ax.plot(x, mpt1, color='dbz53', label='53 dBz')
is wrong with 'dbz53' where python treated it as a string of unknown rgb value.
You can simply put
color='#DD3044'
and it will work.
Or you can try
color=dbz53.get_hex()
without quote if you want to use the colour module you imported.
In the plot command, you could enter Hex colours. A much more simple way to beautify your plot would be to simply use matplotlib styles. For instance, before any plot function, just write
plt.style.use('ggplot')

matplotlib x-axis formatting if x-axis is pandas index

I'm using iPython notebook's %matplotlib inline and I'm having trouble formatting my plot.
As you can see, my first and last data point aren't showing up the way the other data points are showing up. I'd like to have the error bars visible and have the graph be "zoomed out" a bit.
df.plot(yerr=df['std dev'],color='b', ecolor='r')
plt.title('SpO2 Mean with Std Dev')
plt.xlabel('Time (s)')
plt.ylabel(SpO2)
I assume I have to use
matplotlib.pyplot.xlim()
but I'm not sure how to use it properly if my x-axis is a DataFrame index composed of strings:
index = ['-3:0','0:3','3:6','6:9','9:12','12:15','15:18','18:21','21:24']
Any ideas? Thanks!
You can see the usage of xlim here. Basically in this case if you ran plt.xlim() you would get(0.0, 8.0). As you have an index that uses text and not numbers the values for xlim are actually just the index of the entries in your index. So in this case you would just need to change the values by feeding in however many steps left and right you want your graph to take. For example:
plt.xlim(-1,len(df))
Would change this:
to this:
Hope that helps.

Categories

Resources